diff --git a/app/lib/admin/metrics/measure/tag_servers_measure.rb b/app/lib/admin/metrics/measure/tag_servers_measure.rb index 6eb46bca45..e0f1bf3440 100644 --- a/app/lib/admin/metrics/measure/tag_servers_measure.rb +++ b/app/lib/admin/metrics/measure/tag_servers_measure.rb @@ -28,14 +28,17 @@ class Admin::Metrics::Measure::TagServersMeasure < Admin::Metrics::Measure::Base def sql_query_string <<~SQL.squish SELECT axis.*, ( - SELECT count(distinct accounts.domain) AS value - FROM statuses - INNER JOIN statuses_tags ON statuses.id = statuses_tags.status_id - INNER JOIN accounts ON statuses.account_id = accounts.id - WHERE statuses_tags.tag_id = :tag_id - AND statuses.id BETWEEN :earliest_status_id AND :latest_status_id - AND date_trunc('day', statuses.created_at)::date = axis.period - ) + WITH tag_servers AS ( + SELECT DISTINCT accounts.domain + FROM statuses + INNER JOIN statuses_tags ON statuses.id = statuses_tags.status_id + INNER JOIN accounts ON statuses.account_id = accounts.id + WHERE statuses_tags.tag_id = :tag_id + AND statuses.id BETWEEN :earliest_status_id AND :latest_status_id + AND date_trunc('day', statuses.created_at)::date = axis.period + ) + SELECT COUNT(*) FROM tag_servers + ) AS value FROM ( SELECT generate_series(date_trunc('day', :start_at::timestamp)::date, date_trunc('day', :end_at::timestamp)::date, interval '1 day') AS period ) as axis