doctrine / mongodb-odm

The Official PHP MongoDB ORM/ODM
https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
MIT License
1.09k stars 502 forks source link

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

Closed hmatanock closed 3 years ago

hmatanock commented 3 years ago

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 commented 3 years ago

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

alcaeus commented 3 years ago

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