Skip to content

Compile regular index keys once and keep the indexes in step on DELETE, UPDATE and TRUNCATE to close #2880 - #2987

Open
mateuscmtropical wants to merge 2 commits into
AlaSQL:developfrom
mateuscmtropical:fix-2880-index-sync
Open

mateuscmtropical wants to merge 2 commits into
AlaSQL:developfrom
mateuscmtropical:fix-2880-index-sync

Conversation

@mateuscmtropical

@mateuscmtropical mateuscmtropical commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2880

An inline INDEX made INSERT about 6 times slower, because the key function was compiled with new Function for every row and every index. DELETE, UPDATE and TRUNCATE TABLE never touched the index, so it kept deleted rows and old keys.

  • src/65createindex.js has the new helpers. The key function of an index is compiled once and cached by its code.
  • INSERT in src/60createtable.js adds the row with the cached function.
  • DELETE, UPDATE and INSERT OR REPLACE move 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.
  • UPDATE syncs the indexes in a finally, so a row that fails a check leaves the indexes matching the rows updated before it.
  • TRUNCATE TABLE rebuilds the regular indexes.
  • The stored inddefs keep their shape, so test2607 still sees the same definition for INDEX and CREATE INDEX.

Timings in Node 22 with two indexes, median of two runs in ms:

develop this PR
50k rows: INSERT 150 50
50k rows: 200 UPDATEs by primary key 40 55
50k rows: 200 DELETEs by primary key 400 415
100k rows, index on a % 3: UPDATE of 40% 16 65
100k rows, index on a % 3: DELETE of 40% 10 36

The UPDATE and DELETE rows cost a little more than develop, which leaves the index stale.

test/test2880.js covers an inline INDEX and CREATE INDEX after DELETE and UPDATE, INSERT OR REPLACE, TRUNCATE TABLE, a parameter in the WHERE, an index on several columns with a prefix, an UPDATE and DELETE of most rows, a row that keeps its key, and an UPDATE that fails on a later row. The index tests fail on develop and pass with the change. yarn test-only gives 2560 pass. The only failures are the 5 Test 418 cases that load data from the internet, and they fail the same way on develop in my environment.

@mathiasrw

Copy link
Copy Markdown
Member

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.

@mateuscmtropical

Copy link
Copy Markdown
Contributor Author

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:

develop previous commit now
50k rows: INSERT 150 55 50
50k rows: 200 UPDATEs by primary key 40 1600 55
50k rows: 200 DELETEs by primary key 400 1770 415
100k rows, index on a % 3: UPDATE of 40% 16 2270 65
100k rows, index on a % 3: DELETE of 40% 10 23 36

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.

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.

Inline INDEX in CREATE TABLE makes INSERT about 6 times slower, and DELETE and UPDATE leave the index stale

2 participants