Question on how the Oracle backend creates queries

49 views
Skip to first unread message

Divvid

unread,
Nov 23, 2016, 5:39:12 AM11/23/16
to Django developers (Contributions to Django itself)
This is more a question of understanding how things work than a issue

I'm working with Django 1.9 and Oracle 12 and python 3.4

I have a model Resources
When I do the following query to find all the uid's from matching resources in the Django shell

  Resources.objects.filter(remote_source=source.pk).values_list('uid')


I get the following SQL:
DEBUG (0.002) QUERY = 'SELECT * FROM (SELECT "_SUB".*, ROWNUM AS "_RN" FROM (SELECT "DATA_MASTER"."RESOURCES"."UID" AS Col1 FROM "DATA_MASTER"."RESOURCES" WHERE "DATA_MASTER"."RESOURCES"."REMOTE_SOURCE_ID" = :arg0) "_SUB" WHERE ROWNUM <= 21) WHERE "_RN" > 0' - PARAMS = ('6�mbe�$��S]�\x03\n\x0f�',); args=(b'6\xcbmbe\xac$\xe7\xe0S]\x99\x03\n\x0f\xce',)

A list of tuples is returned as expected.

However, when I do the same query, but now in a list comprehension to parse out the uid field I get a different (more optimized) query.

  [ x[0] for x in Resources.objects.filter(remote_source=a.pk).values_list('uid') if x[0] ]

DEBUG (0.100) QUERY = 'SELECT "DATA_MASTER"."RESOURCES"."UID" FROM "DATA_MASTER"."RESOURCES" WHERE "DATA_MASTER"."RESOURCES"."REMOTE_SOURCE_ID" = :arg0' - PARAMS = ('6�mbe�$��S]�\x03\n\x0f�',); args=(b'6\xcbmbe\xac$\xe7\xe0S]\x99\x03\n\x0f\xce',)

my question is : why do I get a SELECT * FROM in the first situation and why a SELECT "DATA_MASTER"."RESOURCES"."UID"
in the second situation. I would expect the SELECT <field> query always in a values_list query. i.e. how does the django orm know it lives inside the list comprehension.?

BTW, on PostgreSQL I always get the SELECT <field> query, regardless of the context
.

Cheers Div


Florian Apolloner

unread,
Nov 23, 2016, 7:42:01 AM11/23/16
to Django developers (Contributions to Django itself)
Doing that query in a shell will cause a repr() to be used which usually just shows the first 20 items or so, therefore it applies LIMIT 20 on databases supporting that, or that subselect with the rownumber on Oracle -- that is not really anything to worry about.

Cheers,
Florian
Reply all
Reply to author
Forward
0 new messages