mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
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
```