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?
source share