Djangoのmodelクエリ操作とクエリの性能の最適化
1どのようにORMクエリをする時SQlの実行状況を調べますか?
(1)最下階のdjango.db.co nnection
django shellで使用します。 python manage.py shell
このように毎回検索すると、sql出力があります。
基本操作
FK foreign keyが使用する理由:制約 はハードディスク を節約します。
しかし、複数テーブルのクエリは速度を下げます。大型プログラムはむしろ外キーを使わず、単表(制約の場合はコードで判断します。)
extra
defer(self、*fields):
(1)最下階のdjango.db.co nnection
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-extensionプラグイン
pip install django-extensions
INSTALLED_APPS = (
...
'django_extensions',
...
)
django shellで使用します。 python manage.py shell_プラス --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、*lookup)
: , 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 ,
次の2つは対象となり、他のフィールドを取得することができます。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)
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。