Commit 1906d2f5 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'ab-60524-optimize-query' into 'master'

Optimize count query by using a limited count only

See merge request gitlab-org/gitlab-ee!12231
parents 63aebf44 6fc73bf3
......@@ -79,12 +79,13 @@ module EE
scope :mirror, -> { where(mirror: true) }
scope :mirrors_to_sync, ->(freeze_at) do
scope :mirrors_to_sync, ->(freeze_at, limit: nil) do
mirror
.joins_import_state
.where.not(import_state: { status: [:scheduled, :started] })
.where("import_state.next_execution_timestamp <= ?", freeze_at)
.where("import_state.retry_count <= ?", ::Gitlab::Mirror::MAX_RETRY)
.limit(limit)
end
scope :with_wiki_enabled, -> { with_feature_enabled(:wiki) }
......
---
title: Limit count to improve query performance
merge_request: 12231
author:
type: performance
......@@ -37,11 +37,11 @@ module Gitlab
return false if available_spots < capacity_threshold
# Only reschedule if we are able to completely fill up the available spots.
mirrors_ready_to_sync_count >= available_spots
mirrors_ready_to_sync_count(available_spots) >= available_spots
end
def mirrors_ready_to_sync_count
Project.mirrors_to_sync(Time.now).count
def mirrors_ready_to_sync_count(up_to = nil)
Project.mirrors_to_sync(Time.now, limit: up_to).count
end
def available_capacity
......
......@@ -191,6 +191,17 @@ describe Project do
expect(described_class.mirrors_to_sync(timestamp)).to be_empty
end
context 'when a limit is applied' do
before do
another_project = create(:project)
create(:import_state, :mirror, :finished, project: another_project)
end
it 'returns project if next_execution_timestamp is not in the future' do
expect(described_class.mirrors_to_sync(timestamp, limit: 1)).to match_array(project)
end
end
end
context 'when project is failed' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment