[9.x] Add index hinting support to query builder #46063
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds
useIndex()
,forceIndex()
andignoreIndex()
methods to the query builder, to help MySQL's (and others) query planner use the indexes I want for a query. I've had to do this in the past to optimize slow queries where the planner didn't choose to use an index even though it was dramatically more performant, and today I came across @assertchris doing the same thing, so I thought it might be worth adding to the ORM for other people to use.Support across engines is spotty, with only MySQL supporting all three hinting modes. Based on my research this seems to be the support:
I have only implemented these where supported, for engines without support the query will be unchanged by the
useIndex()
,forceIndex()
andignoreIndex()
methods.I have also only added support for hinting one index. MySQL supports the use of multiple
USE INDEX
hints, and a combination ofUSE INDEX
hints withIGNORE INDEX
hints, but I have left that unimplemented for simplicity and to test the water on if this change is wanted. If we want to add support for hinting multiple indexes, I can add that functionality.A 4th method called
ignoreIndexes()
could be added to support SQLite and SQL Server's ability to ignore all indexes and do full table scans, but I struggle to think of why you'd actually want to do that in a real environment.