Skip to content

Add index on merge_requests.state

What does this MR do?

Add index on merge_requests.state.

Example query - when visiting http://localhost:3000/gitlab-org/gitlab-test -:

SELECT COUNT(*) FROM "merge_requests" WHERE "merge_requests"."deleted_at" IS NULL AND "merge_requests"."target_project_id" = 1 AND ("merge_requests"."state" IN ('opened','reopened'));

Before the index - https://explain.depesz.com/s/EocWE:

                                                         QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=135085.87..135085.88 rows=1 width=8) (actual time=487.182..487.182 rows=1 loops=1)
   ->  Seq Scan on merge_requests  (cost=0.00..135085.87 rows=1 width=0) (actual time=0.320..487.174 rows=9 loops=1)
         Filter: ((deleted_at IS NULL) AND ((state)::text = ANY ('{opened,reopened}'::text[])) AND (target_project_id = 1))
         Rows Removed by Filter: 4006451
 Planning time: 0.315 ms
 Execution time: 487.221 ms
(6 rows)

After - https://explain.depesz.com/s/yl3q:

                                                                     QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=8.89..8.90 rows=1 width=8) (actual time=0.255..0.255 rows=1 loops=1)
   ->  Index Scan using index_merge_requests_on_state on merge_requests  (cost=0.43..8.89 rows=1 width=0) (actual time=0.203..0.251 rows=9 loops=1)
         Index Cond: ((state)::text = ANY ('{opened,reopened}'::text[]))
         Filter: ((deleted_at IS NULL) AND (target_project_id = 1))
         Rows Removed by Filter: 31
 Planning time: 0.431 ms
 Execution time: 0.358 ms

Are there points in the code the reviewer needs to double check?

Why was this MR needed?

Screenshots (if relevant)

Does this MR meet the acceptance criteria?

What are the relevant issue numbers?

Merge request reports

Loading