From 6d9db92a07af8648cf3c56c47b24fbbcf322d0e2 Mon Sep 17 00:00:00 2001 From: 17billion <17earlgrey@gmail.com> Date: Thu, 1 Jun 2023 21:31:03 +0900 Subject: [PATCH] Dashboards: Improve delete dashboard performance due to slow annotations query (#68544) * Add index on dashboard_id column in annotation table * Added org_id condition to the delete query for dashboard deletion. --- pkg/services/dashboards/database/database.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/services/dashboards/database/database.go b/pkg/services/dashboards/database/database.go index 4b3b9cb10f0..28874d01085 100644 --- a/pkg/services/dashboards/database/database.go +++ b/pkg/services/dashboards/database/database.go @@ -705,7 +705,6 @@ func (d *dashboardStore) deleteDashboard(cmd *dashboards.DeleteDashboardCommand, "DELETE FROM dashboard WHERE id = ?", "DELETE FROM playlist_item WHERE type = 'dashboard_by_id' AND value = ?", "DELETE FROM dashboard_version WHERE dashboard_id = ?", - "DELETE FROM annotation WHERE dashboard_id = ?", "DELETE FROM dashboard_provisioning WHERE dashboard_id = ?", "DELETE FROM dashboard_acl WHERE dashboard_id = ?", } @@ -737,6 +736,11 @@ func (d *dashboardStore) deleteDashboard(cmd *dashboards.DeleteDashboardCommand, return err } + _, err = sess.Exec("DELETE FROM annotation WHERE dashboard_id = ? AND org_id = ?", dashboard.ID, dashboard.OrgID) + if err != nil { + return err + } + for _, sql := range deletes { _, err := sess.Exec(sql, dashboard.ID) if err != nil { @@ -780,10 +784,15 @@ func (d *dashboardStore) deleteChildrenDashboardAssociations(sess *db.Session, d "DELETE FROM dashboard_tag WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)", "DELETE FROM star WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)", "DELETE FROM dashboard_version WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)", - "DELETE FROM annotation WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)", "DELETE FROM dashboard_provisioning WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)", "DELETE FROM dashboard_acl WHERE dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)", } + + _, err = sess.Exec("DELETE FROM annotation WHERE org_id = ? AND dashboard_id IN (SELECT id FROM dashboard WHERE org_id = ? AND folder_id = ?)", dashboard.OrgID, dashboard.OrgID, dashboard.ID) + if err != nil { + return err + } + for _, sql := range childrenDeletes { _, err := sess.Exec(sql, dashboard.OrgID, dashboard.ID) if err != nil {