Solved

Searching an arraylist in java or database query which is faster

Posted on 2009-04-02
6
608 Views
Last Modified: 2013-11-24
Searching an arraylist of size 25000 (using contains method)  or queryig a database table of the same size, which is faster?
We have to validate a field entered by the user, we can cache the master data for that field in application scope in an arraylist and use contains function to look for the validity of user entered data. Or fire an SQL (Oracle database)  to check if the data exist. caching 25000 records in Tomcat server is it advisable? is the contains function faster than SQL considering the network round trip required for a database call?
0
Comment
Question by:gksuresh_ee
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:Bart_Cr
Comment Utility
If you don't need to open a new DB connection (connection pooling) and you've got the correct indexes placed on your table, the Oracle DB call probably will be faster. But the best way to find out is to write both and do a benchmark check.
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
in memory is much faster than Db call! that is the reason the cache evolved!

you may choose the best between map or list based on the data you are going to store. Usually search will be faster in Map than list!
0
 
LVL 13

Expert Comment

by:Bart_Cr
Comment Utility
Caches never use arraylists to perform searches, as stated in the question. Having a map with a well chosen key can indeed be faster, but caching an entire table containing 25000 records is hardly ever a good idea.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
>>Caches never use arraylists to perform searches

thats why I told him to consider the map data structure and choose the best fit for his data and search!

>>caching an entire table containing 25000 records is hardly ever a good idea

Well, it purely depends the size of a single entity but 25000 is not a big number to be worried as you can get memory for less cost now a days. Its a trade off between efficiency vs memory!
0
 
LVL 13

Expert Comment

by:Bart_Cr
Comment Utility
The reason I say is caching 25000 records is probably not a good idea is that I can not imagine a table of that size to contain frozen data. And if the data is not fixed, it's a pain to keep your map up to date.
0
 
LVL 3

Accepted Solution

by:
kingsob earned 500 total points
Comment Utility
the time it takes to search the array list in memory is prob less than the time it will take to send a single packet to the database server (with pooled connections or not, its still got to make blocking system calls)

that being said, i would still go with the database, thats what its for

also, it will allow for growth in the future
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now