gitlab-org--gitlab-foss/app
Yorick Peterse 70985aa19b
Limit the number of pipelines to count
When displaying the project pipelines dashboard we display a few tabs
for different pipeline states. For every such tab we count the number of
pipelines that belong to it. For large projects such as GitLab CE this
means having to count over 80 000 rows, which can easily take between 70
and 100 milliseconds per query.

To improve this we apply a technique we already use for search results:
we limit the number of rows to count. The current limit is 1000, which
means that if more than 1000 rows are present for a state we will show
"1000+" instead of the exact number. The SQL queries used for this
perform much better than a regular COUNT, even when a project has a lot
of pipelines.

Prior to these changes we would end up running a query like this:

    SELECT COUNT(*)
    FROM ci_pipelines
    WHERE project_id = 13083
    AND status IN ('success', 'failed', 'canceled')

This would produce a plan along the lines of the following:

    Aggregate  (cost=3147.55..3147.56 rows=1 width=8) (actual time=501.413..501.413 rows=1 loops=1)
      Buffers: shared hit=17116 read=861 dirtied=2
      ->  Index Only Scan using index_ci_pipelines_on_project_id_and_ref_and_status_and_id on ci_pipelines  (cost=0.56..2984.14 rows=65364 width=0) (actual time=0.095..490.263 rows=80388 loops=1)
            Index Cond: (project_id = 13083)
            Filter: ((status)::text = ANY ('{success,failed,canceled}'::text[]))
            Rows Removed by Filter: 2894
            Heap Fetches: 353
            Buffers: shared hit=17116 read=861 dirtied=2
    Planning time: 1.409 ms
    Execution time: 501.519 ms

Using the LIMIT count technique we instead run the following query:

    SELECT COUNT(*)
    FROM (
        SELECT 1
        FROM ci_pipelines
        WHERE project_id = 13083
        AND status IN ('success', 'failed', 'canceled')
        LIMIT 1001
    ) for_count

This query produces the following plan:

    Aggregate  (cost=58.77..58.78 rows=1 width=8) (actual time=1.726..1.727 rows=1 loops=1)
      Buffers: shared hit=169 read=15
      ->  Limit  (cost=0.56..46.25 rows=1001 width=4) (actual time=0.164..1.570 rows=1001 loops=1)
            Buffers: shared hit=169 read=15
            ->  Index Only Scan using index_ci_pipelines_on_project_id_and_ref_and_status_and_id on ci_pipelines  (cost=0.56..2984.14 rows=65364 width=4) (actual time=0.162..1.426 rows=1001 loops=1)
                  Index Cond: (project_id = 13083)
                  Filter: ((status)::text = ANY ('{success,failed,canceled}'::text[]))
                  Rows Removed by Filter: 9
                  Heap Fetches: 10
                  Buffers: shared hit=169 read=15
    Planning time: 1.832 ms
    Execution time: 1.821 ms

While this query still uses a Filter for the "status" field the number
of rows that it may end up filtering (at most 1001) is small enough that
an additional index does not appear to be necessary at this time.

See https://gitlab.com/gitlab-org/gitlab-ce/issues/43132#note_68659234
for more information.
2018-05-17 13:52:59 +02:00
..
assets Merge branch 'remove-css-imports' into 'master' 2018-05-17 07:24:50 +00:00
controllers Limit the number of pipelines to count 2018-05-17 13:52:59 +02:00
finders show only groups an admin is a member of in dashboards/grops 2018-05-01 09:24:21 +00:00
helpers Merge branch 'sh-fast-admin-counts' into 'master' 2018-05-16 17:31:48 +00:00
mailers Backport 5480-epic-notifications from EE 2018-05-07 12:58:47 -06:00
models Resolve "Opening Project with invite but without accepting leads to 404 error page" 2018-05-17 09:19:47 +00:00
policies Refactor out duplication in runner_policy.rb 2018-05-16 11:42:09 +02:00
presenters Respect permissions when showing Failed Jobs 2018-05-06 18:46:00 +02:00
serializers Merge branch '43557-osw-present-merge-sha-commit' into 'master' 2018-05-07 22:17:55 +00:00
services Include project.full_path when moving repo 2018-05-10 16:24:57 +10:00
uploaders Resolve "Avatar URLs are wrong when using a CDN path and Object Storage" 2018-04-23 16:59:53 +00:00
validators Rename allow_private_networks to allow_local_network 2018-04-02 17:24:19 +02:00
views Resolve "Opening Project with invite but without accepting leads to 404 error page" 2018-05-17 09:19:47 +00:00
workers remove the `Upload` redefinition in `MigrateUploadsWorker` 2018-05-08 10:04:52 -04:00