jazzband / django-authority

A Django app that provides generic per-object-permissions for Django's auth app and helpers to create custom permission checks.
http://django-authority.readthedocs.org
BSD 3-Clause "New" or "Revised" License
292 stars 57 forks source link

_get_user_cached_perms is doing a dependent sub-query #9

Closed jlward closed 11 years ago

jlward commented 11 years ago
        perms = Permission.objects.filter(
            Q(user__pk=self.user.pk) | Q(group__in=self.user.groups.all()),
        )

This is causing a dependent sub-query to be executed. Switch to doing something like:

        group_pks = set(self.user.groups.values_list(
            'pk',
            flat=True,
        ))
        perms = Permission.objects.filter(
            Q(user__pk=self.user.pk) | Q(group__pk__in=group_pks),
        )

It might be doing an extra query, however in the general case it will be faster to run the two small queries than the one that contains a dependent sub-query.

winhamwr commented 11 years ago

Looks good to me.