Skip to content

Adds indexes on service_desk_custom_email_verifications

Marc Saleiko requested to merge ms-sd-verification-indexes into master

What does this MR do and why?

For the Service Desk custom email feature, where we have email verification, we'll need a recurring task that closes all started and overdue verifications. It will use a query like:

# Something along these lines
ServiceDesk::CustomEmailVerification.started.overdue
# translates to something like
ServiceDesk::CustomEmailVerification.where(state: :started).where('triggered_at <= ?', 30.minutes.ago)

That produces a query like:

SELECT "service_desk_custom_email_verifications".* 
FROM "service_desk_custom_email_verifications" 
WHERE "service_desk_custom_email_verifications"."state" = 0 AND 
(triggered_at <= '2023-08-04 13:21:28.666463')

This scheduled job will run every few minutes.

This MR adds indexes to the state and triggered_at fields of service_desk_custom_email_verifications to lay out the foundation for the cleanup task.

Query plans

Because the feature is still behind a feature flag, there are now rows to select in postgres.ai.

Using example query from above 👆🏻

SELECT "service_desk_custom_email_verifications".* 
FROM "service_desk_custom_email_verifications" 
WHERE "service_desk_custom_email_verifications"."state" = 0 AND 
(triggered_at <= '2023-08-04 13:21:28.666463')

Plan without indexes

Seq Scan on service_desk_custom_email_verifications  (cost=0.00..49.00 rows=1 width=108)  
  Filter: ((triggered_at <= '2023-08-04 13:21:28.666463+00'::timestamp with time zone) AND (state = 0)) 

Plan with two indexes (a37c8568)

Index Scan using index_service_desk_custom_email_verifications_on_state on service_desk_custom_email_verifications  (cost=0.15..6.21 rows=1 width=108)  
  Index Cond: (state = 0)  
  Filter: (triggered_at <= '2023-08-04 13:21:28.666463+00'::timestamp with time zone)

Executed this statement before the plan:

CREATE INDEX index_service_desk_custom_email_verifications_on_state ON service_desk_custom_email_verifications (state);
CREATE INDEX index_service_desk_custom_email_verifications_on_triggered_at ON service_desk_custom_email_verifications (triggered_at);

Plan with one partial index (current version 2fa95847)

Index Scan using i_custom_email_verifications_on_triggered_at_and_state_started on service_desk_custom_email_verifications  (cost=0.12..3.14 rows=1 width=108)  
  Index Cond: (triggered_at <= '2023-08-04 13:21:28.666463+00'::timestamp with time zone)  

Executed this statement before the plan:

CREATE INDEX i_custom_email_verifications_on_triggered_at_and_state_started ON 
i_custom_email_verifications_on_triggered_at_and_state_started (triggered_at)
WHERE state = 0;

Screenshots or screen recordings

🚫 migration only

How to set up and validate locally

🚫 migration only

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 Marc Saleiko

Merge request reports