Solved

Searching an arraylist in java or database query which is faster

Posted on 2009-04-02
6
612 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 Cremers
ID: 24057056
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
ID: 24057139
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 Cremers
ID: 24057171
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 26

Expert Comment

by:ksivananth
ID: 24057217
>>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 Cremers
ID: 24057280
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
ID: 24083366
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note: You must have administrative privileges in order to create/edit Roles. Salesforce.com (http://www.salesforce.com/) (SFDC) is a cloud-based customer relationship management (CRM) system. It is a database most commonly used by sales and marke…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 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

23 Experts available now in Live!

Get 1:1 Help Now