Solved

Searching an arraylist in java or database query which is faster

Posted on 2009-04-02
6
621 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Salesforce.com is a cloud-based customer relationship management (CRM) system. In this article, you will learn how to add and map custom lead and contact fields to your Salesforce instance.
Let’s face it: one of the reasons your organization chose a SaaS solution (whether Microsoft Dynamics 365, Netsuite or SAP) is that it is subscription-based. The upkeep is done. Or so you think.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

624 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