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?