FIX default_sort has no associated index#11680
FIX default_sort has no associated index#11680lekoala wants to merge 1 commit intosilverstripe:5.3from
Conversation
|
Looking deeper into this, it seems that should already be dealing with that, but it doesn't seem to be working as expected it's even mentionned in the docs
not sure what's going on here |
|
ok so after a bit of testing, currently the default sort is split into two indexes, one for Surname and one for FirstName here silverstripe-framework/src/ORM/DataObjectSchema.php Lines 642 to 667 in b4d6cb7 Where in the current stateThis works "mostly" well when using a where clause, eg there is still a filesort, but since it's very likely on a small resultset, it's not noticeable No where clause and a limitthese indexes seem to be ignored when there is no where clause (could be db engine dependent, I'm testing on mysql 8.0) No multi column index => SELECT * FROM Member ORDER BY Surname ASC, FirstName ASC LIMIT 1 With multi column index => SELECT * FROM Member ORDER BY Surname ASC, FirstName ASC LIMIT 1 Where and multiple indexeswhen there are multiple indexes (the two generated from the default_sort, and the one I added), the engine is not always super smart vs EXPLAIN SELECT * FROM Member FORCE INDEX(SortedName)
WHERE FirstName LIKE 'test%' AND Surname LIKE 'test%'
ORDER BY Surname ASC, FirstName ASC LIMIT 1this is not the first time I have issues with combined indexes that are not as fast as expected. I haven't looked too much into it, but things may get even worse when joining on a table (a Member with many_many other Members) I'm thinking that maybe splitting columns from the default_sort is not always great and it would be much better to actually use a multi column index some documentation on multi columns indexes: https://dev.mysql.com/doc/refman/8.4/en/multiple-column-indexes.html |
|
Closing in favour of #11747 |





Description
See issue for context
This PR adds a new index that matches the default_sort
This index should help many queries, also when filtering by surname/firstname (eg: in autocomplete scenarios). With this index, it means it actually better to filter against Surname/FirstName rather than just Surname
Manual testing steps
Simply use
Member::get()->first()without the index, the db engine may be using filesort due to default_sort being used
would show "using filesort" on all records of the table
with the index, it shouldn't, and it will fetch the first record from the index directly
for filters, queries like
will now be using the index as well resulting in much faster queries
Issues
#11674
Pull request checklist