From d9dda9f7c6014508732344f8892bc3339173a77a Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Thu, 7 Oct 2021 11:55:59 +0530 Subject: [PATCH] Add index on Jobs table (#18244) From Grafana charts, GetCountbyStatusandType and GetNewestJobByStatusesAndType were the two top queries. Overall, a through look into all job methods leads to the conclusion of 2 indexes - one on CreateAt, another a compound index of Status+Type. I have just gone ahead with the compound index for now. Once the job cleaner is implemented, I want to take a second look to decide whether to add the second index or not. Here is the before-after of the queries: Query 1: ``` explain analyze select count(*) from jobs where status='error' and type='migrations'; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=103.42..103.43 rows=1 width=8) (actual time=0.032..0.034 rows=1 loops=1) -> Bitmap Heap Scan on jobs (cost=4.54..103.42 rows=1 width=0) (actual time=0.027..0.028 rows=0 loops=1) Recheck Cond: ((type)::text = 'migrations'::text) Filter: ((status)::text = 'error'::text) -> Bitmap Index Scan on idx_jobs_type (cost=0.00..4.54 rows=34 width=0) (actual time=0.018..0.019 rows=0 loops=1) Index Cond: ((type)::text = 'migrations'::text) explain analyze select count(*) from jobs where status='error' and type='migrations'; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=8.31..8.32 rows=1 width=8) (actual time=0.079..0.080 rows=1 loops=1) -> Index Only Scan using jobs_multi on jobs (cost=0.29..8.30 rows=1 width=0) (actual time=0.072..0.073 rows=0 loops=1) Index Cond: ((status = 'error'::text) AND (type = 'migrations'::text)) Heap Fetches: 0 explain analyze select * from jobs where status='error' and type='migrations' order by createat desc limit 1; ``` Query 2: ``` QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------- Limit (cost=8.31..8.32 rows=1 width=187) (actual time=0.037..0.039 rows=0 loops=1) -> Sort (cost=8.31..8.32 rows=1 width=187) (actual time=0.035..0.036 rows=0 loops=1) Sort Key: createat DESC Sort Method: quicksort Memory: 25kB -> Index Scan using idx_jobs_type on jobs (cost=0.29..8.30 rows=1 width=187) (actual time=0.027..0.027 rows=0 loops=1) Index Cond: ((type)::text = 'migrations'::text) Filter: ((status)::text = 'error'::text) explain analyze select * from jobs where status='error' and type='migrations' order by createat desc limit 1; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------- Limit (cost=8.31..8.32 rows=1 width=187) (actual time=0.065..0.067 rows=0 loops=1) -> Sort (cost=8.31..8.32 rows=1 width=187) (actual time=0.063..0.064 rows=0 loops=1) Sort Key: createat DESC Sort Method: quicksort Memory: 25kB -> Index Scan using jobs_multi on jobs (cost=0.29..8.30 rows=1 width=187) (actual time=0.021..0.022 rows=0 loops=1) Index Cond: (((status)::text = 'error'::text) AND ((type)::text = 'migrations'::text)) ``` ```release-note NONE ``` --- store/sqlstore/job_store.go | 1 + store/sqlstore/upgrade.go | 1 + 2 files changed, 2 insertions(+) diff --git a/store/sqlstore/job_store.go b/store/sqlstore/job_store.go index 1aebd0153d..12ffad2654 100644 --- a/store/sqlstore/job_store.go +++ b/store/sqlstore/job_store.go @@ -37,6 +37,7 @@ func newSqlJobStore(sqlStore *SqlStore) store.JobStore { func (jss SqlJobStore) createIndexesIfNotExists() { jss.CreateIndexIfNotExists("idx_jobs_type", "Jobs", "Type") + jss.CreateCompositeIndexIfNotExists("idx_jobs_status_type", "Jobs", []string{"Status", "Type"}) } func (jss SqlJobStore) Save(job *model.Job) (*model.Job, error) { diff --git a/store/sqlstore/upgrade.go b/store/sqlstore/upgrade.go index 0d6232653e..6fd4c7c76e 100644 --- a/store/sqlstore/upgrade.go +++ b/store/sqlstore/upgrade.go @@ -1347,6 +1347,7 @@ func upgradeDatabaseToVersion610(sqlStore *SqlStore) { sqlStore.AlterColumnTypeIfExists("Sessions", "Roles", "text", "varchar(256)") sqlStore.AlterColumnTypeIfExists("ChannelMembers", "Roles", "text", "varchar(256)") sqlStore.AlterColumnTypeIfExists("TeamMembers", "Roles", "text", "varchar(256)") + sqlStore.CreateCompositeIndexIfNotExists("idx_jobs_status_type", "Jobs", []string{"Status", "Type"}) // saveSchemaVersion(sqlStore, Version610) // }