annotation的中文含义是"注解"。正如这名字所暗示的,传递给annotate函数的每个参数,都会以"注解"的形式添加到model queryset返回的每一个object里面。
和annotate经常在一起使用的是aggregation函数。
举个栗子
Blog Model有一个外键entry指向Entry model。我们想计算每个blog有多少个entry:
>>> from django.db.models import Count
>>> q = Blog.objects.annotate(Count('entry'))
# The name of the first blog
>>> q[0].name
'Blogasaurus'
# The number of entries on the first blog
>>> q[0].entry__count
42
复制代码
我们一起break down上面这部分代码:
q = Blog.objects.annotate(Count('entry'))
复制代码
这里使用了Count这个aggregation函数,作用是对一个指定的Blog object,计算它对应的Entry object有多少个。Blog.objects.annotate(Count('entry'))就是对每个Blog object,计算一下与之对应entry有几个。返回值是一个queryset。与
Blog.objects.all()
复制代码
的区别在于,Blog.objects.annotate(Count('entry'))中的每一项,都多了一个entry__count字段,这就是我们想要的那个数据。
q[0].name
q[0].entry__count
复制代码
q是一个queryset,q[0]就是获取第一个object,他里面多了一个entry__count字段。
举个反栗子
如果你不知道annotate这个东西,你肯定会想到一种"pythonic"的方法:
q = Blog.objects.all()
for blog in q:
entry__count = blog.entry.count()
print(blog.name)
print(entry__count)
复制代码
这种方法更容易理解,但是会杀死你的性能。假如你有10W条blog,q = Blog.objects.all() 这里进行了一次查询,for循环那里,对每一个blog都要进行一次查询,所以总查询次数是10W+1次,也就是那么多次IO。而前面那种方法,总查询次数只有一次,IO只有一次,计算entry的个数是在数据库内容进行的,效率当然要高很多!
数据库查询有一个黄金原则:尽可能减少IO次数。而Python的for循环天然就会增加IO次数,所以,请拥抱annotation吧。
关注我的微信公众号