Skip to content

Conversation

@einarsozols
Copy link

@einarsozols einarsozols commented Dec 1, 2024

This pull request extends the functionality introduced in PR #46063, which added index hints to select queries.

Select query:

User::useIndex('test_index')->where('name', 'bar')->get();

Would generate sql:

select * from `users` use index (test_index) where `name` = 'bar'

But:

User::useIndex('test_index')->where('name', 'bar')->update(['name' => 'bar2']);

Would generate:

update `users` set `name` = 'bar2' where `name` = 'bar'

But it should generate:

update `users` use index (test_index) set `name` = 'bar2' where `name` = 'bar'

With this update, index hints can now also be applied to update and delete queries.

Edge case for delete in mysql, because of: Index hints work with DELETE statements, but only if you use multi-table DELETE syntax

This would not work:

delete from `users` use index (test_index)

This would work:

delete `users` from `users` use index (test_index)

@einarsozols einarsozols changed the title Add index hints support for update and delete queries [11.x] Add index hints support for update and delete queries Dec 1, 2024
@einarsozols einarsozols force-pushed the 11.x branch 2 times, most recently from 8bd5ab2 to 41b410d Compare December 1, 2024 13:11
@shaedrich
Copy link
Contributor

Are you aware of the deprecation and the syntax change?

@einarsozols
Copy link
Author

Are you aware of the deprecation and the syntax change?

I am aware of the deprecation note regarding the USE INDEX, FORCE INDEX, and IGNORE INDEX hints in MySQL. However, it is important to note that as of now, these hints are still not deprecated and are fully supported. Additionally, the current MySQL 8.4 documentation still provides examples using the old syntax for index hints, indicating that they are still in use and relevant for developers.
For more information, MySQL 8.4 documentation on index hints.

The main concern is that the current implementation of the forceIndex, useIndex, ignoreIndex methods in the Laravel framework is incomplete, as it does not support update and delete queries.

Furthermore, Laravel 11 supports MySQL 5.7+ as stated in the Laravel database documentation. To support the new syntax, we would need to drop support for MySQL 5.7 or support both syntaxes based on the MySQL version.

This comment provides a detailed explanation of the situation. The simplest solution would be to drop support for MySQL 5.7 and use only the new syntax for Laravel 12.

I still think that this pull request is valuable as it handles update and delete operations. It should also be adapted to the new syntax, as managing all cases with the old syntax will make the transition to the new syntax easier.

@shaedrich
Copy link
Contributor

I am aware of the deprecation note

👍🏻

I still think that this pull request is valuable as it handles update and delete operations.

No objection here

The main concern is that the current implementation of the forceIndex, useIndex, ignoreIndex methods in the Laravel framework is incomplete […] To support the new syntax, we would need to drop support for MySQL 5.7 or support both syntaxes based on the MySQL version. […] This comment provides a detailed explanation of the situation. […] It should also be adapted to the new syntax, as managing all cases with the old syntax will make the transition to the new syntax easier.

Yeah, that should probably a separate PR and probably results in discussion that would only slow this one down anyway.

@taylorotwell
Copy link
Member

Given the deprecations and the fact that index hints generally should avoided in production code anyways I think I will wait on this one for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants