Search code examples
djangoperformancefilterdjango-querysetlazy-evaluation

Filtering on objects.all() isn't behaving lazy in Django


Django querysets are supposed to be lazy but it seams as though the following is not.

I have the following (5 seconds) slow code (on 200,000 records):
1. qs = MyCars.objects.all()
2.
3. qs = qs.filter( car_id__gt=0 )

The following filtering is fast:
A. qs = MyCars.objects.filter( car_id__gt=0 )

What am I missing? (And for reasons I must include line 1).

To make it more interesting. If I on line 2 add:
2. qs2 = qs
And then on line 7:
7. qs2 = qs2.filter( car_id__gt=0 )
Then it is fast.
So, I thought I'll use qs2 to filter on line 3 but then it is slow.


Solution

  • Ok, so I had written if qs: on line 2. I changed it to if qs.count() > 0:. I didn't realize I had missed an importan portion of the code.