Rohit
Rohit

Reputation: 495

Django: bigger querysets & small no. of database hits or vice versa

I am developing a website on django & MySQL which is similar to linkedin in terms of complexity and functionality and am expecting steep growth for a few months.

I have one question. While designing the models and the views, I have two options. Either go for smaller number of database hits and handle bigger querysets(by making more complex queries & more joins) or handle smaller querysets thereby increasing the number of database hits. For example, when I use prefetch_related() & select_related(), the no. of database hits is reduced significantly, but the size of the querysets and the no of joins performed on the tables increase. This is just one example.

So considering the conditions under production, what should i prefer out of the above two. A little explanation would be better.

Upvotes: 1

Views: 83

Answers (1)

user142650
user142650

Reputation:

It is mostly better to go for smaller number of database hits because that may as well be your bottleneck when performance is critical.

Having more hits is almost like hitting the hard disk for a file every time you want to read a line. Of course the final answer depends on the frequency of your usage and the actual gains you derive by putting the queries together. For example, does it mean that you have to cache the data longer? How will it affect your app if it has to do the same for lots of users?

One way to start off is to try and reduce the number of query sets within the same function - if one function is doing 100 separate queries then you can do better by putting them together, if possible.

Upvotes: 1

Related Questions