Skip to content

Optimize rolling 28 days snippets counter

Alper Akgun requested to merge 221184-rolling-28-day-snippets-optimization into master

What does this MR do?

Adds an index to optimize rolling 28 days snippets counter

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'
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'
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

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

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 Alper Akgun

Merge request reports