[9.x] Add index hinting support to query builder#46063
Conversation
1edb6f3 to
93b7907
Compare
93b7907 to
6b12931
Compare
|
Note from MySQL documentation. They have plans to deprecate and remove these hints.
|
|
Hmm, well, we probably should not have added this then. |
|
Damn, I wasn't aware of that depreciation. This complicates things for MySQL > 8.0. We can still support forcing and ignoring indexes, but the syntax and where you specify the hinting in the query has now changed to: (I attempted to move the index hinting to different parts of the query, but it only works if it's between the So to support all versions of MySQL without introducing a breaking change, I'd have to:
I could simplify this and only support index hinting on MySQL 8.0 and above, but that would be a breaking change since this PR is already released with Laravel 10, so I assume it'd go to Let me know how you'd like to handle this. |
|
@cwhite92 What decision have you made about it? Because current MySQL implementation will stop working at one point I think.
I think this is the most suitable for this situation. |
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 INDEXhints, and a combination ofUSE INDEXhints withIGNORE INDEXhints, 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.