From fe128723ae9b3483094a9a68693ce79fa411ba0a Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 7 Feb 2023 12:10:23 +0100 Subject: [PATCH] Create indices on entries table of log db This greatly improves performance when querying logs for a single thing or a single state/event/action. --- libnymea-core/logging/logengine.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libnymea-core/logging/logengine.cpp b/libnymea-core/logging/logengine.cpp index b30f18c2..b5d7ec4d 100644 --- a/libnymea-core/logging/logengine.cpp +++ b/libnymea-core/logging/logengine.cpp @@ -817,8 +817,17 @@ bool LogEngine::initDB(const QString &username, const QString &password) m_db.close(); return false; } + } - + m_db.exec("CREATE INDEX IF NOT EXISTS idx_query_single_thing ON entries (thingId);"); + if (m_db.lastError().isValid()) { + qCWarning(dcLogEngine()) << "Error creating entries table thing index in log database. Driver error:" << m_db.lastError().driverText() << "Database error:" << m_db.lastError().databaseText(); + return false; + } + m_db.exec("CREATE INDEX IF NOT EXISTS idx_query_single_type ON entries (typeId, thingId);"); + if (m_db.lastError().isValid()) { + qCWarning(dcLogEngine()) << "Error creating entries table state index in log database. Driver error:" << m_db.lastError().driverText() << "Database error:" << m_db.lastError().databaseText(); + return false; } qCDebug(dcLogEngine) << "Initialized logging DB successfully. (maximum DB size:" << m_dbMaxSize << ")";