Skip to content

Add index on resource_weight_events for created_at

What does this MR do?

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

Needed for:

SQL

explain analyze
  select 'milestone' as "event_type", rme.created_at, rme.milestone_id as "value", rme.action, rme.issue_id from resource_milestone_events rme where issue_id in (505, 482, 504) and created_at <= '2020-03-19 23:00:00'
union
  select 'weight' as "event_type", rwe.created_at, rwe.weight as "value", null as "action", rwe.issue_id from resource_weight_events rwe where issue_id in (505, 482, 504) and created_at <= '2020-03-19 23:00:00'
order by created_at;

Count of records for resource_weight_events in the test setup: 1060

Without index

Sort  (cost=45.51..45.63 rows=51 width=58) (actual time=0.793..0.796 rows=11 loops=1)
  Sort Key: rme.created_at
  Sort Method: quicksort  Memory: 25kB
  ->  HashAggregate  (cost=43.55..44.06 rows=51 width=58) (actual time=0.709..0.722 rows=11 loops=1)
        Group Key: ('milestone'::text), rme.created_at, rme.milestone_id, rme.action, rme.issue_id
        ->  Append  (cost=4.41..42.91 rows=51 width=58) (actual time=0.050..0.674 rows=11 loops=1)
              ->  Bitmap Heap Scan on resource_milestone_events rme  (cost=4.41..16.08 rows=11 width=58) (actual time=0.049..0.063 rows=7 loops=1)
                    Recheck Cond: (created_at <= '2020-03-19 23:00:00+01'::timestamp with time zone)
                    Filter: (issue_id = ANY ('{505,482,504}'::bigint[]))
                    Rows Removed by Filter: 8
                    Heap Blocks: exact=1
                    ->  Bitmap Index Scan on index_resource_milestone_events_created_at  (cost=0.00..4.40 rows=17 width=0) (actual time=0.027..0.027 rows=15 loops=1)
                          Index Cond: (created_at <= '2020-03-19 23:00:00+01'::timestamp with time zone)
              ->  Subquery Scan on "*SELECT* 2"  (cost=0.00..26.73 rows=40 width=58) (actual time=0.040..0.605 rows=4 loops=1)
                    ->  Seq Scan on resource_weight_events rwe  (cost=0.00..26.23 rows=40 width=54) (actual time=0.039..0.600 rows=4 loops=1)
                          Filter: ((created_at <= '2020-03-19 23:00:00+01'::timestamp with time zone) AND (issue_id = ANY ('{505,482,504}'::bigint[])))
                          Rows Removed by Filter: 1056
Planning time: 0.697 ms
Execution time: 0.971 ms

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

With index

Sort  (cost=34.02..34.15 rows=51 width=58) (actual time=0.049..0.049 rows=11 loops=1)
  Sort Key: rme.created_at
  Sort Method: quicksort  Memory: 25kB
  ->  HashAggregate  (cost=32.06..32.57 rows=51 width=58) (actual time=0.038..0.040 rows=11 loops=1)
        Group Key: ('milestone'::text), rme.created_at, rme.milestone_id, rme.action, rme.issue_id
        ->  Append  (cost=4.41..31.42 rows=51 width=58) (actual time=0.012..0.032 rows=11 loops=1)
              ->  Bitmap Heap Scan on resource_milestone_events rme  (cost=4.41..16.08 rows=11 width=58) (actual time=0.012..0.014 rows=7 loops=1)
                    Recheck Cond: (created_at <= '2020-03-19 23:00:00+01'::timestamp with time zone)
                    Filter: (issue_id = ANY ('{505,482,504}'::bigint[]))
                    Rows Removed by Filter: 8
                    Heap Blocks: exact=1
                    ->  Bitmap Index Scan on index_resource_milestone_events_created_at  (cost=0.00..4.40 rows=17 width=0) (actual time=0.005..0.005 rows=15 loops=1)
                          Index Cond: (created_at <= '2020-03-19 23:00:00+01'::timestamp with time zone)
              ->  Subquery Scan on "*SELECT* 2"  (cost=4.74..15.24 rows=40 width=58) (actual time=0.013..0.017 rows=4 loops=1)
                    ->  Bitmap Heap Scan on resource_weight_events rwe  (cost=4.74..14.74 rows=40 width=54) (actual time=0.013..0.016 rows=4 loops=1)
                          Recheck Cond: (created_at <= '2020-03-19 23:00:00+01'::timestamp with time zone)
                          Filter: (issue_id = ANY ('{505,482,504}'::bigint[]))
                          Rows Removed by Filter: 55
                          Heap Blocks: exact=1
                          ->  Bitmap Index Scan on index_resource_weight_events_created_at  (cost=0.00..4.73 rows=61 width=0) (actual time=0.007..0.007 rows=59 loops=1)
                                Index Cond: (created_at <= '2020-03-19 23:00:00+01'::timestamp with time zone)
Planning time: 0.449 ms
Execution time: 0.097 ms

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

Migration outputs

Up

> bundle exec rails db:migrate
== 20200406141452 AddIndexToIssueIdAndCreatedAtOnResourceWeightEvents: migrating 
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:resource_weight_events, [:issue_id, :created_at], {:name=>"index_resource_weight_events_on_issue_id_and_created_at", :algorithm=>:concurrently})
   -> 0.0026s
-- execute("SET statement_timeout TO 0")
   -> 0.0002s
-- add_index(:resource_weight_events, [:issue_id, :created_at], {:name=>"index_resource_weight_events_on_issue_id_and_created_at", :algorithm=>:concurrently})
   -> 0.0172s
-- execute("RESET ALL")
   -> 0.0004s
== 20200406141452 AddIndexToIssueIdAndCreatedAtOnResourceWeightEvents: migrated (0.0206s) 

Down

> bundle exec rails db:migrate:down VERSION=20200406141452
== 20200406141452 AddIndexToIssueIdAndCreatedAtOnResourceWeightEvents: reverting 
-- transaction_open?()
   -> 0.0000s
-- indexes(:resource_weight_events)
   -> 0.0028s
-- execute("SET statement_timeout TO 0")
   -> 0.0001s
-- remove_index(:resource_weight_events, {:algorithm=>:concurrently, :name=>"index_resource_weight_events_on_issue_id_and_created_at"})
   -> 0.0038s
-- execute("RESET ALL")
   -> 0.0002s
== 20200406141452 AddIndexToIssueIdAndCreatedAtOnResourceWeightEvents: reverted (0.0070s) 

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

#38096 (closed)

Edited by Patrick Derichs

Merge request reports