I have quite a large database table (1M+ rows) that experiences issues when filtering results in Django.
Currently the filter logic is the following:
results = Result.objects.filter(date__range=(date_from, date_to))
for result in results:
# do stuff
On some periods it causes a crash (probably due to memory exhaustion)
I am wondering, would it be more efficient replacing it with the following:
results = Result.objects.all().order_by('-id')
for result in results:
if result.date > date_to:
continue
if result.date < date_from:
break
# do stuff
In theory, since .all() creates a lazy QuerySet, it might perform better than range filtering in database with 1M+ rows. Also would be interesting to know about memory consumption in both cases.
Maybe there is another solution how to do it more efficiently and in constant memory?
Thanks!