Home > Net >  PERFORMANCE Calling multiple times a posgres db to get filtered queries vs querying all objects and
PERFORMANCE Calling multiple times a posgres db to get filtered queries vs querying all objects and

Time:12-01

I'm working in a Django project and it has a postgreSQL db.

I'm calling multiple times the model to filter the results:

latest = Product.objects.all().order_by('-update_date')[:4]
best_rate = Product.objects.all().order_by('rating')[:2]
expensive = Product.objects.all().order_by('-price')[:3]

But I wonder if it's better for performance and resources consumption to just do 1 query and get all the objects from the database and do the filtering inside my Django view.

all = Product.objects.all()
# Do some filtering here iterating over variable all

Which of these do you think would be the best approximation? Or do you have a better option?

CodePudding user response:

The second way you suggested will be better, you can do this way i guess:

products_all = Products.objects.all()
latest = products_all.order_by('-update_date')[:4]
best_rate = products_all.order_by('rating')[:2]
expensive = products_all.order_by('-price')[:3]

CodePudding user response:

The point of database tech is to allow your program to work with very large datasets. Your requirement is to retrieve the newest / best / cheapest items from your list of products.

Should you do this with one query operation or three? That depends on how many products you will have in your list when you're running at scale. If you know you will never have more than, say, 100 products, retrieve the data with one query and filter in your Django program. But if you will eventually have thousands of products, use three separate filters.

You don't want your application to take more and more RAM as it scales up.

  • Related