Problem/Motivation
- K/V store uses a merge() query
- Merge() does do DELETE + INSERT _every_ time
- Therefore the data is always written - even if its the same
Proposed resolution
- Use an upsert() instead
- While upsert() usually can only be used when there is one key, the implementation works fine to update also the collection as long as (key, collection) is a unique index. (and it should be)
- In case we don't want upsert, we could get() before set() and not set() if its the same - we are racy anyway, so the get() before set() is not a problem.
Both save 3s / 80s for installation of standard profile.
Remaining tasks
- Do it
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 2755831-18-with-3191465-2.patch | 11.69 KB | daffie |
Issue fork drupal-2755831
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
fabianx commentedThis will likely fail unit tests.
Also setIfNotExists() is totally wrong - as it always sets via the merge query. Should use a try { insert() } instead.
Comment #3
fabianx commentedComment #4
fabianx commentedThanks for whoever tested PGSQL, which does not support upsert(), but uses DELETE + INSERT.
I think get() before set() is easier and because write operations are so expensive, this should still save a lot.
We can likely do it also just in state() (one of the biggest offenders during installation) or use a quick decorator, so that it works for all backends.
This might save ~ 1 min for test bot PHP 5.5.
Comment #5
bzrudi71 commented@Fabianx, it was me. I had the feeling that this will fail on PostgreSQL ;) Native UPSERTs are supported as of PostgreSQL >= 9.5, for all other versions we had to work around in #2542776: Add an Upsert class. But that made PostgreSQL testing terrible slow. We worked around that in #2549323: Improve the Upsert query implementation for PostgreSQL, but it's still not as fast as before the UPSERT implementation and seems buggy anyway, so not sure what can be done in this issue to make PostgreSQL rock again ;)
Comment #11
t-loHere's a re-roll
Comment #16
daffie commentedComment #17
anmolgoyal74 commentedRe-rolled.
Comment #18
daffie commented@anmolgoyal74: Thank you for the reroll.
I see now that changing the query from a merge query to a upsert query is failing, because upsert queries on take one key and the key_value table has a primary key with multiple columns.
I opened #3191465: Change upsert queries to allow multiple keys to change upsert queries to take in multiple key fields.
Postponing this issue on the mentioned issue.
Adding a combined patch to see if it works.
Comment #19
daffie commentedThis issue is also blocked by #3159073: Use the new UPSERT capability from SQLite 3.24.
Comment #20
amateescu commentedLooks like a duplicate of #2547493: Add support for unique / primary key constraints composed of multiple fields for Upsert queries.. or that is a duplicate of this one :)
Comment #21
daffie commented@amateescu: I forgot about that issue. I will let you choose which one to close. My preference would be to do the one that you will think will land the quickest.
Comment #22
catch#3159073: Use the new UPSERT capability from SQLite 3.24 is in.
Comment #31
longwaveNot sure if this needs to be postponed.
In a trivial functional test we do 2,639 database queries, many of which are to the keyvalue and config tables. Swapping the merge queries for upsert drops 285 (11%) of these queries. A spot check of some tests still pass locally, let's see what CI thinks.
Comment #33
longwaveOK so that nearly worked, but broke all the build tests :)
Comment #34
amateescu commentedThe
key_valuetable has a composite primary key, so we still have to do #2547493: Add support for unique / primary key constraints composed of multiple fields for Upsert queries first :)Comment #35
amateescu commentedLooking at the MR from that issue again, this is actually a duplicate because we're changing the key_value queries there.
Comment #37
amateescu commented