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

Resolve high CPU usage when performing DB reads #419

Merged
merged 10 commits into from
Mar 24, 2019
15 changes: 14 additions & 1 deletion src/itemdb.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct Item {
final class ItemDatabase
{
// increment this for every change in the db schema
immutable int itemDatabaseVersion = 7;
immutable int itemDatabaseVersion = 8;

Database db;
string insertItemStmt;
Expand Down Expand Up @@ -59,9 +59,21 @@ final class ItemDatabase
db.exec("DROP TABLE item");
createTable();
}
// Set the enforcement of foreign key constraints.
// https://www.sqlite.org/pragma.html#pragma_foreign_keys
db.exec("PRAGMA foreign_keys = ON");
// Set the recursive trigger capability
// https://www.sqlite.org/pragma.html#pragma_recursive_triggers
db.exec("PRAGMA recursive_triggers = ON");
// Set the journal mode for databases associated with the current connection
// https://www.sqlite.org/pragma.html#pragma_journal_mode
db.exec("PRAGMA journal_mode = WAL");
// Automatic indexing is enabled by default as of version 3.7.17
// https://www.sqlite.org/pragma.html#pragma_automatic_index
db.exec("PRAGMA automatic_index = OFF");
// Tell SQLite to store temporary tables in memory. This will speed up many read operations that rely on temporary tables, indices, and views.
// https://www.sqlite.org/pragma.html#pragma_temp_store
db.exec("PRAGMA temp_store = MEMORY");

insertItemStmt = "
INSERT OR REPLACE INTO item (driveId, id, name, type, eTag, cTag, mtime, parentId, crc32Hash, sha1Hash, quickXorHash, remoteDriveId, remoteId)
Expand Down Expand Up @@ -106,6 +118,7 @@ final class ItemDatabase
)");
db.exec("CREATE INDEX name_idx ON item (name)");
db.exec("CREATE INDEX remote_idx ON item (remoteDriveId, remoteId)");
db.exec("CREATE INDEX item_children ON item (driveId, parentId)");
db.setVersion(itemDatabaseVersion);
}

Expand Down