'does filtering queryset in django query database every time?
Imagine I have the following code:
qs = Users.objects.all()
list = []
for i in range(10):
list.append(qs.filter(age=i))
here the filter is called 10 times, does it connect to database 10 times or the first time filter is being used, the qs
is assigned (due to being lazy) and the filters are applied locally (total of one database connection)?
Solution 1:[1]
here the filter is called 10 times, does it connect to database 10 times or the first time filter is being used.
Django does not perform a query when you call .filter(…)
. You only construct a QuerySet
. It is only when you consume a QuerySet
, for example by iterating over it, subscripting, or calling len(…)
on the object, you make a query.
If you thus later in the process iterate over all querysets, for example:
for myqs in mylist:
for obj in myqs:
print(obj)
you will make ten separate queries, a query for each queryset that is evaluated.
If you iterate over the queryset, then Django will run the query on the database, and store the result, the list of records, in the QuerySet
. This thus means that if you iterate a second time over this queryset, it will not run the query a second time, but reuse the results cached in the QuerySet
.
Note that subscripting, i.e. myqueryset[0]
, will make a query to fetch a single object. If you do this a second time, it will again run the query on the database.
Certain technologies, like Haxl [GitHub] have been implemented to "batch queries", and in case one of the results is necessary, all queries are combined in the query to the database. But as far as I know, Django has no support for Haxl at the moment.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |