On Wed, Jul 13, 2011 at 4:37 PM, Cal Leeming [Simplicity Media Ltd] <
cal.leem...@simplicitymedialtd.co.uk> wrote:

(...)


> **
> *This original query took 4 seconds to complete:*
>
>         # return sessions for all subscriptions under this username
>         _users = Members.objects.filter(
>             username = 'testuser'
>         )
>         return Session.objects.filter(
>             member__in = _users,
>             is_fake = 0
>         ).order_by("-id")
>
> *SELECT* *COUNT*(*) *FROM* `ddcms_session` *WHERE* (`ddcms_session`.`is_fake`
> = 0 *AND* `ddcms_session`.`member_id` *IN* (*SELECT* 
> U0.`memberid`<http://dev.cp.dukedollars.com/paytools/lookingglass/120186270#>
>  *FROM* `members` U0 *WHERE*U0.`username` = testuser ))
>
> ID SELECT_TYPE TABLE TYPE POSSIBLE_KEYS KEY KEY_LEN REF ROWS EXTRA
> 1 PRIMARY ddcms_session index None member_id 281 None 397790 Using where;
> Using index
> 2 DEPENDENT SUBQUERY U0 unique_subquery PRIMARY,username PRIMARY 8 func 1 
> Using
> where
>
>

> *To speed this up, I had to change it to the following:*
>
>         _users = map(lambda x: x.get('memberid'), Members.objects.filter(
>             username = self.username
>         ).values('memberid'))
>

        # return sessions for all subscriptions under this username
>         return Session.objects.filter(
>             member__memberid__in = _users
>             is_fake = 0
>         ).order_by("-id")
>    *SELECT* 
> *COUNT*(*)<http://dev.cp.dukedollars.com/paytools/lookingglass/120186270#>
>  *FROM* `ddcms_session` *WHERE* (`ddcms_session`.`is_fake` = 0 *AND*
>  `ddcms_session`.`member_id` *IN* (120186270, 120235430, 120235431))
>
>
>  ID SELECT_TYPE TABLE TYPE POSSIBLE_KEYS KEY KEY_LEN REF ROWS EXTRA
>  1 SIMPLE ddcms_session range member_id member_id 4 None 187 Using where;
> Using index
>

So basically this means you'll write two different queries, whereas the
former example used one query + subquery? How long did these two queries
take combined? Less than the 4 seconds from the first code?


Cheers,
André

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to