亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Django的model查詢操作與查詢性能優(yōu)化

 更新時間:2018年10月16日 09:51:36   作者:派對動物  
這篇文章主要介紹了詳解Django的model查詢操作與查詢性能優(yōu)化,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1 如何 在做ORM查詢時 查看SQl的執(zhí)行情況

(1) 最底層的 django.db.connection

在 django shell 中使用  python manage.py shell

>>> from django.db import connection
>>> Books.objects.all()
>>> connection.queries  ## 可以查看查詢時間
[{'sql': 'SELECT "testsql_books"."id", "testsql_books"."name", "testsql_books"."author_id" FROM "testsql_books" LIMI
T 21', 'time': '0.002'}]

(2) django-extensions 插件 

pip install django-extensions
 INSTALLED_APPS = (
    ...
    'django_extensions',
    ...
    )

在 django shell 中使用  python manage.py shell_plus  --print-sql (extensions 強化)

這樣每次查詢都會 有sql 輸出

>>> from testsql.models import Books
>>> Books.objects.all()
  SELECT "testsql_books"."id", "testsql_books"."name", "testsql_books"."author_id" FROM "testsql_books" LIMIT 21

Execution time: 0.002000s [Database: default]

<QuerySet [<Books: Books object>, <Books: Books object>, <Books: Books object>]>

2 ORM查詢操作 以及優(yōu)化

基本操作

增

models.Tb1.objects.create(c1='xx', c2='oo') 增加一條數(shù)據(jù),可以接受字典類型數(shù)據(jù) **kwargs

obj = models.Tb1(c1='xx', c2='oo')
obj.save()

 查

models.Tb1.objects.get(id=123)     # 獲取單條數(shù)據(jù),不存在則報錯(不建議)
models.Tb1.objects.all()        # 獲取全部
models.Tb1.objects.filter(name='seven') # 獲取指定條件的數(shù)據(jù)
models.Tb1.objects.exclude(name='seven') # 獲取指定條件的數(shù)據(jù)

 刪

models.Tb1.objects.filter(name='seven').delete() # 刪除指定條件的數(shù)據(jù)

 改
models.Tb1.objects.filter(name='seven').update(gender='0') # 將指定條件的數(shù)據(jù)更新,均支持 **kwargs
obj = models.Tb1.objects.get(id=1)
obj.c1 = '111'
obj.save()                         # 修改單條數(shù)據(jù)

查詢簡單操作

獲取個數(shù)

  models.Tb1.objects.filter(name='seven').count()

大于,小于

  models.Tb1.objects.filter(id__gt=1)       # 獲取id大于1的值
  models.Tb1.objects.filter(id__gte=1)       # 獲取id大于等于1的值
  models.Tb1.objects.filter(id__lt=10)       # 獲取id小于10的值
  models.Tb1.objects.filter(id__lte=10)       # 獲取id小于10的值
  models.Tb1.objects.filter(id__lt=10, id__gt=1)  # 獲取id大于1 且 小于10的值

in

  models.Tb1.objects.filter(id__in=[11, 22, 33])  # 獲取id等于11、22、33的數(shù)據(jù)
  models.Tb1.objects.exclude(id__in=[11, 22, 33]) # not in

isnull
  Entry.objects.filter(pub_date__isnull=True)

contains

  models.Tb1.objects.filter(name__contains="ven")
  models.Tb1.objects.filter(name__icontains="ven") # icontains大小寫不敏感
  models.Tb1.objects.exclude(name__icontains="ven")

range

  models.Tb1.objects.filter(id__range=[1, 2])  # 范圍bettwen and

其他類似

  startswith,istartswith, endswith, iendswith,

order by

  models.Tb1.objects.filter(name='seven').order_by('id')  # asc
  models.Tb1.objects.filter(name='seven').order_by('-id')  # desc

group by--annotate

  from django.db.models import Count, Min, Max, Sum
  models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num'))
  SELECT "app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"

limit 、offset

  models.Tb1.objects.all()[10:20]

regex正則匹配,iregex 不區(qū)分大小寫

  Entry.objects.get(title__regex=r'^(An?|The) +')
  Entry.objects.get(title__iregex=r'^(an?|the) +')

date

  Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
  Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))

