Are DB hits costlier than accessing collection in java?
collections, database, java, performance
Solution
You can answer this yourself if you think through what happens when you talk to the database:
- Your program has to send the query to the database. Depending on whether the database server is running in-process or somewhere else on the network, this may take anywhere from a few microseconds to a few milliseconds.
- The database server has to parse your query and generate an execution plan. Depending on the server, it might cache an execution plan for frequently executed queries. If not, plan on another few microseconds to generate the plan.
- The database server has to execute your plan, reading whatever disk blocks are needed to access the data. Each disk access will take tens of milliseconds. Depending on how large the table is, and how well it is indexed, your query might take seconds.
- The database server has to package up the data and send it back to the application. Again, depending on whether it's in-process or across the network, this will take microseconds to milliseconds, and it will vary depending on how much data is sent back.
- Your application must convert the retrieved data into a useful form. This is probably a microsecond or less.
By comparison, a lookup on a hashed data structure requires a few memory accesses, which may take a few nanoseconds each. The difference is several orders of magnitude.
Problem
Just implemented a design where i had cached some data in hashmap and retrieved data from it instead querying the same data from DB. Is my thinking correct ?