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/index.hpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/database/index.hpp (limited to 'src/database/index.hpp') 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