Skip to content

Improve ci_builds runner_id index

Adrien Kohlbecker requested to merge ak/add-index-on-builds into master

What does this MR do?

See #267374 (closed), specifically this thread

Fixes timeout when viewing runners in admin settings. Adding the id to the index helps with the query on this page. Explain with the following query: https://explain.depesz.com/s/XDxb

SELECT "ci_builds".* 
FROM "ci_builds" 
WHERE "ci_builds"."type" = 'Ci::Build' AND "ci_builds"."runner_id" = 1506021 
ORDER BY id DESC 
LIMIT 30

Before

Time: 40.759 s
  - planning: 0.481 ms
  - execution: 40.759 s
    - I/O read: 40.204 s
    - I/O write: 0.000 ms

Shared buffers:
  - hits: 40814 (~318.90 MiB) from the buffer pool
  - reads: 41120 (~321.30 MiB) from the OS file cache, including disk I/O
  - dirtied: 22114 (~172.80 MiB)
  - writes: 0

After

Time: 148.240 ms
  - planning: 0.470 ms
  - execution: 147.770 ms
    - I/O read: 29.221 ms
    - I/O write: 0.000 ms

Shared buffers:
  - hits: 31794 (~248.40 MiB) from the buffer pool
  - reads: 97 (~776.00 KiB) from the OS file cache, including disk I/O
  - dirtied: 0
  - writes: 0

Migration output

== 20201120140210 AddRunnerIdAndIdDescIndexToCiBuilds: migrating ==============
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:ci_builds, [:runner_id, :id], {:name=>"index_ci_builds_on_runner_id_and_id_desc", :order=>{:id=>:desc}, :algorithm=>:concurrently})
   -> 0.0106s
-- execute("SET statement_timeout TO 0")
   -> 0.0002s
-- add_index(:ci_builds, [:runner_id, :id], {:name=>"index_ci_builds_on_runner_id_and_id_desc", :order=>{:id=>:desc}, :algorithm=>:concurrently})
   -> 0.0044s
-- execute("RESET ALL")
   -> 0.0001s
-- transaction_open?()
   -> 0.0000s
-- indexes(:ci_builds)
   -> 0.0096s
-- remove_index(:ci_builds, {:algorithm=>:concurrently, :name=>"index_ci_builds_on_runner_id"})
   -> 0.0019s
== 20201120140210 AddRunnerIdAndIdDescIndexToCiBuilds: migrated (0.0276s) =====

Rollback output

== 20201120140210 AddRunnerIdAndIdDescIndexToCiBuilds: reverting ==============
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:ci_builds, [:runner_id], {:name=>"index_ci_builds_on_runner_id", :algorithm=>:concurrently})
   -> 0.0111s
-- execute("SET statement_timeout TO 0")
   -> 0.0001s
-- add_index(:ci_builds, [:runner_id], {:name=>"index_ci_builds_on_runner_id", :algorithm=>:concurrently})
   -> 0.0151s
-- execute("RESET ALL")
   -> 0.0002s
-- transaction_open?()
   -> 0.0000s
-- indexes(:ci_builds)
   -> 0.0100s
-- remove_index(:ci_builds, {:algorithm=>:concurrently, :name=>"index_ci_builds_on_runner_id_and_id_desc"})
   -> 0.0027s
== 20201120140210 AddRunnerIdAndIdDescIndexToCiBuilds: reverted (0.0401s) =====

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team
Edited by Adrien Kohlbecker

Merge request reports

Loading