Monday, 15 February 2010

Django ORM queryset on nested queries -



Django ORM queryset on nested queries -

i can't figure out how default django orm processes queries like:

model.objects.filter(foreign__field=value)

does create lookup on each row, or smart plenty resolve foreign field id(s) target value?

or cheaper create way?

value_temp = foreign.objects.get(field=value) model.objects.filter(foreign=value_temp)

since django orm front-end sql, , since sql performance database- , context-dependent, it's hard generalize. that, in general, unless have understanding of performance tradeoffs involved shouldn't bother trying work around straightforward approach. both django , sql databases take pains optimize typical query patterns.

in specific illustration cited, first phone call join between 2 tables, while sec create 2 separate queries, each of single table. factors include: round-trip time access database; whether there's index on foreign key; whether there's index on field; , specific values in database. guess first faster, since databases tailored kind of lookup. (you ask: does create lookup on each row or smart plenty resolve foreign field id target value? reply it's database, not django, makes decision; and, yes, database take efficient route.)

django django-orm

No comments:

Post a Comment