sql - Django Optimisation of a queryset with Q objects -


i'm using django 1.8.

i have queryset requires logical "or" , "and". gives :

mymodel.objects.filter(     q(start__gt=today) | q(end__lte=today),     active=true).update(active=false) 

as may understand it, should take every active mymodel instances shouldn't have started, , 1 have finished, , deactivate them.

"start" , "end" datefields, , "active" boolean.

it works, generates query far being optimised. i'd able start query filtering on "active" state, check other 2 fields, because in database, have thousands of entries, few of them have active=true. boolean test faster comparisons.

i can't reorder arguments, because former 2 q() positioned argument while latter name argument, , can't chain multiple filter() because generates "or", , not "and".

is there way this?

first of all, sql command generated django orm won't have condition clauses in same order .filter methods. don't worry "optimal" order.

second, no matter in order clauses appear in sql command, db engine optimize request , generate execution plan tailored data distribution. db engine worth considering keeps data distribution statistics. if proportion of active records best discriminant in query, filtered first.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -