Prefetching tags leads to 1 extra join #275

Merged
merged 1 commit into from Apr 26, 2015

Projects

None yet

2 participants

@coagulant
Contributor

Before (2 joins):

SELECT DISTINCT ("taggit_taggeditem"."object_id") AS "_prefetch_related_val", "taggit_tag"."id", "taggit_tag"."name", "taggit_tag"."slug" 
FROM "taggit_tag" INNER JOIN "taggit_taggeditem" ON ( "taggit_tag"."id" = "taggit_taggeditem"."tag_id" )
INNER JOIN "taggit_taggeditem" T4 ON ( "taggit_tag"."id" = T4."tag_id" ) 
WHERE ("taggit_taggeditem"."content_type_id" = %s AND T4."object_id" IN (%s))

After (1 join):

SELECT DISTINCT ("taggit_taggeditem"."object_id") AS "_prefetch_related_val", "taggit_tag"."id", "taggit_tag"."name", "taggit_tag"."slug" 
FROM "taggit_tag" INNER JOIN "taggit_taggeditem" ON ( "taggit_tag"."id" = "taggit_taggeditem"."tag_id" )
WHERE ("taggit_taggeditem"."content_type_id" = %s AND "taggit_taggeditem"."object_id" IN (%s))

Avoid using ORM private api (sticky_filter) and use one filter call instead of 2 to reduce number of joins.

Above query (1) was kinda slow with large number of tagged items on our setup (postgres).

@coagulant
Contributor

Hey! This PR has tests ;) Could anyone review and merge this if changes are ok?

@frewsxcv
Collaborator
frewsxcv commented Mar 4, 2015

There are merge conflicts that need to be resolved

@coagulant coagulant Remove 1 extra prefetch join
Avoid using orm private api (sticky_filter) and
use one filter call instead of 2
6f237a8
@coagulant
Contributor

I've rebased against current develop, all tests are green now :)

@frewsxcv frewsxcv merged commit bae6a32 into alex:develop Apr 26, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@frewsxcv
Collaborator

Thanks for this @coagulant!

@frewsxcv
Collaborator

I just released 0.14.0 that includes this change

https://pypi.python.org/pypi/django-taggit/0.14.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment