]> git.ipfire.org Git - thirdparty/patchwork.git/commit
Fix slow Patch counting query
authorDaniel Axtens <dja@axtens.net>
Thu, 8 Mar 2018 01:28:22 +0000 (12:28 +1100)
committerDaniel Axtens <dja@axtens.net>
Tue, 20 Mar 2018 23:21:06 +0000 (10:21 +1100)
commitc97dad1cd5a289fa8aaea8feb3be1a7fb53417c0
treef1a5dfcc6b849ae60e287fc494669b59f8d415df
parent0f25d8a15788df5f5e8ed2521e71755da7ffe30b
Fix slow Patch counting query

Stephen Rothwell noticed (way back in September - sorry Stephen!) that
the following query is really slow on OzLabs:

SELECT COUNT(*) AS "__count" FROM "patchwork_patch"
    INNER JOIN "patchwork_submission" ON
        ("patchwork_patch"."submission_ptr_id" = "patchwork_submission"."id")
    WHERE ("patchwork_submission"."project_id" = 14 AND
           "patchwork_patch"."state_id" IN
       (SELECT U0."id" AS Col1 FROM "patchwork_state" U0
                WHERE U0."action_required" = true
ORDER BY U0."ordering" ASC));

I think this is really slow because we have to join the patch and
submission table to get the project id, which we need to filter the
patches.

Duplicate the project id in the patch table itself, which allows us to
avoid the JOIN.

The new query reads as:
SELECT COUNT(*) AS "__count" FROM "patchwork_patch"
    WHERE ("patchwork_patch"."patch_project_id" = 1 AND
           "patchwork_patch"."state_id" IN
       (SELECT U0."id" AS Col1 FROM "patchwork_state" U0
        WHERE U0."action_required" = true
ORDER BY U0."ordering" ASC));

Very simple testing on a small, artifical Postgres instance (3
projects, 102711 patches), shows speed gains of ~1.5-5x for this
query. Looking at Postgres' cost estimates (EXPLAIN) of the first
query vs the second query, we see a ~1.75x improvement there too.

I suspect the gains will be bigger on OzLabs.

Reported-by: Stephen Rothwell <sfr@canb.auug.org.au>
Signed-off-by: Daniel Axtens <dja@axtens.net>
patchwork/migrations/0024_patch_patch_project.py [new file with mode: 0644]
patchwork/models.py
patchwork/parser.py
patchwork/tests/utils.py
patchwork/views/__init__.py