Optimize rolling 28 days snippets counter
What does this MR do?
Adds an index to optimize rolling 28 days snippets counter
- Main issue #221184 (closed)
- Main MR !34363 (merged)
Optimization
CREATE INDEX index_snippets_on_id_and_created_at ON public.snippets USING btree (id, created_at);
Queries
SELECT MIN("snippets"."id") FROM "snippets" WHERE "snippets"."created_at" BETWEEN '2020-05-22 09:12:11.847078' AND '2020-06-19 09:12:11.847153'
- Before https://explain.depesz.com/s/TZZh 343msecs warm, 10-40 seconds cold: Needs optimization for
id
,created_at
columns - After https://explain.depesz.com/s/Rj3r 27msec
SELECT MAX("snippets"."id") FROM "snippets" WHERE "snippets"."created_at" BETWEEN '2020-05-22 09:12:11.847078' AND '2020-06-19 09:12:11.847153'
- Before < 1 ms https://explain.depesz.com/s/hvct Warm
- After https://explain.depesz.com/s/pMI4 Still fine
SELECT COUNT("snippets"."id") FROM "snippets" WHERE "snippets"."created_at" BETWEEN '2020-05-22 09:12:11.847078' AND '2020-06-19 09:12:11.847153' AND "snippets"."id" BETWEEN 1900000 AND 1999999
- Before https://explain.depesz.com/s/qhBU 145msecs
- After https://explain.depesz.com/s/DqGY 48msecs using index only scans for index_snippets_on_id_and_created_at
Migration output
$ rails db:migrate:down VERSION=20200618105638
== 20200618105638 AddIndexOnIdAndCreatedAtToSnippets: reverting ===============
-- transaction_open?()
-> 0.0000s
-- index_exists?(:snippets, [:id, :created_at], {:algorithm=>:concurrently})
-> 0.0042s
-- remove_index(:snippets, {:algorithm=>:concurrently, :column=>[:id, :created_at]})
-> 0.0087s
== 20200618105638 AddIndexOnIdAndCreatedAtToSnippets: reverted (0.0133s) ======
$ rails db:migrate:up VERSION=20200618105638
== 20200618105638 AddIndexOnIdAndCreatedAtToSnippets: migrating ===============
-- transaction_open?()
-> 0.0000s
-- index_exists?(:snippets, [:id, :created_at], {:algorithm=>:concurrently})
-> 0.0042s
-- add_index(:snippets, [:id, :created_at], {:algorithm=>:concurrently})
-> 0.0152s
== 20200618105638 AddIndexOnIdAndCreatedAtToSnippets: migrated (0.0196s) ======
Does this MR meet the acceptance criteria?
Conformity
-
Changelog entry -
Documentation (if required) -
Code review guidelines -
Merge request performance guidelines -
Style guides -
Database guides -
Separation of EE specific content
Availability and Testing
-
Review and add/update tests for this feature/bug. Consider all test levels. See the Test Planning Process. -
Tested in all supported browsers -
Informed Infrastructure department of a default or new setting change, if applicable per definition of done
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 Alper Akgun