Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite optimizations #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

sqlite optimizations #58

wants to merge 1 commit into from

Conversation

cogman
Copy link

@cogman cogman commented Dec 19, 2024

Creating new expiry key index as that's often used for lookups.

Adding "without rowid" to the table creation to reduce the size of this table on disk and improve lookup performance.

(Second PR. First one had to close as the primary key was broken with the prior PR).

Adding "without rowid" to the table creation to reduce the size of this table on disk and improve lookup performance.
@ColonelThirtyTwo
Copy link
Contributor

The index does not help either of the queries as-is. Session token lookup is uniquely determined by the ID and uses its index, so the expiry index isn't used. Expiration deletion queries over datetime(expiry_date), not expiry_date directly, so it can't use the index:

sqlite> CREATE TABLE test(id TEXT PRIMARY KEY NOT NULL, expire INTEGER NOT NULL);
sqlite> CREATE INDEX test_expire ON test(expire);
sqlite> EXPLAIN QUERY PLAN SELECT * FROM test WHERE id = ? AND expire < ?;
QUERY PLAN
`--SEARCH test USING INDEX sqlite_autoindex_test_1 (id=?)
sqlite> EXPLAIN QUERY PLAN SELECT * FROM test WHERE datetime(expire) < datetime('now');
QUERY PLAN
`--SCAN test

You could create the index over datetime(expiry_date):

sqlite> CREATE INDEX test_expire2 ON test(datetime(expire));
sqlite> EXPLAIN QUERY PLAN SELECT * FROM test WHERE datetime(expire) < datetime('now');
QUERY PLAN
`--SEARCH test USING INDEX test_expire2 (<expr><?)

But in practice I'm not sure if the cost of maintaining the index on inserts/updates is worth optimizing the infrequently-ran expiry cleaner.

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.

2 participants