As I’ve mentioned countless times, the performance of a website can really make or break a user’s experience. Everyone places emphasis on tuning and …
As I’ve mentioned countless times, the performance of a website can really make or break a user’s experience. Everyone places emphasis on tuning and sharding databases, and buying multi-threaded and multi-core processors. These are all viable solutions but they are either pricey or labor intensive. So what are some quick and clean ways to improve the performance of your web app or web service?
Start with the slowest moving part and work from there, disk I/O, or more specifically data base access times. What impacts data base access time? Aside from mechanical parts…slow queries. Ok what are some slow queries? Queries that perform joins and table scans. The biggest and quietest culprit in producing slow queries ORM (Object relationship mapping) software such as Hibernate. Hibernate’s job is to make life easier for Java developers by abstracting away the relationship of a POJO (plain old java object) to a data base object. However, it has a few pitfalls which cause slow queries. Lets explore just two for today:
1. Non-lazy loading of non-nullable objects.
2. Retrieving every column in a row.
If you have a data base object A that has a foreign key relationship to another object B, which is non-nullable Hibernate will load B when only object A is requested. One solution to avoid this is to avoid using a Hibernate query such as the Criteria query, and instead using a straight up JDBC call to retrieve object A.
Suppose only the name field of object A is desired, while this is possible to do in Hibernate, a straight up JDBC call to retrieve a single column from object A is much faster.
Using ORM software apis is easy, but you should understand the price you pay in terms of performance. Use Hibernate queries when you need relationships to be managed amongst objects like when you want to traverse a class hierarchy in Java. But if all you want is a small piece of data using straight JDBC calls are faster and will get you the data you need when you need it.