Skip to content

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

Open

Description

@mathiasrw

Since #2614 (in 4.21.0), an inline INDEX in CREATE TABLE builds a real index. Two problems with it:

  1. INSERT is about 6 times slower. Every inserted row runs new Function(...) once per index.
  2. DELETE with a WHERE and UPDATE never touch the index, so it keeps deleted rows and old keys.
// 100,000 rows with INSERT INTO t SELECT ... FROM ?
// CREATE TABLE t (a INT, b INT, c STRING)                                     52 ms
// CREATE TABLE t (a INT, b INT, c STRING, INDEX ia (a), INDEX ib (b), INDEX ic (c))  330 ms (4.20.0: 50 ms)

var db = new alasql.Database();
db.exec('CREATE TABLE t (a INT, b INT, INDEX ia (a)); INSERT INTO t VALUES (1,1),(2,2),(3,3)');
db.exec('DELETE FROM t WHERE a = 2');
db.exec('UPDATE t SET a = 10 WHERE a = 3');
db.tables.t.indices;
// still {"1": ..., "2": ..., "3": ...}: no 10, and 2 is still there

Results are still right only because nothing reads these indexes yet. SHOW INDEX reports the wrong sizes, and the memory of deleted rows is held. Every MySQL dump with inline INDEX pays the insert cost.

The fix: compile the index function once and keep it on the index (the same for CREATE INDEX), and keep the index in step on DELETE and UPDATE (src/statements/delete.js, src/statements/update.js), or do not fill an index that nothing reads.

Breaking?

No.

Activity

  1. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I'd like to work on this one.

  2. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I've opened a PR for this: #2987

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions