在Django中,检查空查询集最有效的方法是什么? [英] In Django, what is the most efficient way to check for an empty query set?

查看:159
本文介绍了在Django中,检查空查询集最有效的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过使用以下内容的建议:

 如果qs.exists():
.. 。

如果qs.count():
...

try:
qs [0]
除了IndexError:
...

从以下评论中复制:我正在寻找一个In MySQL和PostgreSQL计数()对于短查询来说更快,对于长查询来说,exists()更快,并且当可能需要第一个元素并且要检查它是否存在时,使用QuerySet [0]。然而,当count()更快时,它只是稍微更快,所以建议在两者之间选择时始终使用exists()。

解决方案

看起来像 qs.count()和qs.exists()有效地相当于,所以我没有发现使用exists()超过count()的原因,后者不慢它可以用于检查存在和长度,可能存在()和count()对于MySQL中的同一查询求值。



只能使用 qs [0] 如果您真的需要该对象,如果您只是测试存在,则显着较慢。



在Amazon SimpleDB上,有400,000行:




  • qs :325.00 usec / pass

  • qs.exists():144.46 usec / pass

  • qs.count() 144.33 usec / pass

  • qs [0] :324.98 usec / pass



在MySQL上,57行:




  • qs :1.07 usec / pass

  • qs.exsts():1.21 usec / pass

  • qs.count():1.16 usec / pass

  • qs [0] :1.27 usec / pass



我为每次通过使用随机查询来降低db级缓存的风险。测试代码:

  import timeit 

base =
import random
from plum.bacon.models import Session
ip_addr = str(random.randint(0,256))+'。'+ str(random.randint(0,256))+'。'+ str(random.randint( 0,256))+'。'+ str(random.randint(0,256))
try:
session = Session.objects.filter(ip = ip_addr)%s
如果会话:
pass
除了:
pass


query_variatons = [
base%,
base%.exists (),
base%.count(),
base%[0]
]

在query_variatons中:
t = timeit.Timer(stmt = s)
打印%.2f usec / pass%(1000000 * t.timeit(number = 100)/ 100000)
pre>

I've heard suggestions to use the following:

if qs.exists():
    ...

if qs.count():
    ...

try:
    qs[0]
except IndexError:
    ...

Copied from comment below: "I'm looking for a statement like "In MySQL and PostgreSQL count() is faster for short queries, exists() is faster for long queries, and use QuerySet[0] when it's likely that you're going to need the first element and you want to check that it exists. However, when count() is faster it's only marginally faster so it's advisable to always use exists() when choosing between the two."

解决方案

It looks like qs.count() and qs.exists() are effectively equivalent. Therefore I have not discovered a reason to use exists() over count(). The latter is not slower and it can be used to check for both existence and length. It's possible that both exists() and count() evaluate to the same query in MySQL.

Only use qs[0]if you actually need the object. It's significantly slower if you're just testing for existence.

On Amazon SimpleDB, 400,000 rows:

  • bare qs: 325.00 usec/pass
  • qs.exists(): 144.46 usec/pass
  • qs.count() 144.33 usec/pass
  • qs[0]: 324.98 usec/pass

On MySQL, 57 rows:

  • bare qs: 1.07 usec/pass
  • qs.exsts(): 1.21 usec/pass
  • qs.count(): 1.16 usec/pass
  • qs[0]: 1.27 usec/pass

I used a random query for each pass to reduce the risk of db-level caching. Test code:

import timeit

base = """
import random
from plum.bacon.models import Session
ip_addr = str(random.randint(0,256))+'.'+str(random.randint(0,256))+'.'+str(random.randint(0,256))+'.'+str(random.randint(0,256))
try:
    session = Session.objects.filter(ip=ip_addr)%s
    if session:
        pass
except:
    pass
"""

query_variatons = [
    base % "",
    base  % ".exists()",
    base  % ".count()",
    base  % "[0]"
    ]

for s in query_variatons:
    t = timeit.Timer(stmt=s)
    print "%.2f usec/pass" % (1000000 * t.timeit(number=100)/100000)

这篇关于在Django中,检查空查询集最有效的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