详解Django的model查询操作与查询性能优化


Posted in Python onOctober 16, 2018

1 如何 在做ORM查询时 查看SQl的执行情况

(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查询操作 以及优化

基本操作

增

models.Tb1.objects.create(c1='xx', c2='oo') 增加一条数据,可以接受字典类型数据 **kwargs

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

 查

models.Tb1.objects.get(id=123)     # 获取单条数据,不存在则报错(不建议)
models.Tb1.objects.all()        # 获取全部
models.Tb1.objects.filter(name='seven') # 获取指定条件的数据
models.Tb1.objects.exclude(name='seven') # 获取指定条件的数据

 删

models.Tb1.objects.filter(name='seven').delete() # 删除指定条件的数据

 改
models.Tb1.objects.filter(name='seven').update(gender='0') # 将指定条件的数据更新,均支持 **kwargs
obj = models.Tb1.objects.get(id=1)
obj.c1 = '111'
obj.save()                         # 修改单条数据

查询简单操作

获取个数

  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的数据
  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 不区分大小写

  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)

查询复杂操作

FK foreign key 使用的原因:

  • 约束
  • 节省硬盘

但是多表查询会降低速度,大型程序反而不使用外键,而是用单表(约束的时候,通过代码判断)

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)

# 条件查询
  # 条件可以是:参数,字典,Q

select_related(self, *fields)

性能相关:表之间进行join连表操作,一次性获取关联的数据。
   model.tb.objects.all().select_related()
   model.tb.objects.all().select_related('外键字段')
   model.tb.objects.all().select_related('外键字段__外键字段')

prefetch_related(self, *lookups)

性能相关:多表连表操作时速度会慢,使用其执行多次SQL查询 在内存中做关联,而不会再做连表查询
      # 第一次 获取所有用户表
      # 第二次 获取用户类型表where id in (用户表中的查到的所有用户ID)
      models.UserInfo.objects.prefetch_related('外键字段')

annotate(self, *args, **kwargs)

# 用于实现聚合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)

# 构造额外的查询条件或者映射,如:子查询

    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')
# 映射中排除某列数据

only(self, *fields):

# 仅取某个表中的数据
models.UserInfo.objects.only('username','id')
或
models.UserInfo.objects.filter(...).only('username','id')

执行原生SQL

1.connection
from django.db import connection, connections
cursor = connection.cursor() 
# cursor = connections['default'].cursor()
django的settings中的db配置 ' default',指定数据库
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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python的包管理器pip更换软件源的方法详解
Jun 20 Python
Python使用requests及BeautifulSoup构建爬虫实例代码
Jan 24 Python
Python模拟简单电梯调度算法示例
Aug 20 Python
详解多线程Django程序耗尽数据库连接的问题
Oct 08 Python
pycharm远程开发项目的实现步骤
Jan 20 Python
Python+OpenCV+pyQt5录制双目摄像头视频的实例
Jun 28 Python
详解Django 时间与时区设置问题
Jul 23 Python
python3.7 利用函数os pandas利用excel对文件名进行归类
Sep 29 Python
python计算二维矩形IOU实例
Jan 18 Python
python requests.get带header
May 05 Python
pygame用blit()实现动画效果的示例代码
May 28 Python
Python字符串split及rsplit方法原理详解
Jun 29 Python
python查看模块,对象的函数方法
Oct 16 #Python
Python中asyncio与aiohttp入门教程
Oct 16 #Python
python查看模块安装位置的方法
Oct 16 #Python
Django model序列化为json的方法示例
Oct 16 #Python
Python重新加载模块的实现方法
Oct 16 #Python
django Serializer序列化使用方法详解
Oct 16 #Python
为什么str(float)在Python 3中比Python 2返回更多的数字
Oct 16 #Python
You might like
一个程序下载的管理程序(四)
2006/10/09 PHP
重新封装zend_soap实现http连接安全认证的php代码
2011/01/12 PHP
php导入导出excel实例
2013/10/25 PHP
php 检查电子邮件函数(自写)
2014/01/16 PHP
PHP高级编程实例:编写守护进程
2014/09/02 PHP
Laravel模板引擎Blade中section的一些标签的区别介绍
2015/02/10 PHP
Laravel 5.1 on SAE环境开发教程【附项目demo源码】
2016/10/09 PHP
laravel Model 执行事务的实现
2019/10/10 PHP
angularJS 入门基础
2015/02/09 Javascript
jquery常用函数与方法汇总
2015/09/01 Javascript
JavaScript的ExtJS框架中数面板TreePanel的使用实例解析
2016/05/21 Javascript
JS只能输入正整数的简单实例
2016/10/07 Javascript
js浏览器html5表单验证
2016/10/17 Javascript
jQuery加密密码到cookie的实现代码
2017/04/18 jQuery
单行 JS 实现移动端金钱格式的输入规则
2017/05/22 Javascript
vue基于Element构建自定义树的示例代码
2017/09/19 Javascript
vue 修改 data 数据问题并实时显示操作
2020/09/07 Javascript
[48:24]完美世界DOTA2联赛循环赛LBZS vs Forest 第一场 10月30日
2020/10/31 DOTA
Python 初始化多维数组代码
2008/09/06 Python
python获取当前日期和时间的方法
2015/04/30 Python
python显示生日是星期几的方法
2015/05/27 Python
Python语法快速入门指南
2015/10/12 Python
在IPython中执行Python程序文件的示例
2018/11/01 Python
python 日期排序的实例代码
2019/07/11 Python
Python抓新型冠状病毒肺炎疫情数据并绘制全国疫情分布的代码实例
2020/02/05 Python
使用python接受tgam的脑波数据实例
2020/04/09 Python
python中remove函数的踩坑记录
2021/01/04 Python
html5文本内容_动力节点Java学院整理
2017/07/11 HTML / CSS
里程积分管理买卖交换平台:Points.com
2017/01/13 全球购物
美国在线购物频道:Shop LC
2019/04/21 全球购物
英文自我鉴定
2013/12/10 职场文书
中英文自我评价语句
2013/12/20 职场文书
求职信:会计求职的写作技巧
2019/04/24 职场文书
Nginx配置80端口访问8080及项目名地址方法解析
2021/03/31 Servers
mysql timestamp比较查询遇到的坑及解决
2021/11/27 MySQL
Win11控制面板快捷键是什么?Win11打开控制面板的方法汇总
2022/07/07 数码科技