title: Which is a better way to query in Django (Raw query vs ORM)?
body: <p>I am using MySQL, I have two schemas and I have to perform a <strong>join over multiple schemas</strong>, which as you might know is <strong>not supported</strong> with the Django ORM.</p>
  
  <p>So, this is most simplified SQL query for this scenario</p>
  
  <pre><code>SELECT * FROM db1.a AS a
  JOIN db2.b AS b 
  ON b.some_id = a.id
  WHERE a.name = 'localhost'
  </code></pre>
  
  <p><strong>DJANGO ORM</strong></p>
  
  <p>Now, if I use Django ORM, I will have to do something like</p>
  
  <pre><code>a_obj = ModelA.objects.filter(name='localhost')
  ModelB.objects.filter(some_id__in=a_obj)
  </code></pre>
  
  <p><strong>DJANGO RAW SQL / CONNECTION</strong></p>
  
  <pre><code>from django.db import connection
  
  sql = 'SELECT * FROM db1.a AS a
         JOIN db2.b AS b 
         ON b.some_id = a.id
         WHERE a.name = %s'
  cursor = connection.cursor()
  try:
        cursor.execute(sql, ['localhost'])
        row = cursor.fetchall()
  except Exception as e:
        cursor.close
  </code></pre>
  
  <p>Now, I understand that both of them are going to give me the required result. But, why use one over the other? From what I see, the Django ORM will have to hit the database twice to get the required info, while the raw query can get it with a single hit, here in my opinion the raw query wins. Again, the raw query prevents from sql injection attacks by using parameterised query, which escapes the variable, thus preventing the attack.</p>
  
  <p>A lot of people have suggested using Django ORM, over the raw query, but, in these scenarios, I don't see Django ORM doing any better than Raw query, it not worse.</p>
  
  <p>So, which one should I use, keeping in mind database performance and security? And Why?</p>