简体   繁体   中英

Django ORM query performance

I am trying to find best the django query for my Django app. I am using default Sqlite DB as backend. I am using timeit to find time taken for query.

>>> import timeit
>>>
>>> setup_arg = 'from .models import AwsConsoleAccess'
>>> stmt1 = """AwsConsoleAccess.objects.filter(request_id='8548a2d5-4bb7-4fa9-add2-a41219dc8785')"""
>>> stmt2 = """AwsConsoleAccess.objects.filter(request_id='8548a2d5-4bb7-4fa9-add2-a41219dc8785').values('status')"""
>>>
>>>
>>> print(timeit.timeit(setup=setup_arg, stmt=stmt1, number=100) * 1000, 'ms')
7.873513997765258 ms
>>> print(timeit.timeit(setup=setup_arg, stmt=stmt2, number=100) * 1000, 'ms')
11.816384001576807 ms

SQL query executed:


>>> print(AwsConsoleAccess.objects.filter(request_id='8548a2d5-4bb7-4fa9-add2-a41219dc8785').query)
SELECT "aws_console_awsconsoleaccess"."created",.....(all fields) FROM "aws_console_awsconsoleaccess" WHERE "aws_console_awsconsoleaccess"."request_id" = 8548a2d54bb74fa9add2a41219dc8785
>>> print(AwsConsoleAccess.objects.filter(request_id='8548a2d5-4bb7-4fa9-add2-a41219dc8785').values('status').query)
SELECT "aws_console_awsconsoleaccess"."status" FROM "aws_console_awsconsoleaccess" WHERE "aws_console_awsconsoleaccess"."request_id" = 8548a2d54bb74fa9add2a41219dc8785

In theory select field from table should run faster than select * from table but my result are not reflecting it.

  1. Did I do my testing properly?
  2. Is there any other way to test the performance between two queries in Django ORM?

You have not considered the fact that QuerySets are lazy , that is when you create a queryset no actual query is made to the database. The query will run only when the results of the query are actually needed.

So when you write Model.objects.filter(...) all that does is create a query. You should instead force the evaluation of the queryset, for example by converting it to a list. This will actually run the query and give you more accurate results:

list(Model.objects.filter(...))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM