Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Searching an arraylist in java or database query which is faster

Posted on 2009-04-02
6
Medium Priority
?
622 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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 1500 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

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…
Read about achieving the basic levels of HRIS security in the workplace.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

719 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