Add database indexes for hashed columns in user_credit_card_validations
What does this MR do and why?
- Pre-requisite for sub-task 2 of https://gitlab.com/gitlab-org/gitlab/-/issues/413525 and follows from discussion !131365 (comment 1590623278).
- Before using the hashed columns to compare credit-card records, we need to add the database indexes on the hashed columns.
- Methods
similar_holder_names_count
andused_by_banned_user?
will use the index onholder_name_hash
- Method
similar_records
will use the index on(expiration_date_hash, last_digits_hash, network_hash, credit_card_validated_at)
Database
- There are a total 160K records in the user_credit_card_validations table
- We currently have 2 existing indexes on the table -
-
(expiration_date, last_digits, network, credit_card_validated_at)
. To match this, an index on(expiration_date_hash, last_digits_hash, network_hash, credit_card_validated_at)
has been added. -
(lower(holder_name), expiration_date, last_digits, credit_card_validated_at)
. To match this, an index on only(holder_name_hash)
has been added.- Because the query plan for used_by_banned_user? with the full index on has similar performance to the query plan for used_by_banned_user? with a index only on
holder_name_hash
- query plan for similar_holder_names_count
- Because the query plan for used_by_banned_user? with the full index on has similar performance to the query plan for used_by_banned_user? with a index only on
-
Output of db:migrate
main: == 20231009104202 AddHolderNameHashIndexOnCreditCardValidations: migrating ====
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- view_exists?(:postgres_partitions)
main: -> 0.0013s
main: -- index_exists?(:user_credit_card_validations, :holder_name_hash, {:name=>"idx_user_credit_card_validations_on_holder_name_hash", :algorithm=>:concurrently})
main: -> 0.0040s
main: -- execute("SET statement_timeout TO 0")
main: -> 0.0006s
main: -- add_index(:user_credit_card_validations, :holder_name_hash, {:name=>"idx_user_credit_card_validations_on_holder_name_hash", :algorithm=>:concurrently})
main: -> 0.0018s
main: -- execute("RESET statement_timeout")
main: -> 0.0006s
main: == 20231009104202 AddHolderNameHashIndexOnCreditCardValidations: migrated (0.0240s)
main: == [advisory_lock_connection] object_id: 41184540, pg_backend_pid: 11045
main: == [advisory_lock_connection] object_id: 41234780, pg_backend_pid: 11050
main: == 20231009104325 AddPartialMatchIndexOfHashesOnCreditCardValidations: migrating
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- view_exists?(:postgres_partitions)
main: -> 0.0018s
main: -- index_exists?(:user_credit_card_validations, [:expiration_date_hash, :last_digits_hash, :network_hash, :credit_card_validated_at], {:name=>"idx_user_credit_card_validations_partial_match_of_hashes", :algorithm=>:concurrently})
main: -> 0.0053s
main: -- execute("SET statement_timeout TO 0")
main: -> 0.0006s
main: -- add_index(:user_credit_card_validations, [:expiration_date_hash, :last_digits_hash, :network_hash, :credit_card_validated_at], {:name=>"idx_user_credit_card_validations_partial_match_of_hashes", :algorithm=>:concurrently})
main: -> 0.0022s
main: -- execute("RESET statement_timeout")
main: -> 0.0006s
main: == 20231009104325 AddPartialMatchIndexOfHashesOnCreditCardValidations: migrated (0.0274s)
main: == [advisory_lock_connection] object_id: 41234780, pg_backend_pid: 11050
Output of db:rollback
main: == 20231009104325 AddPartialMatchIndexOfHashesOnCreditCardValidations: reverting
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- view_exists?(:postgres_partitions)
main: -> 0.0245s
main: -- indexes(:user_credit_card_validations)
main: -> 0.0055s
main: -- execute("SET statement_timeout TO 0")
main: -> 0.0006s
main: -- remove_index(:user_credit_card_validations, {:algorithm=>:concurrently, :name=>"idx_user_credit_card_validations_partial_match_of_hashes"})
main: -> 0.0014s
main: -- execute("RESET statement_timeout")
main: -> 0.0005s
main: == 20231009104325 AddPartialMatchIndexOfHashesOnCreditCardValidations: reverted (0.0578s)
main: == 20231009104202 AddHolderNameHashIndexOnCreditCardValidations: reverting ====
main: -- transaction_open?(nil)
main: -> 0.0000s
main: -- view_exists?(:postgres_partitions)
main: -> 0.0009s
main: -- indexes(:user_credit_card_validations)
main: -> 0.0032s
main: -- remove_index(:user_credit_card_validations, {:algorithm=>:concurrently, :name=>"idx_user_credit_card_validations_on_holder_name_hash"})
main: -> 0.0011s
main: == 20231009104202 AddHolderNameHashIndexOnCreditCardValidations: reverted (0.0193s)
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Related to #413525
Edited by Hinam Mehra