Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance: Java vs. Database

When programming there are decisions to make all the time, like:

  • Should I generate my Menu/Buttons/Navigation dynamically from a db entry or static via code
  • Should I count entries of a list by Java or firing a DB query ..or maybe you find more of those DB vs Javacode questions.

(I hope that question is not too common:)

What should your DB do and what excercises are better done by your Java-code, regarding performance issues in web applications.

Maybe differing between small and huge projects is reasonable

like image 644
Sven Avatar asked Apr 19 '26 02:04

Sven


2 Answers

Extending Ignacio's answer a bit: the DB typically has a big latency (unless it is physically on the same machine), so you want to hit it as rarely as possible. If you use an ORM like Hibernate, you get caching and lazy loading for free; otherwise you need to take care of these yourself.

Thus it is OK to fetch GUI element data from the DB once - then cache and reuse it locally. And it is better to count entries of a list locally if you already have all the elements. If you need a query anyway, you can try combining the fetches into one. However, if you have a huge list and you want to select relatively few elements from it, it may be preferred to let the DB do the work and return only the selected entries, as opposed to cramming a large amount of data through a slow network connection.

like image 75
Péter Török Avatar answered Apr 21 '26 16:04

Péter Török


Database is slow. Java code is relatively very fast. Cache everything pulled from the database in memory if possible, possibly using something like memcache (if relevant; I don't do much Java web code).

like image 25
Ignacio Vazquez-Abrams Avatar answered Apr 21 '26 16:04

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!