Solved

Searching an arraylist in java or database query which is faster

Posted on 2009-04-02
6
618 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 32
tomcat administrtor 12 61
Unhandled exception type Exception 18 42
Running JavaFX on the Raspberry Pi 27 41
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

860 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