7

I hava an ArrayList that got filled from an SQL query and contains all the rows of a table.

If I want a subset of this ArrayList (say WHERE column1 = x)
Is it faster to remake an SQL connection and fetch a query with this condition, or loop through the arraylist and filter the data ? (considering an arraylist of 1000 rows)

If the loop through the arraylist is faster, until what size is it still appliable ?

Thank you

1
  • It depends on your environment configuration. Why won't you try some simple performance test? Commented Feb 2, 2013 at 11:41

2 Answers 2

5

It should be always faster to do this in Java. Java filtering of a memory array is as fast as, or faster, than SQL. And you also have to consider the overhead of connecting to the database, sending the command, and decoding the answer.

For really complex queries it could be simpler to have this done by SQL, and it probably scales better regarding to database size (think one million rows: are you going to keep them all in memory?). Also, the SQL query optimizer might have more information on column cardinality and be able to perform more efficiently, and therefore faster, overall, than your merely-faster-on-a-check-by-check-basis Java code.

On the other hand, also consider maintainability of the code. Some things are better left to SQL even if they're a mite slower that way.

Sign up to request clarification or add additional context in comments.

2 Comments

Also consider the overhead of transferring all those rows from the database to the Java application, the memory needed to store all those rows, to finally throw away most of those. I am not sure I agree it will be faster in Java.
@MarkRotteveel, normally it would not, but the OP was asking whether it would be faster to filter an already existing query array, or repeat the query. If the data is already Java side (and I agree with you, maybe something can be done there), then it's faster to reuse it.
3

If you have everything in memory already, my guess is that filtering the list will be faster than making an inter-process call to a database, especially for such a small amount of rows.

But it seems strange to have the full list in memory in the first place. Databases are fast, and are there to return the data you want when you want it. Caching the contents of such small tables in memory probably makes your application more complex for no significant speed gain. I would try keeping the application as stateless as possible unless you have a performance problem and you prove that caching the table contents solves the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.