Django 中 Aggregation聚合的使用

Django 的 filter、exclude 等方法使得對數據庫的查詢很方便了。這在數據量較小的時候還不錯,但若是數據量很大,或者查詢條件比較複雜,那麼查詢效率就會很低。git

提升數據庫查詢效率能夠經過原生 SQL 語句來實現,可是它的缺點就是須要開發者熟練掌握 SQL。假若查詢條件是動態變化的,則編寫 SQL 會更加困難。數據庫

對於以便捷著稱的 Django,怎麼能忍受這樣的事。因而就有了Aggregation聚合django

聚合最好的例子就是官網給的案例了:翻譯

# models.py

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()

class Publisher(models.Model):
    name = models.CharField(max_length=300)

class Book(models.Model):
    name = models.CharField(max_length=300)
    pages = models.IntegerField()
    price = models.DecimalField(max_digits=10, decimal_places=2)
    rating = models.FloatField()
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)
    pubdate = models.DateField()

class Store(models.Model):
    name = models.CharField(max_length=300)
    books = models.ManyToManyField(Book)

接下來能夠這樣求全部書籍的平均價格:code

>>> from django.db.models import Avg, Max, Min

>>> Book.objects.all().aggregate(Avg('price'))
{'price__avg': Decimal('30.67')}

實際上能夠省掉 all()排序

>>> Book.objects.aggregate(Avg('price'))
{'price__avg': Decimal('30.67')}

還能夠指定返回的鍵名:ci

>>> Book.objects.aggregate(price_avg=Avg('price'))
{'price_avg': Decimal('30.67')}

若是要獲取全部書籍中的最高價格:開發

>>> Book.objects.aggregate(Max('price'))
{'price__max': Decimal('44')}

獲取全部書籍中的最低價格:文檔

>>> Book.objects.aggregate(Min('price'))
{'price__min': Decimal('12')}

aggregate() 方法返回的再也不是 QuerySet 了,而是一個包含查詢結果的字典。若是我要對 QerySet 中每一個元素都進行聚合計算、而且返回的仍然是 QuerySet ,那就要用到 annotate() 方法了。get

annotate 翻譯過來就是註解,它的做用有點像給 QuerySet 中的每一個元素臨時貼上一個臨時的字段,字段的值是分組聚合運算的結果。

比方說要給查詢集中的每本書籍都增長一個字段,字段內容是外鏈到書籍的做者的數量:

>>> from django.db.models import Count

>>> q = Book.objects.annotate(Count('authors'))
>>> q[0].authors__count
3

aggregate() 的語法相似,也能夠給這個字段自定義個名字:

>>> q = Book.objects.annotate(a_count=Count('authors'))

跨外鏈查詢字段也是能夠的:

>>> s = Store.objects.annotate(min_price=Min('books__price'), max_price=Max('books__price'))

>>> s[0].min_price
Decimal('12')
>>> s[0].max_price
Decimal('44')

既然 annotate() 返回的是查詢集,那麼天然也能夠和 filter()exclude() 等查詢方法組合使用:

>>> b = Book.objects.filter(name__startswith="Django").annotate(num_authors=Count('authors'))
>>> b[0].num_authors
4

聯用的時候 filterannotate 的順序會影響返回結果,因此邏輯要想清楚。

也能夠排序:

>>> Book.objects.annotate(num_authors=Count('authors')).order_by('num_authors')

總而言之,aggregateannotate 用於組合查詢。當你須要對某些字段進行聚合操做時(好比Sum, Avg, Max),請使用 aggregate 。若是你想要對數據集先進行分組(Group By)而後再進行某些聚合操做或排序時,請使用 annotate

進行此類查詢有時候容易讓人迷惑,若是你對查詢的結果有任何的疑問,最好的方法就是直接查看它所執行的 SQL 原始語句,像這樣:

>>> b = Book.objects.annotate(num_authors=Count('authors')).order_by('num_authors')
>>> print(b.query)
SELECT "aggregation_book"."id", "aggregation_book"."name",
"aggregation_book"."pages", "aggregation_book"."price",
"aggregation_book"."rating", "aggregation_book"."publisher_id", 
"aggregation_book"."pubdate", COUNT("aggregation_book_authors"."author_id") 
AS "num_authors" FROM "aggregation_book" LEFT OUTER JOIN "aggregation_book_authors" 
ON ("aggregation_book"."id" = "aggregation_book_authors"."book_id") 
GROUP BY "aggregation_book"."id", "aggregation_book"."name",
"aggregation_book"."pages", "aggregation_book"."price",
"aggregation_book"."rating", "aggregation_book"."publisher_id", 
"aggregation_book"."pubdate"
ORDER BY "num_authors" ASC
相關文檔: Aggregation

複合使用聚合時的相互干擾問題:Count and Sum annotations interfere with each other

相關文章
相關標籤/搜索