mirror of
https://github.com/ohwgiles/laminar.git
synced 2024-10-27 20:34:20 +00:00
Optimize query which is used on /jobs page
By replacing the primary key index and simplifying the query.
This commit is contained in:
parent
6a20291dc4
commit
458ec26943
@ -95,11 +95,31 @@ Laminar::Laminar(Server &server, Settings settings) :
|
|||||||
db = new Database((homePath/"laminar.sqlite").toString(true).cStr());
|
db = new Database((homePath/"laminar.sqlite").toString(true).cStr());
|
||||||
// Prepare database for first use
|
// Prepare database for first use
|
||||||
// TODO: error handling
|
// TODO: error handling
|
||||||
db->exec("CREATE TABLE IF NOT EXISTS builds("
|
const char *create_table_stmt =
|
||||||
"name TEXT, number INT UNSIGNED, node TEXT, queuedAt INT, "
|
"CREATE TABLE IF NOT EXISTS builds("
|
||||||
"startedAt INT, completedAt INT, result INT, output TEXT, "
|
"name TEXT, number INT UNSIGNED, node TEXT, queuedAt INT, "
|
||||||
"outputLen INT, parentJob TEXT, parentBuild INT, reason TEXT, "
|
"startedAt INT, completedAt INT, result INT, output TEXT, "
|
||||||
"PRIMARY KEY (name, number))");
|
"outputLen INT, parentJob TEXT, parentBuild INT, reason TEXT, "
|
||||||
|
"PRIMARY KEY (name, number DESC))";
|
||||||
|
db->exec(create_table_stmt);
|
||||||
|
|
||||||
|
// Migrate from (name, number) primary key to (name, number DESC).
|
||||||
|
// SQLite does not allow to alter primary key of existing table, so
|
||||||
|
// we have to create a new table.
|
||||||
|
db->stmt("SELECT sql LIKE '%, PRIMARY KEY (name, number))' "
|
||||||
|
"FROM sqlite_master WHERE type = 'table' AND name = 'builds'")
|
||||||
|
.fetch<int>([&](int has_old_index) {
|
||||||
|
if (has_old_index) {
|
||||||
|
LLOG(INFO, "Migrating table to the new primary key");
|
||||||
|
db->exec("BEGIN TRANSACTION");
|
||||||
|
db->exec("ALTER TABLE builds RENAME TO builds_old");
|
||||||
|
db->exec(create_table_stmt);
|
||||||
|
db->exec("INSERT INTO builds SELECT * FROM builds_old");
|
||||||
|
db->exec("DROP TABLE builds_old");
|
||||||
|
db->exec("COMMIT");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
db->exec("CREATE INDEX IF NOT EXISTS idx_completion_time ON builds("
|
db->exec("CREATE INDEX IF NOT EXISTS idx_completion_time ON builds("
|
||||||
"completedAt DESC)");
|
"completedAt DESC)");
|
||||||
|
|
||||||
@ -338,9 +358,8 @@ std::string Laminar::getStatus(MonitorScope scope) {
|
|||||||
j.set("description", desc == jobDescriptions.end() ? "" : desc->second);
|
j.set("description", desc == jobDescriptions.end() ? "" : desc->second);
|
||||||
} else if(scope.type == MonitorScope::ALL) {
|
} else if(scope.type == MonitorScope::ALL) {
|
||||||
j.startArray("jobs");
|
j.startArray("jobs");
|
||||||
db->stmt("SELECT name,number,startedAt,completedAt,result,reason FROM builds b "
|
db->stmt("SELECT name, number, startedAt, completedAt, result, reason "
|
||||||
"JOIN (SELECT name n,MAX(number) latest FROM builds WHERE result IS NOT NULL GROUP BY n) q "
|
"FROM builds GROUP BY name HAVING number = MAX(number)")
|
||||||
"ON b.name = q.n AND b.number = latest")
|
|
||||||
.fetch<str,uint,time_t,time_t,int,str>([&](str name,uint number, time_t started, time_t completed, int result, str reason){
|
.fetch<str,uint,time_t,time_t,int,str>([&](str name,uint number, time_t started, time_t completed, int result, str reason){
|
||||||
j.StartObject();
|
j.StartObject();
|
||||||
j.set("name", name);
|
j.set("name", name);
|
||||||
|
Loading…
Reference in New Issue
Block a user