The way the query is written strikes me as very inefficient. Why use 2 nested subqueries instead of relational joins for which the database optimizer will be able to generate a widely more efficient execution plan?
Something along the line of
select account_subscription_line.id
from account_subscription_line join account_subscription on (subscription_id=account_subscription.id) join account_model on (account_subscription.model_id=account_model.id)
where account_subscription_line.date<%s and account_subscription_line.move_id is null
and account_model.company_id=%s;
The way the query is written strikes me as very inefficient. Why use 2 nested subqueries instead of relational joins for which the database optimizer will be able to generate a widely more efficient execution plan?
Something along the line of
select account_ subscription_ line.id subscription_ line join account_ subscription on (subscription_ id=account_ subscription. id) join account_model on (account_ subscription. model_id= account_ model.id) subscription_ line.date< %s and account_ subscription_ line.move_ id is null model.company_ id=%s;
from account_
where account_
and account_
(not tested, maybe outer joins should be used)