Skip to content

Improve performance of recent Ci::Runner scope

What does this MR do and why?

Describe in detail what your merge request does and why.

This MR optimizes the query behind the recent Ci::Runner scope so that it uses UNION ALL instead of OR. This allows making use of the index_ci_runners_on_id_desc_and_contacted_at_desc and index_ci_runners_on_created_at_and_id_desc indices.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

Database queries

Ci::Runner.recent

Previous query

SQL query
SELECT "ci_runners".*
FROM "ci_runners"
WHERE (ci_runners.created_at >= '2022-10-09 13:40:10.667767'
  OR ci_runners.contacted_at >= '2022-10-09 13:40:10.667767')
Query execution plan

https://postgres.ai/console/gitlab/gitlab-production-ci/sessions/14307/commands/50173

 Seq Scan on public.ci_runners  (cost=0.00..660129.96 rows=557228 width=282) (actual time=0.029..3331.239 rows=396589 loops=1)
   Filter: ((ci_runners.created_at >= '2022-10-09 13:40:10.667767'::timestamp without time zone) OR (ci_runners.contacted_at >= '2022-10-09 13:40:10.667767'::timestamp without time zone))
   Rows Removed by Filter: 1434475
   Buffers: shared hit=134303 read=23863 dirtied=2 written=2
   I/O Timings: read=2249.003 write=0.109

New query

SQL query
SELECT "ci_runners".*
FROM ((
    SELECT "ci_runners".*
    FROM "ci_runners"
    WHERE "ci_runners"."created_at" >= '2022-10-09 14:25:11.733405')
  UNION ALL (
    SELECT "ci_runners".*
    FROM "ci_runners"
    WHERE "ci_runners"."contacted_at" >= '2022-10-09 14:25:11.733492')) ci_runners
Query execution plan

https://postgres.ai/console/gitlab/gitlab-production-ci/sessions/14307/commands/50191

 Append  (cost=0.43..467231.24 rows=602903 width=282) (actual time=0.074..1132.169 rows=604613 loops=1)
   Buffers: shared hit=623268
   I/O Timings: read=0.000 write=0.000
   ->  Index Scan using index_ci_runners_on_created_at_and_id_desc on public.ci_runners  (cost=0.43..208649.26 rows=216817 width=282) (actual time=0.073..489.829 rows=217893 loops=1)
         Index Cond: (ci_runners.created_at >= '2022-10-09 14:25:11.733405'::timestamp without time zone)
         Buffers: shared hit=229799
         I/O Timings: read=0.000 write=0.000
   ->  Index Scan using index_ci_runners_on_contacted_at_desc_and_id_desc on public.ci_runners ci_runners_1  (cost=0.43..249538.44 rows=386086 width=282) (actual time=0.026..585.561 rows=386720 loops=1)
         Index Cond: (ci_runners_1.contacted_at >= '2022-10-09 14:25:11.733492'::timestamp without time zone)
         Buffers: shared hit=393469
         I/O Timings: read=0.000 write=0.000

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Pedro Pombeiro

Merge request reports