Skip to content

Create index on `security_findings`(`scan_id`, `id`)

What does this MR do and why?

This index will be used by the keyset paginator to iterate through security_findings records filtered by scanner_id which times out without this index(#357422 (closed)).

Extracted from Mark stale `security_scans` as `purged` (!82711 - merged) as introducing the index will be faster than merging the entire MR which will also be useful to fix other parts of the software.

Database review

Important Note: The following DDL change runs on a huge table with almost 1B records.

This index will be utilized by queires like this
SELECT
    "security_findings".*
FROM
    "security_findings"
WHERE
    "security_findings"."scan_id" BETWEEN 1 AND 10000
    AND (("security_findings"."scan_id",
            "security_findings"."id") > (1020,
            21852))
ORDER BY
    "security_findings"."scan_id" ASC,
    "security_findings"."id" ASC
LIMIT 10
rake db:migrate:up
== 20220311010352 CreateScanIdAndIdIndexOnSecurityFindings: migrating =========
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:security_findings, [:scan_id, :id], {:name=>"index_security_findings_on_scan_id_and_id", :algorithm=>:concurrently})
   -> 0.0058s
-- execute("SET statement_timeout TO 0")
   -> 0.0004s
-- add_index(:security_findings, [:scan_id, :id], {:name=>"index_security_findings_on_scan_id_and_id", :algorithm=>:concurrently})
   -> 0.0063s
-- execute("RESET statement_timeout")
   -> 0.0004s
== 20220311010352 CreateScanIdAndIdIndexOnSecurityFindings: migrated (0.0224s)
rake db:migrate:down
== 20220311010352 CreateScanIdAndIdIndexOnSecurityFindings: reverting =========
-- transaction_open?()
   -> 0.0000s
-- indexes(:security_findings)
   -> 0.0051s
-- execute("SET statement_timeout TO 0")
   -> 0.0005s
-- remove_index(:security_findings, {:algorithm=>:concurrently, :name=>"index_security_findings_on_scan_id_and_id"})
   -> 0.0044s
-- execute("RESET statement_timeout")
   -> 0.0004s
== 20220311010352 CreateScanIdAndIdIndexOnSecurityFindings: reverted (0.0188s)

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 Mehmet Emin INAC

Merge request reports