year

  Entry.objects.filter(pub_date__year=2005)
  Entry.objects.filter(pub_date__year__gte=2005)

month

  Entry.objects.filter(pub_date__month=12)
  Entry.objects.filter(pub_date__month__gte=6)

day

  Entry.objects.filter(pub_date__day=3)
  Entry.objects.filter(pub_date__day__gte=3)

week_day

  Entry.objects.filter(pub_date__week_day=2)
  Entry.objects.filter(pub_date__week_day__gte=2)

hour

  Event.objects.filter(timestamp__hour=23)
  Event.objects.filter(time__hour=5)
  Event.objects.filter(timestamp__hour__gte=12)

minute

  Event.objects.filter(timestamp__minute=29)
  Event.objects.filter(time__minute=46)
  Event.objects.filter(timestamp__minute__gte=29)

second

  Event.objects.filter(timestamp__second=31)
  Event.objects.filter(time__second=2)
  Event.objects.filter(timestamp__second__gte=31)

查詢復(fù)雜操作

FK foreign key 使用的原因:

  • 約束
  • 節(jié)省硬盤

但是多表查詢會降低速度,大型程序反而不使用外鍵,而是用單表(約束的時候,通過代碼判斷)

extra

  extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None)
    Entry.objects.extra(select={'new_id': "select col from sometable where othercol > %s"}, select_params=(1,))
    Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
    Entry.objects.extra(where=["foo='a' OR bar = 'a'", "baz = 'a'"])
    Entry.objects.extra(select={'new_id': "select id from tb where id > %s"}, select_params=(1,), order_by=['-nid'])

F

  from django.db.models import F
  models.Tb1.objects.update(num=F('num')+1)

Q

  方式一:
  Q(nid__gt=10)
  Q(nid=8) | Q(nid__gt=10)
  Q(Q(nid=8) | Q(nid__gt=10)) & Q(caption='root')

  方式二:
  con = Q()
  q1 = Q()
  q1.connector = 'OR'
  q1.children.append(('id', 1))
  q1.children.append(('id', 10))
  q1.children.append(('id', 9))
  q2 = Q()
  q2.connector = 'OR'
  q2.children.append(('c1', 1))
  q2.children.append(('c1', 10))
  q2.children.append(('c1', 9))
  con.add(q1, 'AND')
  con.add(q2, 'AND')

  models.Tb1.objects.filter(con)

exclude(self, *args, **kwargs)

  # 條件查詢
  # 條件可以是:參數(shù),字典,Q

select_related(self, *fields)

   性能相關(guān):表之間進(jìn)行join連表操作,一次性獲取關(guān)聯(lián)的數(shù)據(jù)。
   model.tb.objects.all().select_related()
   model.tb.objects.all().select_related('外鍵字段')
   model.tb.objects.all().select_related('外鍵字段__外鍵字段')

prefetch_related(self, *lookups)

  性能相關(guān):多表連表操作時速度會慢,使用其執(zhí)行多次SQL查詢 在內(nèi)存中做關(guān)聯(lián),而不會再做連表查詢
      # 第一次 獲取所有用戶表
      # 第二次 獲取用戶類型表where id in (用戶表中的查到的所有用戶ID)
      models.UserInfo.objects.prefetch_related('外鍵字段')

annotate(self, *args, **kwargs)

# 用于實現(xiàn)聚合group by查詢

  from django.db.models import Count, Avg, Max, Min, Sum

  v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id'))
  # SELECT u_id, COUNT(ui) AS `uid` FROM UserInfo GROUP BY u_id

  v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id')).filter(uid__gt=1)
  # SELECT u_id, COUNT(ui_id) AS `uid` FROM UserInfo GROUP BY u_id having count(u_id) > 1

  v = models.UserInfo.objects.values('u_id').annotate(uid=Count('u_id',distinct=True)).filter(uid__gt=1)
  # SELECT u_id, COUNT( DISTINCT ui_id) AS `uid` FROM UserInfo GROUP BY u_id having count(u_id) > 1

extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None)

 # 構(gòu)造額外的查詢條件或者映射,如:子查詢

    Entry.objects.extra(select={'new_id': "select col from sometable where othercol > %s"}, select_params=(1,))
    Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
    Entry.objects.extra(where=["foo='a' OR bar = 'a'", "baz = 'a'"])
    Entry.objects.extra(select={'new_id': "select id from tb where id > %s"}, select_params=(1,), order_by=['-nid'])

reverse(self):

# 倒序
models.UserInfo.objects.all().order_by('-nid').reverse()
# 注:如果存在order_by,reverse則是倒序,如果多個排序則一一倒序

下面兩個 取到的是對象,并且注意 取到的對象可以 獲取其他字段(這樣會再去查找該字段降低性能
defer(self, *fields):

 models.UserInfo.objects.defer('username','id')
或
models.UserInfo.objects.filter(...).defer('username','id')
# 映射中排除某列數(shù)據(jù)

only(self, *fields):

# 僅取某個表中的數(shù)據(jù)
models.UserInfo.objects.only('username','id')
或
models.UserInfo.objects.filter(...).only('username','id')

執(zhí)行原生SQL

1.connection
from django.db import connection, connections
cursor = connection.cursor() 
# cursor = connections['default'].cursor()
django的settings中的db配置 ' default',指定數(shù)據(jù)庫
cursor.execute("""SELECT * from auth_user where id = %s""", [1])
row = cursor.fetchone()

2 .extra
Entry.objects.extra(select={'new_id': "select id from tb where id > %s"}, select_params=(1,), order_by=['-nid'])

3 . raw     
name_map = {'a':'A','b':'B'}
models.UserInfo.objects.raw('select * from xxxx',translations=name_map)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python面向?qū)ο笾蓡T相關(guān)知識總結(jié)

    Python面向?qū)ο笾蓡T相關(guān)知識總結(jié)

    通過面向?qū)ο筮M(jìn)行編程時,會遇到很多種情況,也會使用不同的成員來實現(xiàn),接下來我們來逐一介紹成員特性和應(yīng)用場景,需要的朋友可以參考下
    2021-06-06
  • python 反向輸出字符串的方法

    python 反向輸出字符串的方法

    今天小編就為大家分享一篇python 反向輸出字符串的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python繪制七段數(shù)碼管實例代碼

    Python繪制七段數(shù)碼管實例代碼

    這篇文章主要介紹了Python繪制七段數(shù)碼管實例代碼,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Python中獲取網(wǎng)頁狀態(tài)碼的兩個方法

    Python中獲取網(wǎng)頁狀態(tài)碼的兩個方法

    這篇文章主要介紹了Python中獲取網(wǎng)頁狀態(tài)碼的兩個方法,分別使用urllib模塊和requests模塊實現(xiàn),需要的朋友可以參考下
    2014-11-11
  • django項目中使用手機號登錄的實例代碼

    django項目中使用手機號登錄的實例代碼

    這篇文章主要介紹了django項目中使用手機號登錄的實例代碼,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-08-08
  • Python中常用的os操作匯總

    Python中常用的os操作匯總

    這篇文章主要匯總了Python中常用的os操作,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-11-11
  • Django如何防止定時任務(wù)并發(fā)淺析

    Django如何防止定時任務(wù)并發(fā)淺析

    這篇文章主要給大家介紹了關(guān)于Django如何防止定時任務(wù)并發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • pycharm的console輸入實現(xiàn)換行的方法

    pycharm的console輸入實現(xiàn)換行的方法

    今天小編就為大家分享一篇pycharm的console輸入實現(xiàn)換行的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python描述器descriptor詳解

    Python描述器descriptor詳解

    這篇文章主要向我們詳細(xì)介紹了Python描述器descriptor,需要的朋友可以參考下
    2015-02-02
  • python實現(xiàn)班級檔案管理系統(tǒng)

    python實現(xiàn)班級檔案管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)班級檔案管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05

最新評論