Why django ORM is much slower than raw SQL

I have the following two pieces of code:

First, in SQL:

self.cursor.execute('SELECT apple_id FROM main_catalog WHERE apple_id=%s', apple_id) if self.cursor.fetchone(): print '##' 

Next, in Django:

 if Catalog.objects.filter(apple_id=apple_id).exists(): print '>>>' 

Performing this first method is about 4 times faster than the second path in a loop of 100 thousand records. Which explains that Django is much slower?

+3
source share
1 answer

Typically, ORMs are faced with the problem of instantiating an object for each row and returning it. Your raw SQL does not do this, so it wonโ€™t take the penalty. For large result sets where you are not going to use an object, it is better to bypass ORM.

+6
source

All Articles