Skip to content

Add index to resource_milestone_events

What does this MR do?

We will need to query events based on their created_at time stamps for burn up charts.

Needed for:

Relevant call

ResourceMilestoneEvent.where(issue_id: milestone.issue_ids).where(created_at: start_time..end_time).order(:created_at)

to_sql:

EXPLAIN ANALYZE
SELECT
    "resource_milestone_events".*
FROM
    "resource_milestone_events"
WHERE
    "resource_milestone_events"."issue_id" IN (505, 482, 504)
    AND "resource_milestone_events"."created_at" BETWEEN '2020-02-11 23:00:00'
    AND '2020-03-02 23:00:00'
ORDER BY
    "resource_milestone_events"."created_at" ASC;

Records count in resource_milestone_events: 1017.

Without index

Sort  (cost=30.21..30.23 rows=9 width=52) (actual time=0.462..0.464 rows=7 loops=1)
  Sort Key: created_at
  Sort Method: quicksort  Memory: 25kB
  ->  Seq Scan on resource_milestone_events  (cost=0.00..30.07 rows=9 width=52) (actual time=0.024..0.442 rows=7 loops=1)
        Filter: ((created_at >= '2020-02-11 23:00:00+01'::timestamp with time zone) AND (created_at <= '2020-03-02 23:00:00+01'::timestamp with time zone) AND (issue_id = ANY ('{505,482,504}'::bigint[])))
        Rows Removed by Filter: 1010
Planning time: 0.378 ms
Execution time: 0.508 ms

https://explain.depesz.com/s/U4je

With index

Sort  (cost=16.40..16.42 rows=9 width=52) (actual time=0.035..0.036 rows=7 loops=1)
  Sort Key: created_at
  Sort Method: quicksort  Memory: 25kB
  ->  Bitmap Heap Scan on resource_milestone_events  (cost=4.42..16.26 rows=9 width=52) (actual time=0.018..0.023 rows=7 loops=1)
        Recheck Cond: ((created_at >= '2020-02-11 23:00:00+01'::timestamp with time zone) AND (created_at <= '2020-03-02 23:00:00+01'::timestamp with time zone))
        Filter: (issue_id = ANY ('{505,482,504}'::bigint[]))
        Rows Removed by Filter: 6
        Heap Blocks: exact=1
        ->  Bitmap Index Scan on index_resource_milestone_events_created_at  (cost=0.00..4.42 rows=14 width=0) (actual time=0.009..0.009 rows=13 loops=1)
              Index Cond: ((created_at >= '2020-02-11 23:00:00+01'::timestamp with time zone) AND (created_at <= '2020-03-02 23:00:00+01'::timestamp with time zone))
Planning time: 0.253 ms
Execution time: 0.076 ms

https://explain.depesz.com/s/a6xE

Migration outputs

Up

> bundle exec rails db:migrate
== 20200406135648 AddIndexToCreatedAtOnResourceMilestoneEvents: migrating =====
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:resource_milestone_events, :created_at, {:name=>"index_resource_milestone_events_created_at", :algorithm=>:concurrently})
   -> 0.0037s
-- execute("SET statement_timeout TO 0")
   -> 0.0003s
-- add_index(:resource_milestone_events, :created_at, {:name=>"index_resource_milestone_events_created_at", :algorithm=>:concurrently})
   -> 0.0088s
-- execute("RESET ALL")
   -> 0.0002s
== 20200406135648 AddIndexToCreatedAtOnResourceMilestoneEvents: migrated (0.0132s)

Down

> bundle exec rails db:migrate:down VERSION=20200406135648
== 20200406135648 AddIndexToCreatedAtOnResourceMilestoneEvents: reverting =====
-- transaction_open?()
   -> 0.0000s
-- indexes(:resource_milestone_events)
   -> 0.0030s
-- execute("SET statement_timeout TO 0")
   -> 0.0002s
-- remove_index(:resource_milestone_events, {:algorithm=>:concurrently, :name=>"index_resource_milestone_events_created_at"})
   -> 0.0112s
-- execute("RESET ALL")
   -> 0.0002s
== 20200406135648 AddIndexToCreatedAtOnResourceMilestoneEvents: reverted (0.0148s) 

Screenshots

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • [-] Label as security and @ mention @gitlab-com/gl-security/appsec
  • [-] The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • [-] Security reports checked/validated by a reviewer from the AppSec team

Related to #38096 (closed)

Edited by 🤖 GitLab Bot 🤖

Merge request reports