Skip to content

Change broadcast message index

Nicolas Dular requested to merge nicolasdular/change-broadcast-index into master

What does this MR do?

Adds the correct index for broadcast messages after a discussion.

Related to #38341 (closed)

I added 6000 broadcast messages (since indexes only get used after a certain threshold) to my local database to test if the index gets used, see the following results for before (sequence scan) and after (index scan).

Before

pry(main)> BroadcastMessage.where('ends_at > :now', now: Time.current).order_id_asc.explain
  BroadcastMessage Load (11.5ms)  SELECT "broadcast_messages".* FROM "broadcast_messages" WHERE (ends_at > '2020-01-29 15:28:07.750183') ORDER BY "broadcast_messages"."id" ASC
=> EXPLAIN for: SELECT "broadcast_messages".* FROM "broadcast_messages" WHERE (ends_at > '2020-01-29 15:28:07.750183') ORDER BY "broadcast_messages"."id" ASC
                                      QUERY PLAN
---------------------------------------------------------------------------------------
 Sort  (cost=120.13..120.99 rows=341 width=686)
   Sort Key: id
   ->  Seq Scan on broadcast_messages  (cost=0.00..105.79 rows=341 width=686)
         Filter: (ends_at > '2020-01-29 15:28:07.750183'::timestamp without time zone)
(4 rows)

After

pry(main)> BroadcastMessage.where('ends_at > :now', now: Time.current).order_id_asc.explain
  BroadcastMessage Load (88.6ms)  SELECT "broadcast_messages".* FROM "broadcast_messages" WHERE (ends_at > '2020-01-29 15:24:56.880528') ORDER BY "broadcast_messages"."id" ASC
=> EXPLAIN for: SELECT "broadcast_messages".* FROM "broadcast_messages" WHERE (ends_at > '2020-01-29 15:24:56.880528') ORDER BY "broadcast_messages"."id" ASC
                                               QUERY PLAN
---------------------------------------------------------------------------------------------------------
 Index Scan using broadcast_messages_pkey on broadcast_messages  (cost=0.28..214.10 rows=5818 width=591)
   Filter: (ends_at > '2020-01-29 15:24:56.880528'::timestamp without time zone)
(2 rows)

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
Edited by Mayra Cabrera

Merge request reports