From 2c717d347d796a2b007331c42d78146e156eaea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sat, 26 Aug 2017 17:40:54 +0200 Subject: Add an index for the muc_log_line table This immensely speeds up the archive select queries. fix #3292 --- src/database/database.cpp | 3 +++ src/database/index.hpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/database/index.hpp (limited to 'src') diff --git a/src/database/database.cpp b/src/database/database.cpp index 0f2349d..f706528 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include sqlite3* Database::db; @@ -42,6 +44,7 @@ void Database::open(const std::string& filename) Database::irc_channel_options.upgrade(Database::db); Database::roster.create(Database::db); Database::roster.upgrade(Database::db); + create_index(Database::db, "archive_index", Database::muc_log_lines.get_name()); } diff --git a/src/database/index.hpp b/src/database/index.hpp new file mode 100644 index 0000000..5924779 --- /dev/null +++ b/src/database/index.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include + +#include +#include + +namespace +{ +template +typename std::enable_if::type +add_column_name(std::string&) +{ } + +template +typename std::enable_if::type +add_column_name(std::string& out) +{ + using ColumnType = typename std::remove_reference(std::declval>()))>::type; + out += ColumnType::name; + if (N != sizeof...(T) - 1) + out += ","; + add_column_name(out); +} +} + +template +void create_index(sqlite3* db, const std::string& name, const std::string& table) +{ + std::string res{"CREATE INDEX IF NOT EXISTS "}; + res += name + " ON " + table + "("; + add_column_name<0, Columns...>(res); + res += ")"; + + char* error; + const auto result = sqlite3_exec(db, res.data(), nullptr, nullptr, &error); + if (result != SQLITE_OK) + { + log_error("Error executing query: ", error); + sqlite3_free(error); + } +} -- cgit v1.2.3