Django Query Performance

I have a performance related question regarding django requests.

Let's say I have a staff table with 10,000 records. Now, if I'm looking to select 5 random employees whose age is greater than or equal to 20, let's say about 5,500 employees are 20 years or older. Django request:

Employee.objects.filter(age__gte=20).order_by('?')[:5]

and a raw instance of this query in mysql would be:

SELECT * FROM `database`.`employee` 
WHERE `employee`.`age` >= 20
ORDER BY RAND ()
LIMIT 5;

From the appearance of the django query, the database first returns 5,500 records, then python sorts these records in any or any order that we select, and a piece of the first five records is returned, while the raw query returns only five records from the database directly.

My question is, is there a performance difference between the two queries? If so, which one is better and why?

+4
2

:

queryset = BlahModel.objects.order_by('?')[:5]
print queryset.query

:

SELECT `blah_model`.`id`, `blah_model`.`date` FROM `blah_model` ORDER BY RAND() LIMIT 5;

, .

, django ORM - sql django, order_by('?') ORDER BY RAND(), [:5] LIMIT mysql ( doc doc).

+1
+1

Source: https://habr.com/ru/post/1607044/


All Articles