gitlab-org--gitlab-foss/spec
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
..
bin
config
controllers Limit the number of pipelines to count 2018-05-17 13:52:59 +02:00
db/production Enable prometheus metrics by default 2018-05-07 08:46:23 +00:00
factories Backport cluster factory changes from EE 2018-05-13 12:36:51 +02:00
features Resolve "Opening Project with invite but without accepting leads to 404 error page" 2018-05-17 09:19:47 +00:00
finders show only groups an admin is a member of in dashboards/grops 2018-05-01 09:24:21 +00:00
fixtures Adjust board lists header text color 2018-05-14 19:05:20 -03:00
helpers Adjust spec to build correct path when storage path ends in slash 2018-05-09 12:56:03 +02:00
initializers Remove method call to deprecated method 2018-05-11 08:57:06 +02:00
javascripts Merge branch '45462-sha-object' into 'master' 2018-05-14 09:54:28 +00:00
lib Merge branch 'zj-workhorse-commit-patch-diff' into 'master' 2018-05-17 08:12:33 +00:00
mailers Resolve "Opening Project with invite but without accepting leads to 404 error page" 2018-05-17 09:19:47 +00:00
migrations Add a unique and not null constraint on the project_features.project_id column 2018-05-15 14:20:14 +02:00
models Resolve "Opening Project with invite but without accepting leads to 404 error page" 2018-05-17 09:19:47 +00:00
policies Enable update_(build|pipeline) for maintainers 2018-05-15 08:18:22 +00:00
presenters Only show push-to-master authorized users 2018-04-24 13:59:41 +02:00
requests Allow admin to assign shared runner to project through API 2018-05-16 10:52:28 +02:00
routing Add new repository archive route 2018-04-06 08:45:17 -04:00
rubocop/cop Revert the addition of goldiloader 2018-04-18 15:51:39 +02:00
serializers Enable update_(build|pipeline) for maintainers 2018-05-15 08:18:22 +00:00
services Merge branch 'ce-5968-extract-ee-specific-files-lines-for-clusters-related-files' into 'master' 2018-05-15 08:59:09 +00:00
sidekiq/cron
support Delete remote uploads 2018-05-16 08:58:07 +02:00
tasks Gitlab::Shell works on shard name, not path 2018-04-25 13:36:22 +02:00
unicorn
uploaders apply feedback 2018-05-09 19:57:08 +00:00
validators
views Project Sidebar: Split CI/CD into CI/CD and Operations 2018-05-16 11:16:33 +00:00
workers Merge branch '42099-port-push-mirroring-to-ce-ce-port-v-2' into 'master' 2018-05-07 15:15:57 +00:00
factories_spec.rb
fast_spec_helper.rb Improve fast specs helper to autoload the library 2018-05-11 13:33:10 +02:00
rails_helper.rb
rake_helper.rb
simplecov_env.rb
spec_helper.rb Support resetting of Prometheus metrics between test runs 2018-05-09 14:33:42 -07:00