Problem/Motivation
We are running a quite big multi domain drupal site (all separate databases but running on the same database server) and found that in some cases we had lots of translation strings which needed to be updated within deployment of new versions.
Updating these would often trigger an update to the field 'version' in the table 'locales_location', which would end up being comparably slow. The reason we found was that the WHERE clause would contain a search by the field 'name', and that one is not indexed.
UPDATE locales_location SET version='8.8.8'
WHERE (sid = '5760') AND (type = 'path') AND (name = '/our/custom/path');We created a composite index for the contained fields and could see that our database load dropped by ~50% (!) immediately after that.
CREATE INDEX sid_type_name on locales_location (sid,type,name);
Point is that we actually do not want to do anything custom to Drupal's Core, respectively the core database structure if we can avoid it, and there might be reasons why there's no index on this field. Clearly creating an index could slow down operations like INSERT a bit. But this table seems to see far more SELECTs or UPDATEs where 'name' is involved.
So what I'm hoping to achieve here is either confirmation this should be considered an improvement and go into Drupal Core, or getting an answer explaining why this index is a bad idea.
Thank you in advance!
Proposed resolution
Index 'name' field in locales_location table
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Issue fork drupal-3156439
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
matsbla commentedComment #3
lendudeSounds reasonable. Moving to the right version and since there is indeed a trade off here, I'm going to mark this as a feature and not a bug.
Comment #9
mkalkbrennerWe just ran into a similar issue when upgrading from Drupal 10.2.x to Drupal 10.3.0. The DB update took very long, 16min 23sec.
So I monitored the database queries during the update and found thousands of similar queries that could not use an index and lead to full table scans:
After adding an appropriate index with
these queries run much faster and the entire update only took only 36sec! The explain of such a single query with that index is
I think that this is a major issue. An update that takes more that 16min looks like the update process is frozen.
If we combine this with the initially reported issue, the index should be
Comment #10
alexpott@mkalkbrenner thanks for the sleuthing.
There's is already an index on
So I think - looking at the queries on the issue and in the code that we only need to add an index on type and name. The query that requires this index is in \Drupal\locale\StringDatabaseStorage::dbStringSelect(). The other queries in the code all use lid (the primary key) or have sid and type defined which is already indexed.
Comment #12
alexpottThis issue is going to need us to resolve #3108658: Handling update path divergence between 11.x and 10.x... as ideally we'd be able to backport this to 10.x
Comment #13
catchWe could avoid blocking this on #3108658: Handling update path divergence between 11.x and 10.x if we backport it to 10.3, given it's in locale module so should benefit everyone with locale module installed, and won't run on sites without it installed, that might be OK but would need a second opinion.
Alternatively we might be OK if we have it in 10.4, 11.0 and 11.1 (and not 10.3) - but only if we don't backport any other database updates to locale module in 10.3 (very unlikely).
Comment #14
alexpottI think given it is an index addition planning to backport everywhere and get it in 11.0.x before 11.0.0 makes sense. Will change the update number to reflect this.
Comment #16
alexpottI've added an MR for 10.x and set the update number to 10101 which is the next available number. This could now be committed to 10.3.x and up as long as we get this done before 11.0.0....
Comment #17
longwaveFWIW the impact here is low and to ease the transition between Drupal 10 and 11 I agree that we should backport this to 11.0.
I would suggest setting the update number to 10300 if this is going to land in 10.3 though as that follows the convention we have used so far.
Comment #18
alexpottChanged the update number to @longwave's recommendation.
Comment #19
catchOne comment on the MR.
Comment #21
catchTwo more comments on the MR - one from me, one via @xjm in slack. Other than that looks good though and I think we should backport to 10.3
Comment #22
quietone commentedComment #23
mkalkbrennerI tested https://git.drupalcode.org/issue/drupal-3156439/-/tree/3156439-db-table-... and updated from 10.2 to 10.3 again. I can confirm that it increased the speed of the update as expected, 30 seconds instead of more than 16 minutes!
Comment #24
alexpottI've fixed two of the MR comments. I'm not convinced about the defensive coding for the update. I don't think we should support custom schema variations on module provided schema. And it erroring is good because in I think it requires human interaction to know what to do... say the index is called type_name and is an index on type, name and sid for example. Or it is sid, type, name as per the example original request.
Comment #25
catchThis looks great now, RTBC for me.
Comment #30
longwaveThanks, I think this satisfies all comments on the MR now.
Committed and pushed e81e6f869d to 11.x and bb09a54352 to 11.0.x. Thanks!
Committed and pushed 5f2858c540 to 10.4.x and dd402f2fec to 10.3.x. Thanks!
Tagging for 10.3.1 release notes as we don't usually add update hooks to patch releases.
Comment #31
quietone commentedThe update test here is failing on PostgreSQL, https://git.drupalcode.org/project/drupal/-/pipelines?page=1&scope=all&r...
Comment #33
alexpottCreated a small MR to get the test to pass on Postgres... by just skipping the index inspection shenanigans there.
Comment #36
alexpott@quietone opened #3458922: Fix index test in LocalesLocationAddIndexUpdateTest::testExistingIndex so marking this back as fixed.
Comment #39
xjmAmending attribution.