Improve speed of sqlite queries using indexes and a trigger to find out the section of a post

This commit is contained in:
Jan-Lukas Else 2020-09-20 21:23:37 +02:00
parent 3f3ffb4bec
commit 8ef592cb22
2 changed files with 29 additions and 1 deletions

View File

@ -45,6 +45,34 @@ func migrateDb() error {
return err
},
},
&migrator.Migration{
Name: "00006",
Func: func(tx *sql.Tx) error {
_, err := tx.Exec("create index pp_path_index on post_parameters (path);")
return err
},
},
&migrator.Migration{
Name: "00007",
Func: func(tx *sql.Tx) error {
_, err := tx.Exec("alter table posts add column section text; create trigger add_section after update on posts begin update posts set section = (select substr(path, 2, len) from (select path, instr(substr(path, 2),'/')-1 as len from (select new.path as path))) where path = new.path; end;")
return err
},
},
&migrator.Migration{
Name: "00008",
Func: func(tx *sql.Tx) error {
_, err := tx.Exec("create index p_section_index on posts (section);")
return err
},
},
&migrator.Migration{
Name: "00009",
Func: func(tx *sql.Tx) error {
_, err := tx.Exec("create trigger add_section_insert after insert on posts begin update posts set section = (select substr(path, 2, len) from (select path, instr(substr(path, 2),'/')-1 as len from (select new.path as path))) where path = new.path; end;")
return err
},
},
),
)
if err != nil {

View File

@ -194,7 +194,7 @@ func getPosts(context context.Context, config *postsRequestConfig) (posts []*Pos
if i > 0 {
postsTable += " or"
}
postsTable += " path like '/" + section.Name + "/%'"
postsTable += " section='" + section.Name + "'"
}
postsTable += ")"
}