Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
perf(history): enable sql index on chat_id in history table
Browse files Browse the repository at this point in the history
This makes every query with a "WHERE history.chat_id" clause quicker,
improving history load time by 50% on my profile.

Related to #5812
  • Loading branch information
anthonybilinski committed Nov 26, 2019
1 parent af19c0d commit edd7290
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
24 changes: 23 additions & 1 deletion src/persistence/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "db/rawdatabase.h"

namespace {
static constexpr int SCHEMA_VERSION = 3;
static constexpr int SCHEMA_VERSION = 4;

bool createCurrentSchema(RawDatabase& db)
{
Expand Down Expand Up @@ -63,6 +63,9 @@ bool createCurrentSchema(RawDatabase& db)
"file_state INTEGER NOT NULL);"
"CREATE TABLE faux_offline_pending (id INTEGER PRIMARY KEY);"
"CREATE TABLE broken_messages (id INTEGER PRIMARY KEY);"));
// sqlite doesn't support including the index as part of the CREATE TABLE statement, so add a second query
queries += RawDatabase::Query(
"CREATE INDEX chat_id_idx on history (chat_id);");
queries += RawDatabase::Query(QStringLiteral("PRAGMA user_version = %1;").arg(SCHEMA_VERSION));
return db.execNow(queries);
}
Expand Down Expand Up @@ -178,6 +181,18 @@ bool dbSchema2to3(RawDatabase& db)
return db.execNow(upgradeQueries);
}

bool dbSchema3to4(RawDatabase& db)
{
QVector<RawDatabase::Query> upgradeQueries;
upgradeQueries += RawDatabase::Query{QString(
"CREATE INDEX chat_id_idx on history (chat_id);")};

upgradeQueries += RawDatabase::Query(QStringLiteral("PRAGMA user_version = 4;"));

return db.execNow(upgradeQueries);
}


/**
* @brief Upgrade the db schema
* @note On future alterations of the database all you have to do is bump the SCHEMA_VERSION
Expand Down Expand Up @@ -252,6 +267,13 @@ void dbSchemaUpgrade(std::shared_ptr<RawDatabase>& db)
return;
}
qDebug() << "Database upgraded incrementally to schema version 3";
case 3:
if (!dbSchema3to4(*db)) {
qCritical() << "Failed to upgrade db to schema version 4, aborting";
db.reset();
return;
}
qDebug() << "Database upgraded incrementally to schema version 4";
// etc.
default:
qInfo() << "Database upgrade finished (databaseSchemaVersion" << databaseSchemaVersion
Expand Down
27 changes: 25 additions & 2 deletions test/persistence/dbschema_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private slots:
void test0to1();
void test1to2();
void test2to3();
void test3to4();
void cleanupTestCase();
private:
bool initSucess{false};
Expand All @@ -50,9 +51,12 @@ const QString testFileList[] = {
"testIsNewDbFalse.db",
"test0to1.db",
"test1to2.db",
"test2to3.db"
"test2to3.db",
"test3to4.db"
};

// db schemas can be select with "SELECT name, sql FROM sqlite_master;" on the database.

const QMap<QString, QString> schema0 {
{"aliases", "CREATE TABLE aliases (id INTEGER PRIMARY KEY, owner INTEGER, display_name BLOB NOT NULL, UNIQUE(owner, display_name))"},
{"faux_offline_pending", "CREATE TABLE faux_offline_pending (id INTEGER PRIMARY KEY)"},
Expand Down Expand Up @@ -82,6 +86,17 @@ const QMap<QString, QString> schema2 {
// move stuck 0-length action messages to the existing "broken_messages" table. Not a real schema upgrade.
const auto schema3 = schema2;

// create index in history table on chat_id to improve query speed. Not a real schema upgrade.
const QMap<QString, QString> schema4 {
{"aliases", "CREATE TABLE aliases (id INTEGER PRIMARY KEY, owner INTEGER, display_name BLOB NOT NULL, UNIQUE(owner, display_name))"},
{"faux_offline_pending", "CREATE TABLE faux_offline_pending (id INTEGER PRIMARY KEY)"},
{"file_transfers", "CREATE TABLE file_transfers (id INTEGER PRIMARY KEY, chat_id INTEGER NOT NULL, file_restart_id BLOB NOT NULL, file_name BLOB NOT NULL, file_path BLOB NOT NULL, file_hash BLOB NOT NULL, file_size INTEGER NOT NULL, direction INTEGER NOT NULL, file_state INTEGER NOT NULL)"},
{"history", "CREATE TABLE history (id INTEGER PRIMARY KEY, timestamp INTEGER NOT NULL, chat_id INTEGER NOT NULL, sender_alias INTEGER NOT NULL, message BLOB NOT NULL, file_id INTEGER)"},
{"peers", "CREATE TABLE peers (id INTEGER PRIMARY KEY, public_key TEXT NOT NULL UNIQUE)"},
{"broken_messages", "CREATE TABLE broken_messages (id INTEGER PRIMARY KEY)"},
{"chat_id_idx", "CREATE INDEX chat_id_idx on history (chat_id)"}
};

void TestDbSchema::initTestCase()
{
for (const auto& path : testFileList) {
Expand Down Expand Up @@ -132,7 +147,7 @@ void TestDbSchema::testCreation()
QVector<RawDatabase::Query> queries;
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"testCreation.db", {}, {}}};
QVERIFY(createCurrentSchema(*db));
verifyDb(db, schema3);
verifyDb(db, schema4);
}

void TestDbSchema::testIsNewDb()
Expand Down Expand Up @@ -314,5 +329,13 @@ void TestDbSchema::test2to3()
verifyDb(db, schema3);
}

void TestDbSchema::test3to4()
{
auto db = std::shared_ptr<RawDatabase>{new RawDatabase{"test3to4.db", {}, {}}};
createSchemaAtVersion(db, schema3);
QVERIFY(dbSchema3to4(*db));
verifyDb(db, schema4);
}

QTEST_GUILESS_MAIN(TestDbSchema)
#include "dbschema_test.moc"

0 comments on commit edd7290

Please sign in to comment.