Skip to content

Indexes are not used for WHERE on the first table, only for JOIN ... ON #2746

Open

Description

@mathiasrw

CREATE INDEX and PRIMARY KEY do not make a WHERE on the indexed column faster: the table is scanned in full either way.

alasql('CREATE TABLE ix (id INT PRIMARY KEY, email STRING, score INT)');
alasql('CREATE INDEX ix_email ON ix (email)');
alasql('CREATE TABLE nx (id INT, email STRING, score INT)');
// 200,000 rows into each
alasql("SELECT * FROM ix WHERE email = '[email protected]'");   // 10 to 24 ms
alasql("SELECT * FROM nx WHERE email = '[email protected]'");   // 10 to 15 ms
alasql('SELECT * FROM ix WHERE id = 9999');                   // 10 to 17 ms

Meanwhile the indexes make writes slower: a bulk INSERT ... SELECT of 25,000 rows takes 75 ms into a table with a primary key and two indexes, against 5 ms without them (benchmark dimension indexes, #2742).

Cause: src/38query.js uses table.indices only for a joined table (k > 0 && source.optimization == 'ix', built from the ON condition). The first table in FROM never looks at its indexes, and the WHERE is not used to pick one. #1312 asks how to add indexes to speed up queries; today they only help the right side of a JOIN ... ON.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions