使用Django annotation,提升django查询性能

2,229 阅读3分钟

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次。我们知道:django orm是对sql进行的一层封装,有封装自然就会有性能损失。每一次django的查询,都要从Python层进入数据库层,然后再从数据库层进入Python层,即使这样的一次转换时间是很短的,但是这么多次累计起来,消耗的无意义时间是很可观的。

而前面那种方法,总查询次数只有一次,从Python层进入数据库层再回到Python层的次数只有一次,效率当然要高很多!

django orm有一个性能优化技巧:尽可能减少Python层和数据库层转换的次数。而Python的for循环天然会增加这种转换次数。所以对于一些简单的逻辑,可以考虑使用annotate取代for循环。

勘误

很感谢有些朋友指出的,annotate并不一定能减少IO次数。

其实是书本(《数据库原理及应用》)第九章的问题,查询优化的问题,用了annotation和不用,看底层如何存储和存取方法是什么?文中举的实例是10w条,第二条是顺序遍历,annotation也不一定会一次都读到内存里啊,还要看预留缓冲区的大小,每个物理块存多少条数据,才能决定io次数,查询效率的高低与查询逻辑或查询语句的优略有关,但到最后还是要归结到底层。

所以用IO次数来解释性能差异是不严谨的,应该用Python层到数据库层的转换次数来解释。

下面来看一个我实际做的一个测试,看看使用annotate和使用for循环,性能差异到底有多大:

数据库中WX_User这个model一共有15W条数据。其中有一个ManyToManyField字段:

selected_stocks = models.ManyToManyField(Company, blank=True)

我们想知道每个用户有多少个selected_stocks。

方法一:annotate

def annotate_test(reuqest):
    from django.db.models import Count
    import time

    start = time.time()
    q = WX_User.objects.annotate(
        stock_count=Count('selected_stocks')
    )

    data = []
    for user in q:
        data.append(user.stock_count)
    end = time.time()

    return JsonResponse({
        'spent': end - start
    })

耗时10.7 s。

方法二:使用for循环

def annotate_test2(reuqest):
    import time

    start = time.time()
    q = WX_User.objects.all()

    data = []
    for user in q:
        data.append(user.selected_stocks.count())
    end = time.time()

    return JsonResponse({
        'spent': end - start
    })

耗时457s。

二者的性能差距是巨大的。

打个广告

关注我的微信公众号