Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index hints not being passed from QueryBuilder to final Find #2346

Closed
hmatanock opened this issue Jul 23, 2021 · 2 comments
Closed

Index hints not being passed from QueryBuilder to final Find #2346

hmatanock opened this issue Jul 23, 2021 · 2 comments
Labels
Milestone

Comments

@hmatanock
Copy link

BC Break Report

Q A
BC Break yes
Version 2.2.0

Summary

The database for our project is actually AWS's DocumentDB service. I will say off the bat that I do understand that it's not technically MongoDB, and there are some feature differences - however, we recently upgraded our Symfony project from v 1.1 to v2.0 (technically we upgraded doctrine/mongo-odm-bundle from ^3.5 to ^4.3) and things were working much better before the upgrade.

We are experiencing very long query times on some particular queries, that are definitely covered by indexes. Performance logging in AWS indicates that Document DB is choosing to not use any indexes for the query, and so in this case the AWS documentation recommends using index hints on the queries to force DocumentDB to use the index.

Previous behavior

Before we upgraded, the hints were being added to the find query

Current behavior

Index hints are not passing through to the query.

How to reproduce

I'm actually not totally sure how to describe how to reproduce the behavior, but I've identified where in the code this is going wrong.

First, we use DoctrineQueryBuilder->hint() method to create a query, and add an index hint.
https://github.com/doctrine/mongodb-odm/blob/2.2.x/lib/Doctrine/ODM/MongoDB/Query/Builder.php#L782

In that MongoDB/Query/Query object that is created, the hint is definitely present, but when it comes time to pass the data to the collection->find(), the hint is not added to the $queryOptions
https://github.com/doctrine/mongodb-odm/blob/2.2.x/lib/Doctrine/ODM/MongoDB/Query/Query.php#L427

For some reason the hint IS being added to count queries later in that same switch, so I'm not sure if it's being intentionally left off of the finds:
https://github.com/doctrine/mongodb-odm/blob/2.2.x/lib/Doctrine/ODM/MongoDB/Query/Query.php#L508

In my test server, if I manually add 'hint' to line#427 above, my page load time goes from around 14803ms to 505ms.

@malarzm
Copy link
Member

malarzm commented Jul 26, 2021

Looking at https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-find/ this is definitely an oversight, find is accepting the hint option. @hmatanock would you be up for a PR fixing the issue and covering it with a test so we won't lose this again?

EDIT: actually I'm not sure if a test can be written easily, still worth exploring tho

@malarzm malarzm added the Bug label Jul 26, 2021
@malarzm malarzm added this to the 2.2.2 milestone Jul 26, 2021
@alcaeus
Copy link
Member

alcaeus commented Jul 29, 2021

actually I'm not sure if a test can be written easily, still worth exploring tho

Here's a test plan:

  1. Create query builder, set index hind
  2. Export the query object, assert that the debug information about the query shows the index hint
  3. Execute the query while a command logger is active (see prior art, e.g. GH1138Test.php)
  4. Assert that the find command sent to the server contains the correct hint query option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants