Skip to content

Commit

Permalink
pkg/database: add indexes to the nars/narinfos tables (#68)
Browse files Browse the repository at this point in the history
Speed up the queries on the SQLite database by adding the necessary
indexes on the tables.
  • Loading branch information
kalbasit authored Dec 9, 2024
1 parent 46d830e commit aad18b6
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@ const (
// narInfosTable represents all the narinfo files that are available in the store.
// NOTE: Updating the structure here **will not** migrate the existing table!
narInfosTable = `
-- table
CREATE TABLE IF NOT EXISTS narinfos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hash TEXT NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP,
last_accessed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- indexes
CREATE UNIQUE INDEX IF NOT EXISTS idx_narinfos_id ON narinfos (id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_narinfos_hash ON narinfos (hash);
CREATE INDEX IF NOT EXISTS idx_narinfos_last_accessed_at ON narinfos (last_accessed_at);
`

// narsTable represents all the nar files that are available in the store.
// NOTE: Updating the structure here **will not** migrate the existing table!
narsTable = `
-- table
CREATE TABLE IF NOT EXISTS nars (
id INTEGER PRIMARY KEY AUTOINCREMENT,
narinfo_id INTEGER NOT NULL REFERENCES narinfos(id),
Expand All @@ -36,6 +43,12 @@ const (
updated_at TIMESTAMP,
last_accessed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- indexes
CREATE UNIQUE INDEX IF NOT EXISTS idx_nars_id ON nars (id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_nars_hash ON nars (hash);
CREATE INDEX IF NOT EXISTS idx_nars_narinfo_id ON nars (narinfo_id);
CREATE INDEX IF NOT EXISTS idx_nars_last_accessed_at ON nars (last_accessed_at);
`

getNarInfoQuery = `
Expand Down Expand Up @@ -97,6 +110,7 @@ const (
narTotalSizeQuery = `
SELECT SUM(file_size) as total_size FROM nars;
`

leastUsedNarsQuery = `
SELECT
id,
Expand Down

0 comments on commit aad18b6

Please sign in to comment.