Most of you will probably read this and say "duh", so this is aimed more at people who haven't had experience with using large data sets in Django.
This isn't necessarily a bug or a problem with the ORM (although it would be nice if it could try and utilize little gotcha's like this), it just shows that sometimes adhering to the typical syntax / usage of the ORM, can be detrimental to performance. Cal * * *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 -- 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.