title: Optimizing Django: nested queries vs relation lookups
body: <p>I have a legacy code which uses nested ORM query, which produces SQL SELECT query with JOIN, and conditions which also contains SELECT and JOIN. Execution of this query takes enormous time. By the way, when I execute this query in raw SQL, taken from <code>Django_ORM_query.query</code>, it performs with reasonable time. </p>
  
  <p>What are best practices for optimization in such cases? <br>
  Would the query perform faster if I will use <code>ManyToMany</code> and <code>ForeignKey</code> relations?</p>