Skip to content

Add indices for protected_environments and protected_environment_approval_rules

What does this MR do and why?

This merge request adds a new index for the following two tables:

  • protected_environments: on required_approval_count and created_at
  • protected_environment_approval_rules: on required_approvals and created_at

The indices are added to ensure metrics introduced in !90089 (merged) avoid sequential scans when querying the tables (check this and this if you need to learn more).

Queries Before & After

Please check the links from database-lab below for queries before/after the changes introduced here for verification:

Before:

After:

Resolves #366559 (closed).

Migration & Rollback Output

Migration

main: == 20220708142744 AddCompositeIndexForProtectedEnvironments: migrating ========
main: -- transaction_open?()
main:    -> 0.0000s
main: -- index_exists?(:protected_environments, [:required_approval_count, :created_at], {:name=>"index_protected_environments_on_approval_count_and_created_at", :algorithm=>:concurrently})
main:    -> 0.0077s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0005s
main: -- add_index(:protected_environments, [:required_approval_count, :created_at], {:name=>"index_protected_environments_on_approval_count_and_created_at", :algorithm=>:concurrently})
main:    -> 0.0020s
main: -- execute("RESET statement_timeout")
main:    -> 0.0005s
main: == 20220708142744 AddCompositeIndexForProtectedEnvironments: migrated (0.0202s)

main: == 20220708142803 AddCompositeIndexForProtectedEnvironmentApprovalRules: migrating 
main: -- transaction_open?()
main:    -> 0.0000s
main: -- index_exists?(:protected_environment_approval_rules, [:required_approvals, :created_at], {:name=>"index_pe_approval_rules_on_required_approvals_and_created_at", :algorithm=>:concurrently})
main:    -> 0.0041s
main: -- add_index(:protected_environment_approval_rules, [:required_approvals, :created_at], {:name=>"index_pe_approval_rules_on_required_approvals_and_created_at", :algorithm=>:concurrently})
main:    -> 0.0013s
main: == 20220708142803 AddCompositeIndexForProtectedEnvironmentApprovalRules: migrated (0.0093s) 

Rollback

main: == 20220708142803 AddCompositeIndexForProtectedEnvironmentApprovalRules: reverting 
main: -- transaction_open?()
main:    -> 0.0000s
main: -- index_exists?(:protected_environment_approval_rules, [:required_approvals, :created_at], {:name=>"index_pe_approval_rules_on_required_approvals_and_created_at", :algorithm=>:concurrently})
main:    -> 0.0223s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0006s
main: -- remove_index(:protected_environment_approval_rules, {:name=>"index_pe_approval_rules_on_required_approvals_and_created_at", :algorithm=>:concurrently, :column=>[:required_approvals, :created_at]})
main:    -> 0.0071s
main: -- execute("RESET statement_timeout")
main:    -> 0.0006s
main: == 20220708142803 AddCompositeIndexForProtectedEnvironmentApprovalRules: reverted (0.0430s) 

main: == 20220708142744 AddCompositeIndexForProtectedEnvironments: reverting ========
main: -- transaction_open?()
main:    -> 0.0000s
main: -- index_exists?(:protected_environments, [:required_approval_count, :created_at], {:name=>"index_protected_environments_on_approval_count_and_created_at", :algorithm=>:concurrently})
main:    -> 0.0088s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0004s
main: -- remove_index(:protected_environments, {:name=>"index_protected_environments_on_approval_count_and_created_at", :algorithm=>:concurrently, :column=>[:required_approval_count, :created_at]})
main:    -> 0.0060s
main: -- execute("RESET statement_timeout")
main:    -> 0.0004s
main: == 20220708142744 AddCompositeIndexForProtectedEnvironments: reverted (0.0259s) 

For more details, please check out the db:check-migrations job.

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 Ahmed Hemdan

Merge request reports