Java application – How to optimize database calls and space & time which is more important

databasedesignjavaperformance

I am writing a simple Java app to run weekly. The app need call database to get data, check it and update.

The flow I need is little as following:

  • select configure,orgID where status=true from orgs;
    • orgs has thousands of rows, configure is blob
  • Check blob object configure, and filtered the orgID list
  • for filtered orgID list, select * from users where status=true and orgID in (orglist)
    • users is a huge table. for each orgID, there can be as much as 400k users.
  • for users information, we update one column of all these users

I have couple questions:

  1. when we select configure this field is blob and it can be as large as 1k. Is it good practice to get thousands of rows at one time? or it is better to make multiple db call? time and space, which is more important?

  2. users table has millions rows, we need update perhaps 1 column for 1 million rows. what is a good practice to make this update? is it better to make 1 million db call? or as little db calls as possible?

Best Answer

  1. when we select configure this field is blob and it can be as large as 1k. Is it good practice to get thousands of rows at one time? or it is better to make multiple db call? time and space, which is more important?

This is totally dependent on the application. The question is: does it meet your performance requirements?

For a simple app that runs once a week, use of time and memory is probably not very important at all. I would write the simple version (get all at once in a single query) first. As long as it performs fine, I wouldn't worry about it. And 1K times several thousand is probably not going to cause a problem.

If you experience any performance problems, only then should you bother to modify it to use multiple DB calls.

  1. users table has millions rows, we need update perhaps 1 column for 1 million rows. what is a good practice to make this update? is it better to make 1 million db call? or as little db calls as possible?

As above, your performance requirements should be driving such decisions. But in this case, making millions of DB calls is more likely to be problematic. And in addition, a single update that modifies many rows at once is typically easy to write.

So, I would aim for a single update statement that updates everything, or at most one update statement per orgID.

Related Topic