简体   繁体   中英

Django QuerySet chaining, subset and caching

I know that QuerySets are lazy and they are evaluated only on certain conditions to avoid hitting the databases all the times.

What I don't know is if given a generic query set (retrieving all the items) and then using it to construct a more refined queryset (adding a filter for example) would lead to multiple sql queries or not?

Example:

all_items = MyModel.objects.all()
subset1 = all_items.filter(**some_conditions)
subset2 = subset1.filter(**other_condition)

1) Would this create 3 different sql queries? Or it all depends if the 3 variable are evaluated (for example iterating over them)?

2) Is this efficient or would it be better to fetch all the items, then convert them into a list and filter them in python?

1) If you enumerate only the final query set subset2 then only one database query request is executed, that is optimal.

2) Avoid premature optimization (before measurement on appropriate amount of data after most of application code is written.). You never know what will be finally the most important problem if the database gets bigger. Eg if you ask for a subset then the query is usually faster thanks to caching in the database. The amount of memory is in opposition to other optimizations. Maybe you can't hold later all data in the memory and users will access them only by a page of data. A clean readable code is more important for a later possible optimization than an optimization by 20% that must be removed later to can continue.

Other important paragraphs about (lazy) evaluation of queries:
When QuerySets are evaluated
QuerySets are lazy
Laziness in Django

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM