Swap cd_rollout_environments environment_id index for composite

What does this MR do and why?

Swaps the index on cd_rollout_environments to support a new query pattern:

  1. Adds index_cd_rollout_environments_on_environment_state_finished_at on (environment_id, state, finished_at DESC, id DESC).
  2. Drops index_cd_rollout_environments_on_environment_id, which becomes redundant.

This supports the query added in !249670 (merged), which filters on state and orders by finished_at DESC, id DESC.

Database

Query this index is built for (from the MR linked above):

SELECT DISTINCT ON (environment_id) *
FROM cd_rollout_environments
WHERE state = 3 /* completed */
  AND environment_id IN (...)
ORDER BY environment_id, finished_at DESC, id DESC

The table is empty on GitLab.com, these plans are local.

Before (old single-column index, incremental sort needed):

 Unique  (cost=10.12..1000.52 rows=100 width=86) (actual time=0.521..1.382 rows=5 loops=1)
   Buffers: shared hit=6008
   ->  Incremental Sort  (cost=10.12..998.01 rows=1005 width=86) (actual time=0.521..1.356 rows=1000 loops=1)
         Sort Key: environment_id, finished_at DESC, id DESC
         Presorted Key: environment_id
         ->  Index Scan using index_cd_rollout_environments_on_environment_id on cd_rollout_environments  (cost=0.29..966.71 rows=1005 width=86) (actual time=0.305..1.269 rows=1000 loops=1)
               Index Cond: (environment_id = ANY ('{1,2,3,4,5,6,7,8,9,10}'::bigint[]))
               Filter: (state = 3)
               Rows Removed by Filter: 5000
 Planning Time: 0.603 ms
 Execution Time: 1.395 ms

After (new composite index, no sort or filter needed):

 Unique  (cost=0.41..659.42 rows=100 width=86) (actual time=0.011..0.483 rows=5 loops=1)
   Buffers: shared hit=1039
   ->  Index Scan using index_cd_rollout_environments_on_environment_state_finished_at on cd_rollout_environments  (cost=0.41..656.94 rows=993 width=86) (actual time=0.011..0.457 rows=1000 loops=1)
         Index Cond: ((environment_id = ANY ('{1,2,3,4,5,6,7,8,9,10}'::bigint[])) AND (state = 3))
 Planning Time: 0.539 ms
 Execution Time: 0.492 ms

References

https://gitlab.com/gitlab-org/gitlab/-/work_items/600769

MR acceptance checklist

Evaluated against the MR acceptance checklist.

Edited by Tiger Watson

Merge request reports

Loading
Loading