国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-02-15 23:14:28
字體:
供稿:網(wǎng)友

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

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

在 django shell 中使用  python manage.py shell

>>> from django.db import connection>>> Books.objects.all()>>> connection.queries  ## 可以查看查詢時(shí)間[{'sql': 'SELECT "testsql_books"."id", "testsql_books"."name", "testsql_books"."author_id" FROM "testsql_books" LIMIT 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 強(qiáng)化)

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

>>> from testsql.models import Books>>> Books.objects.all()  SELECT "testsql_books"."id", "testsql_books"."name", "testsql_books"."author_id" FROM "testsql_books" LIMIT 21Execution 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ù) **kwargsobj = models.Tb1(c1='xx', c2='oo')obj.save() 查models.Tb1.objects.get(id=123)     # 獲取單條數(shù)據(jù),不存在則報(bào)錯(cuò)(不建議)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ù)更新,均支持 **kwargsobj = models.Tb1.objects.get(id=1)obj.c1 = '111'obj.save()                         # 修改單條數(shù)據(jù)

查詢簡單操作

獲取個(gè)數(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 inisnull  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')  # descgroup 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ā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 获嘉县| 铁岭市| 定州市| 涿鹿县| 越西县| 大庆市| 来安县| 化隆| 温宿县| 梓潼县| 台湾省| 阿合奇县| 东台市| 新干县| 东乌珠穆沁旗| 建阳市| 石林| 乾安县| 武鸣县| 利辛县| 庄河市| 郑州市| 霸州市| 东安县| 临海市| 马关县| 高雄市| 鄯善县| 安丘市| 邳州市| 原阳县| 曲靖市| 庄浪县| 德钦县| 临沂市| 温宿县| 博野县| 肥东县| 驻马店市| 永仁县| 灵武市|