Annotations: Change indices and rewrites annotation find query to improve database query performance (#21915)
Drop indices and create new ones and rewrites annotation find query to address performance issues when querying annotation table and there is a large amount of rows. Fixes #21902 Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> Co-authored-by: Kyle Brandt <kyle@kbrandt.com>
This commit is contained in:
co-authored by
Marcus Efraimsson
Kyle Brandt
parent
26d71c90f5
commit
5ae95190ed
@@ -144,46 +144,48 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I
|
||||
FROM annotation
|
||||
LEFT OUTER JOIN ` + dialect.Quote("user") + ` as usr on usr.id = annotation.user_id
|
||||
LEFT OUTER JOIN alert on alert.id = annotation.alert_id
|
||||
INNER JOIN (
|
||||
SELECT a.id from annotation a
|
||||
`)
|
||||
|
||||
sql.WriteString(`WHERE annotation.org_id = ?`)
|
||||
sql.WriteString(`WHERE a.org_id = ?`)
|
||||
params = append(params, query.OrgId)
|
||||
|
||||
if query.AnnotationId != 0 {
|
||||
// fmt.Print("annotation query")
|
||||
sql.WriteString(` AND annotation.id = ?`)
|
||||
sql.WriteString(` AND a.id = ?`)
|
||||
params = append(params, query.AnnotationId)
|
||||
}
|
||||
|
||||
if query.AlertId != 0 {
|
||||
sql.WriteString(` AND annotation.alert_id = ?`)
|
||||
sql.WriteString(` AND a.alert_id = ?`)
|
||||
params = append(params, query.AlertId)
|
||||
}
|
||||
|
||||
if query.DashboardId != 0 {
|
||||
sql.WriteString(` AND annotation.dashboard_id = ?`)
|
||||
sql.WriteString(` AND a.dashboard_id = ?`)
|
||||
params = append(params, query.DashboardId)
|
||||
}
|
||||
|
||||
if query.PanelId != 0 {
|
||||
sql.WriteString(` AND annotation.panel_id = ?`)
|
||||
sql.WriteString(` AND a.panel_id = ?`)
|
||||
params = append(params, query.PanelId)
|
||||
}
|
||||
|
||||
if query.UserId != 0 {
|
||||
sql.WriteString(` AND annotation.user_id = ?`)
|
||||
sql.WriteString(` AND a.user_id = ?`)
|
||||
params = append(params, query.UserId)
|
||||
}
|
||||
|
||||
if query.From > 0 && query.To > 0 {
|
||||
sql.WriteString(` AND annotation.epoch <= ? AND annotation.epoch_end >= ?`)
|
||||
sql.WriteString(` AND a.epoch <= ? AND a.epoch_end >= ?`)
|
||||
params = append(params, query.To, query.From)
|
||||
}
|
||||
|
||||
if query.Type == "alert" {
|
||||
sql.WriteString(` AND annotation.alert_id > 0`)
|
||||
sql.WriteString(` AND a.alert_id > 0`)
|
||||
} else if query.Type == "annotation" {
|
||||
sql.WriteString(` AND annotation.alert_id = 0`)
|
||||
sql.WriteString(` AND a.alert_id = 0`)
|
||||
}
|
||||
|
||||
if len(query.Tags) > 0 {
|
||||
@@ -204,7 +206,7 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I
|
||||
tagsSubQuery := fmt.Sprintf(`
|
||||
SELECT SUM(1) FROM annotation_tag at
|
||||
INNER JOIN tag on tag.id = at.tag_id
|
||||
WHERE at.annotation_id = annotation.id
|
||||
WHERE at.annotation_id = a.id
|
||||
AND (
|
||||
%s
|
||||
)
|
||||
@@ -223,7 +225,8 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I
|
||||
query.Limit = 100
|
||||
}
|
||||
|
||||
sql.WriteString(" ORDER BY epoch DESC" + dialect.Limit(query.Limit))
|
||||
// order of ORDER BY arguments match the order of a sql index for performance
|
||||
sql.WriteString(" ORDER BY a.org_id, a.epoch_end DESC, a.epoch DESC" + dialect.Limit(query.Limit) + " ) dt on dt.id = annotation.id")
|
||||
|
||||
items := make([]*annotations.ItemDTO, 0)
|
||||
|
||||
|
||||
@@ -123,7 +123,28 @@ func addAnnotationMig(mg *Migrator) {
|
||||
mg.AddMigration("Make epoch_end the same as epoch", NewRawSqlMigration("UPDATE annotation SET epoch_end = epoch"))
|
||||
mg.AddMigration("Move region to single row", &AddMakeRegionSingleRowMigration{})
|
||||
|
||||
// TODO! drop region_id column?
|
||||
//
|
||||
// 6.6.1: Optimize annotation queries
|
||||
//
|
||||
mg.AddMigration("Remove index org_id_epoch from annotation table", NewDropIndexMigration(table, &Index{
|
||||
Cols: []string{"org_id", "epoch"}, Type: IndexType,
|
||||
}))
|
||||
|
||||
mg.AddMigration("Remove index org_id_dashboard_id_panel_id_epoch from annotation table", NewDropIndexMigration(table, &Index{
|
||||
Cols: []string{"org_id", "dashboard_id", "panel_id", "epoch"}, Type: IndexType,
|
||||
}))
|
||||
|
||||
mg.AddMigration("Add index for org_id_dashboard_id_epoch_end_epoch on annotation table", NewAddIndexMigration(table, &Index{
|
||||
Cols: []string{"org_id", "dashboard_id", "epoch_end", "epoch"}, Type: IndexType,
|
||||
}))
|
||||
|
||||
mg.AddMigration("Add index for org_id_epoch_end_epoch on annotation table", NewAddIndexMigration(table, &Index{
|
||||
Cols: []string{"org_id", "epoch_end", "epoch"}, Type: IndexType,
|
||||
}))
|
||||
|
||||
mg.AddMigration("Remove index org_id_epoch_epoch_end from annotation table", NewDropIndexMigration(table, &Index{
|
||||
Cols: []string{"org_id", "epoch", "epoch_end"}, Type: IndexType,
|
||||
}))
|
||||
}
|
||||
|
||||
type AddMakeRegionSingleRowMigration struct {
|
||||
|
||||
Reference in New Issue
Block a user