More CI Queries to optimize identified from pg_stat_statements profiling

Some more CI queries to investigate from pg_stat_statements.

This one takes about 200ms and processes about 1GB of data:

https://performance.gitlab.net/dashboard/db/postgres-single-query-drill-down?orgId=1&var-environment=prd&var-queryid=780758609&var-fqdn=All&from=now-24h&to=now

SELECT count(*) as jobs, count(distinct commit_id) as pipelines, count(distinct project_id) as projects
FROM "public"."ci_builds"
WHERE "ci_builds"."type" IN (?)
  AND "ci_builds"."status" IN (?, ?)
  AND (ci_builds.commands LIKE ? OR ci_builds.commands LIKE ? OR ci_builds.commands LIKE ?)

And this one takes about 500ms but and processes about 4GB of data

https://performance.gitlab.net/dashboard/db/postgres-single-query-drill-down?orgId=1&var-environment=prd&var-queryid=506123960&var-fqdn=All&from=now-2d&to=now

SELECT
  projects.namespace_id,
  ci_builds.status,
  projects.shared_runners_enabled,
  (COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, ?) = ? OR
     COALESCE(namespace_statistics.shared_runners_seconds, ?) < COALESCE(namespaces.shared_runners_minutes_limit, application_settings.shared_runners_minutes, ?) * ?) as has_minutes,
  COUNT(*) AS count
FROM ci_builds
JOIN projects
  ON projects.id = ci_builds.project_id
JOIN namespaces
  ON namespaces.id = projects.namespace_id
LEFT JOIN namespace_statistics
  ON namespace_statistics.namespace_id = namespaces.id
JOIN application_settings
  ON application_settings.id = ?
WHERE ci_builds.type = ?
  AND ci_builds.status IN (?, ?)
  AND projects.pending_delete = ?
GROUP BY
  projects.namespace_id,
  ci_builds.status,
  projects.shared_runners_enabled,
  namespaces.shared_runners_minutes_limit,
  namespace_statistics.shared_runners_seconds,
  application_settings.shared_runners_minutes
Edited Sep 24, 2025 by 🤖 GitLab Bot 🤖
Assignee Loading
Time tracking Loading