Home > OS >  What is efficient - single call to database server or multiple call for data?
What is efficient - single call to database server or multiple call for data?

Time:05-26

Actually, I'm new to coding. So just confused over this thing. Whether to retrieve data from database all at once and store it in local data variable or retrieve at different calls when data is required.

In React, building a amazon clone so i should get data at once from the database (like products list) or should get data of only those products which is needed.

CodePudding user response:

The answer depends on project specifics. Any call to the db, is a wait time for the application. The longer you have to wait, the less intuitive would be for users. However, depending on your application, you can do background sql calls while user is not aware of what is happening in the background.

So, in short, depends on your project :)

CodePudding user response:

I have faced similar issues with performance in our applications. It is always better to minimize the number of database calls from your application. A database connection is an expensive process. If you can get your desired data at once(assuming the query is not too slow), always choose to optimize your application by reducing the number of database calls. Developers use 3rd party caching like Redis solely to work from memory by reducing the number of database calls.

I am assuming that all the data you fetch from the database are compulsory for an operation. If they are not necessary (for example, lazyload or pagination), fetching an overhead of data from the database is useless and memory-consuming too. In such scenarios, the procedure depends upon your use case.

  • Related