Add optimizer hint query api#18688
Conversation
|
For the |
|
If we don't mind supporting only mysql 8.4+, we don't need the legacy index hint api and can just use the optimizer hint. |
455c7ab to
3af2513
Compare
|
Optimizer hints can be used for various query types, not just |
No, we can make optimizer hints generic. I used SelectQuery for the RFC because the original index hints were limited. I'm not really sure the syntax for index hints in update statements but I assume it's right after the table name. |
From the mysql docs:
|
|
For the optimizer hint, but not for the legacy index hint. |
Right, I was only asking about the |
|
Seems SQL Server also has query hints which are specified using the https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql?view=sql-server-ver16 |
|
Oracle uses the special comment format similar to MySQL and pg_hint_plan. https://docs.oracle.com/cd/B10500_01/server.920/a96533/hintsref.htm |
This syntax varies. I think it simplest way to support it would be to put the entire value into $name. |
Do we think it's worth supporting both? I don't mind, but it could get confusing since index hint is mysql only. Probably not that confusing though. |
I am wondering if instead of having
I am wondering the same, since index hints are mysql only and planned to be phased out. It's been over a year since mysql 8.4 is out and wouldn't it be safe to assume that those who need this feature would be able to install the required mysql version? |
3af2513 to
714f135
Compare
67e2276 to
e7c0d7d
Compare
a4d85f5 to
36906d5
Compare
| /** | ||
| * @var string Optimizer hint comment opening | ||
| */ | ||
| protected const HINT_COMMENT_OPEN = '/*+'; |
There was a problem hiding this comment.
Are these constants necessary? We have templates for other clauses (though I see we can't just add a string for this to $_templates due to the way it's used).
There was a problem hiding this comment.
Looks like Oracle uses the same prefix so I'm going to hard-code it for now instead.
36906d5 to
0113ecb
Compare
0113ecb to
d30eaec
Compare
|
Renamed query api and clause to |
There was a problem hiding this comment.
Pull Request Overview
Adds support for passing engine-specific optimizer hints via a new optimizerHint() API, and integrates hint comments into compiled SQL when the driver supports them.
- Introduces
optimizerHintclause in all query types (Select, Insert, Update, Delete) - Extends
QueryCompilerto render hint comments based on a newOPTIMIZER_HINT_COMMENTfeature flag - Adds comprehensive tests for hint behavior and adjusts driver support maps
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/TestCase/Database/QueryTest.php | Added testOptimizerHintClause() for the new clause |
| tests/TestCase/Database/QueryCompilerTest.php | Tests for SQL compilation of optimizer hints |
| src/Database/QueryCompiler.php | Hooked _buildOptimizerHintPart() into SQL builders |
| src/Database/Query/SelectQuery.php | Added optimizerHint to select query parts |
| src/Database/Query/InsertQuery.php | Added optimizerHint to insert query parts |
| src/Database/Query/UpdateQuery.php | Added optimizerHint to update query parts |
| src/Database/Query/DeleteQuery.php | Added optimizerHint to delete query parts |
| src/Database/Query.php | Added optimizerHint() API to base Query class |
| src/Database/DriverFeatureEnum.php | New OPTIMIZER_HINT_COMMENT feature enum |
| src/Database/Driver/Sqlserver.php | Marked hints unsupported |
| src/Database/Driver/Sqlite.php | Marked hints unsupported |
| src/Database/Driver/Postgres.php | Marked hints supported |
| src/Database/Driver/Mysql.php | Marked hints supported |
Comments suppressed due to low confidence (2)
tests/TestCase/Database/QueryCompilerTest.php:225
- [nitpick] The method name has typos and is inconsistent: rename to
testUpdateWithOptimizerHintto match other test naming conventions.
public function testUpdateWitOptimizerhHint(): void
tests/TestCase/Database/QueryCompilerTest.php:209
- [nitpick] The test name omits 'Hint'—consider renaming to
testUpdateWithOptimizerHintfor consistency with the other hint tests.
public function testUpdateWithOptimizer(): void
| /** | ||
| * Builds the optimizer hint comment part. | ||
| * | ||
| * @param list<string> $parts The optmizer hints |
There was a problem hiding this comment.
Typo in docblock: change 'optmizer' to 'optimizer'.
| * @param list<string> $parts The optmizer hints | |
| * @param list<string> $parts The optimizer hints |
Add optimzer hint query api
refs #18140
Example mysql index hint:
https://dev.mysql.com/doc/refman/8.4/en/index-hints.html
Example mysql optimizer hint (added in 8.4 with plans to warning to deprecate index hint but not done so):
https://dev.mysql.com/doc/refman/8.4/en/optimizer-hints.html
The postgresql extension has syntax compatible with mysql hint syntax so the api should work for both:
https://github.com/ossc-db/pg_hint_plan/blob/master/docs/hint_details.md