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.
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(...))