Zhenya
Zhenya

Reputation: 6198

Is it better to get all the needed data from the database once or is it better to make more often requests?

So, I have to develop a web component with a help of CQ5 (a CMS in Java based on OSGi, Sling, JCR Content Repository)

This component should do the following: Collect answers to three questions from the user, and then, based on those answers of the user, the response from the database will be provided to the user (one could say it's a calculator)

The first question has 5 variants of answers, the second has 5 variants, and the third has also 5 variants.

But the problem is, if the user has selected, say, option 1 as an answer to the first question, the number of possible answers the second question will be decreased - there will be only 3 possible answers left.

For example, a user has questions: Which country are you from? Which state are you from? Which city are you from?

If the user answers Canada to the first question, he will then be able to choose only Canadian states as an answer to the second question.

So, on event when the form is changed by the user, the data in it should be updated through ajax

So, the question is: is it better to get all the data at once from the database (there is not so much of it as you see), or is it better to make a request to database on each "form-changing event"? Or maybe, it doesn't make any difference?

To my point of view, the second variant is better from the point of view of amount of code needed to be written, but what's better from the performance point of view?

Upvotes: 0

Views: 163

Answers (2)

David Lavender
David Lavender

Reputation: 8311

Premature optimisation should be avoided. I would go with the multiple-requests approach. Like you said, it will make the application much cleaner. Then, only if it proves too slow, you could look at caching or front-loading that data.

There is a lot of debate about premature optimisation - the general opinion is to avoid it until you've proved that you need it!

Upvotes: 3

Dominik Sandjaja
Dominik Sandjaja

Reputation: 6466

As usual: "it depends". For your example with the states it may be useful to preload all states if it is limited to Northern America. Once your list of countries grows and with that the number of states grows, it may be better to only fetch the needed states onChange from the underlying data storage.

I also make this choice dependent on the number of

  1. possibilities the user can choose from (only a few countries or all ~200 countries in the world?)
  2. amount of items in the second select box
  3. if the user should be able to see the non-matching items in the second select box (as would be the case if the items were only removed client side, without AJAX, just plain JS)
  4. required performance (is it OK to load the possibilities of the second box with a second delay or is it required to be near-instant?)

Evaluating those points usually gives you a good rule of thumb. For your actual case with 3*5 possibilities, pre-loading all possibilities from the database and doing the filtering in program code seems like a rational choice.

Upvotes: 1

Related Questions