Repository navigation
Compile regular index keys once and keep the indexes in step on DELETE, UPDATE and TRUNCATE to close #2880 - #2987
mateuscmtropical wants to merge 2 commits into
Conversation
…E, UPDATE and TRUNCATE to close AlaSQL#2880
|
Thanks for this @mateuscmtropical The stale index after DELETE and UPDATE is a real bug, and INSERT got about 3x faster too. Nice. But... Every UPDATE and DELETE now rebuilds all regular indexes from every row. On a 50,000-row table with two inline indexes, 200 single-row UPDATEs by primary key goes up 64x in processing time. 200 DELETEs goes up 15x. You already have addToIndexes and removeFromIndexes, please see if you can use them for the changed rows and only rebuild in full above some threshold. In general we prefer quality over quantity. If you could ask your AI to do a review of the preformance impact of your revews that would make the world a better place for all of us. |
|
Done. UPDATE and DELETE now move only the changed rows. Changes are grouped by index bucket, and each touched bucket is read once, so a large change costs no more than a rebuild and no threshold is needed. A row whose key does not change keeps its place. INSERT OR REPLACE takes the same path, and an UPDATE that fails on a later row still leaves the indexes matching the rows. Timings with two indexes, median of two runs in ms:
INSERT is faster than develop because the index key function is now compiled once instead of once per row. The bulk UPDATE and DELETE still cost a little more than develop, which leaves the index stale. |
Closes #2880
An inline
INDEXmadeINSERTabout 6 times slower, because the key function was compiled withnew Functionfor every row and every index.DELETE,UPDATEandTRUNCATE TABLEnever touched the index, so it kept deleted rows and old keys.src/65createindex.jshas the new helpers. The key function of an index is compiled once and cached by its code.INSERTinsrc/60createtable.jsadds the row with the cached function.DELETE,UPDATEandINSERT OR REPLACEmove only the changed rows. The changes are grouped by index key, and each bucket they touch is read once, so a large change costs no more than a rebuild, even on a column with few distinct values. A row whose key does not change keeps its place in the bucket.UPDATEsyncs the indexes in afinally, so a row that fails a check leaves the indexes matching the rows updated before it.TRUNCATE TABLErebuilds the regular indexes.inddefskeep their shape, sotest2607still sees the same definition forINDEXandCREATE INDEX.Timings in Node 22 with two indexes, median of two runs in ms:
a % 3: UPDATE of 40%a % 3: DELETE of 40%The UPDATE and DELETE rows cost a little more than
develop, which leaves the index stale.test/test2880.jscovers an inlineINDEXandCREATE INDEXafterDELETEandUPDATE,INSERT OR REPLACE,TRUNCATE TABLE, a parameter in theWHERE, an index on several columns with a prefix, anUPDATEandDELETEof most rows, a row that keeps its key, and anUPDATEthat fails on a later row. The index tests fail ondevelopand pass with the change.yarn test-onlygives 2560 pass. The only failures are the 5Test 418cases that load data from the internet, and they fail the same way ondevelopin my environment.