首页 > 其他 > 详细

django queryset的一些技术

时间:2020-03-12 12:45:22      阅读:73      评论:0      收藏:0      [点我收藏+]

一、queryset.count() 和 len(queryset) 如何选择

Note: Don‘t use len() on QuerySets if all you want to do is determine the number of records in the set. It‘s much more efficient to handle a count at the database level, using SQL‘s SELECT COUNT(*), and Django provides a count() method for precisely this reason.

(most crucial) When you only want to know the number of elements and you do not plan to process them in any way it‘s crucial to use count():

DO: queryset.count() - this will perform single SELECT COUNT(*) some_table query, all computation is carried on RDBMS side, Python just needs to retrieve the result number with fixed cost of O(1)

DON‘T: len(queryset) - this will perform SELECT * FROM some_table query, fetching whole table O(N) and requiring additional O(N) memory for storing it. This is the worst that can be done

简单说,如果只看长度,用count,如果循环取得数据,用len

django queryset的一些技术

原文:https://www.cnblogs.com/lxgbky/p/12468181.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!