Add an index for the muc_log_line table
This immensely speeds up the archive select queries. fix #3292
This commit is contained in:
@@ -6,6 +6,8 @@
|
|||||||
#include <utils/get_first_non_empty.hpp>
|
#include <utils/get_first_non_empty.hpp>
|
||||||
#include <utils/time.hpp>
|
#include <utils/time.hpp>
|
||||||
|
|
||||||
|
#include <database/index.hpp>
|
||||||
|
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
|
|
||||||
sqlite3* Database::db;
|
sqlite3* Database::db;
|
||||||
@@ -42,6 +44,7 @@ void Database::open(const std::string& filename)
|
|||||||
Database::irc_channel_options.upgrade(Database::db);
|
Database::irc_channel_options.upgrade(Database::db);
|
||||||
Database::roster.create(Database::db);
|
Database::roster.create(Database::db);
|
||||||
Database::roster.upgrade(Database::db);
|
Database::roster.upgrade(Database::db);
|
||||||
|
create_index<Database::Owner, Database::IrcChanName, Database::IrcServerName>(Database::db, "archive_index", Database::muc_log_lines.get_name());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sqlite3.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
template <std::size_t N=0, typename... T>
|
||||||
|
typename std::enable_if<N == sizeof...(T), void>::type
|
||||||
|
add_column_name(std::string&)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
template <std::size_t N=0, typename... T>
|
||||||
|
typename std::enable_if<N < sizeof...(T), void>::type
|
||||||
|
add_column_name(std::string& out)
|
||||||
|
{
|
||||||
|
using ColumnType = typename std::remove_reference<decltype(std::get<N>(std::declval<std::tuple<T...>>()))>::type;
|
||||||
|
out += ColumnType::name;
|
||||||
|
if (N != sizeof...(T) - 1)
|
||||||
|
out += ",";
|
||||||
|
add_column_name<N+1, T...>(out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Columns>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user