In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "detailed introduction of orm optimization in django". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the detailed introduction of orm optimization in django".
Orm optimization
1. Database technology optimization, including indexing fields, setting uniqueness constraints, etc.
two。 Query filtering is done in database statements, not in code (depending on the situation)
3. If you want to query the number of collections at one time, use the count function instead of the len function, but if you need to get to the collection later, use len, because count needs to do one more database operation
4. Avoid excessive use of count and exists functions
5. If you need to query the foreign key of an object, use the foreign key field instead of the primary key of the object that uses the associated foreign key
Example:
A.b_id # correct a.b.id # error
6. When querying through all statements, do not do cross-table queries, only query some data in the current table, otherwise the performance of the query statements will degrade a lot.
For example, there are foreign keys in table an and table b
A.b.all () # error
7. If you want to query the data of other tables, add select_related (ForeignKey field name, which is actually an active join table query, the performance will also degrade), if there are more than one, add it in parentheses
8. Adding the only parameter fetches only a field from the query result, while the other defer method excludes a field from the query result
9. Don't get what you don't need, you can do it through values and value_list
Values returns an array of dictionaries, for example: [{'key1': value1,' key2': value2}, {'key1': value3,' key2': value4}] value_list returns an array of tuple [('value1',' value2'), ('value3',' value4')] value_list+flat=True returns an array of ['value1',.]
10. If you want to know if there is at least one result, use exists instead of if QuerySet;, but if you need to use the previous QuerySet later, you can use if to determine
# Don't waste a query if you are using the querysetbooks = Book.objects.filter (..) if len (books) > 5: do_stuff_with_books (books) # If you aren't using the queryset use countbooks = Book.objects.filter (..) if books.count () > 5: do_some_stuff () # But neverif len (Book.objects.filter (..)) > 5: do_some_stuff ()
11. Using QuerySet.exists () or QuerySet.count () anywhere will result in additional queries
twelve。 Do not do indifferent sorting, sorting is not without cost, each sorted field is an operation that the database must perform.
13. If you want to insert multiple pieces of data, use bulk_create to insert in bulk, reducing the number of sql queries
14. Using with tags for cached QuerySet objects allows data to be cached and used
15. Use QuerySet.extra to explicitly indicate the fields to query
16. Batch updates and deletions use Queryset.update and delete functions, but update operations pay attention to object caching
17. Iterate over big data with QuerySet.Iterator
When you get a queryset, the django is cached and saved in memory, and there is nothing wrong with this cache if you need to loop the queryset many times, but if you only need to loop once, then you don't really need the cache, and you can use iterator for this use.
For example:
For book in Books.objects.all () .iterator (): do_stuff (book)
18. If you want to determine whether there is a foreign key, you only need to judge the id of the foreign key.
19. Instead of querying in the loop, take it out in advance and map it so that it can be obtained directly in the loop in the form of a dictionary.
20. When calculating a QuerySet, you can keep the cache if you need to do more than one loop, but if you use it only once, there is no need to use the cache
Python Optimization:
1. Sort as much as possible using .sort (), where key is more efficient than cmp
Thank you for reading, the above is the content of "detailed introduction of orm optimization in django". After the study of this article, I believe you have a deeper understanding of the detailed introduction of orm optimization in django, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.