Skip to content

Add a index for vulnerability_state_transitions table

What does this MR do and why?

Describe in detail what your merge request does and why.

This Merge Request adds an index for the vulnerability_id and id on the vulnerability_state_transitions table.

This index would be beneficial for us because:

  1. We will most likely want to present the state transition from the latest one to the first one (ORDER BY created_at DESC)
  2. The same index would be used for queries like SELECT * FROM vulnerability_state_transitions WHERE vulnerability_in (1,2,3,...,n);

More context can be found in this comment.

The original suggestion of using the created_at was replaced by the id because of this comment and this guideline.

This Merge Request is related to issues #363049 (closed) and #325700 (closed).

Migration Up

main: == 20220530170915 AddIndexForVulnerabilityStateTransition: migrating ==========
main: -- transaction_open?()
main:    -> 0.0000s
main: -- index_exists?(:vulnerability_state_transitions, [:vulnerability_id, :id], {:name=>"index_vulnerability_state_transitions_id_and_vulnerability_id", :algorithm=>:concurrently})
main:    -> 0.0086s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0004s
main: -- add_index(:vulnerability_state_transitions, [:vulnerability_id, :id], {:name=>"index_vulnerability_state_transitions_id_and_vulnerability_id", :algorithm=>:concurrently})
main:    -> 0.0032s
main: -- execute("RESET statement_timeout")
main:    -> 0.0004s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- indexes(:vulnerability_state_transitions)
main:    -> 0.0031s
main: -- remove_index(:vulnerability_state_transitions, {:algorithm=>:concurrently, :name=>"index_vulnerability_state_transitions_on_vulnerability_id"})
main:    -> 0.0032s
main: == 20220530170915 AddIndexForVulnerabilityStateTransition: migrated (0.0309s) =

Migration Down

main: == 20220530170915 AddIndexForVulnerabilityStateTransition: reverting ==========
main: -- transaction_open?()
main:    -> 0.0000s
main: -- indexes(:vulnerability_state_transitions)
main:    -> 0.0052s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0003s
main: -- remove_index(:vulnerability_state_transitions, {:algorithm=>:concurrently, :name=>"index_vulnerability_state_transitions_id_and_vulnerability_id"})
main:    -> 0.0028s
main: -- execute("RESET statement_timeout")
main:    -> 0.0004s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- index_exists?(:vulnerability_state_transitions, [:vulnerability_id], {:name=>"index_vulnerability_state_transitions_on_vulnerability_id", :algorithm=>:concurrently})
main:    -> 0.0013s
main: -- add_index(:vulnerability_state_transitions, [:vulnerability_id], {:name=>"index_vulnerability_state_transitions_on_vulnerability_id", :algorithm=>:concurrently})
main:    -> 0.0020s
main: == 20220530170915 AddIndexForVulnerabilityStateTransition: reverted (0.0222s) =

Screenshots or screen recordings

These are strongly recommended to assist reviewers and reduce the time to merge your change.

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

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 Marcos Rocha

Merge request reports

Loading