gitlab-org--gitlab-foss/app
Toon Claes 1c481b7aac Enhance performance of counting local Uploads
Add an index to the `store` column on `uploads`. This makes counting
local uploads faster.

Also, there is no longer need to check for objects with `store = NULL`.
See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/18557

---

### Query plans

Query:

```sql
SELECT COUNT(*)
FROM "uploads"
WHERE ("uploads"."store" = ? OR "uploads"."store" IS NULL)
```

#### Without index

```
gitlabhq_production=# EXPLAIN ANALYZE SELECT uploads.* FROM uploads WHERE (uploads.store = 1 OR uploads.store IS NULL);
                                                  QUERY PLAN
---------------------------------------------------------------------------------------------------------------
 Seq Scan on uploads  (cost=0.00..601729.54 rows=578 width=272) (actual time=6.170..2308.256 rows=545 loops=1)
   Filter: ((store = 1) OR (store IS NULL))
   Rows Removed by Filter: 4411957
 Planning time: 38.652 ms
 Execution time: 2308.454 ms
(5 rows)
```

#### Add index

```
gitlabhq_production=# create index uploads_tmp1 on uploads (store);
CREATE INDEX
```

#### With index

```
gitlabhq_production=# EXPLAIN ANALYZE SELECT uploads.* FROM uploads WHERE (uploads.store = 1 OR uploads.store IS NULL);
                                                          QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on uploads  (cost=11.46..1238.88 rows=574 width=272) (actual time=0.155..0.577 rows=545 loops=1)
   Recheck Cond: ((store = 1) OR (store IS NULL))
   Heap Blocks: exact=217
   ->  BitmapOr  (cost=11.46..11.46 rows=574 width=0) (actual time=0.116..0.116 rows=0 loops=1)
         ->  Bitmap Index Scan on uploads_tmp1  (cost=0.00..8.74 rows=574 width=0) (actual time=0.095..0.095 rows=545 loops=1)
               Index Cond: (store = 1)
         ->  Bitmap Index Scan on uploads_tmp1  (cost=0.00..2.44 rows=1 width=0) (actual time=0.020..0.020 rows=0 loops=1)
               Index Cond: (store IS NULL)
 Planning time: 0.274 ms
 Execution time: 0.637 ms
(10 rows)
```

Closes https://gitlab.com/gitlab-org/gitlab-ee/issues/6070
2018-11-07 11:29:31 +01:00
..
assets Merge branch 'winh-delayed-jobs-dynamic-timer' into 'master' 2018-11-07 10:27:54 +00:00
controllers Merge branch 'ce-7472-epics-autocomplete' into 'master' 2018-11-07 08:22:40 +00:00
finders Merge branch 'use_tokenauthenticatable_method_for_token_lookup' into 'master' 2018-11-05 13:28:16 +00:00
graphql Make GitLab pages support access control 2018-10-05 13:41:11 +00:00
helpers Merge branch 'gt-use-merge-request-prefix-in-event-feed-title' into 'master' 2018-11-07 06:45:31 +00:00
mailers Add email for milestone change 2018-11-02 16:29:32 +00:00
models Enhance performance of counting local Uploads 2018-11-07 11:29:31 +01:00
policies Allow to make builds soft-archived. 2018-11-05 15:51:57 +01:00
presenters Allow to make builds soft-archived. 2018-11-05 15:51:57 +01:00
serializers Extract code into IssueBoardEntity 2018-11-06 10:33:55 -06:00
services Merge branch 'ccr/51052_keep_labels_on_issue' into 'master' 2018-11-06 16:31:32 +00:00
uploaders Backport of "Use Geo log to remove files when migrated to object storage" 2018-10-03 15:09:16 +00:00
validators Allow UrlValidator to work with attr_encrypted 2018-09-17 19:34:40 +01:00
views Merge branch '52771-ldap-users-can-t-choose-private-or-internal-when-creating-a-new-group' into 'master' 2018-11-07 09:54:19 +00:00
workers Use fire_state_event 2018-11-06 11:45:39 +09:00