I have a few views in Django where I query the database about 50 times. I am wondering if it is faster to query the database or to grab the items once and count myself in python.
For example: On one page, I want to list the following statistics: - users who live in New York - users who are admin users - users who are admin users and have one car ... ... etc.
Is it faster to do many Django database queries such as the following:
User.objects.filter(state='NY').count()
User.objects.filter(type='Admin').count()
User.objects.filter(type='Admin', car=1).count()
Or would it be faster to grab all the users, loop through them in python just one time, and count each of these things as I go?
Does it matter on the database? Does it matter how many of these count() queries I execute (on some views I have upwards of 30 as my application is very data driven and is just spitting out many statistics)?
Thanks!