1

I have an application use sorting Criteria. But the issue is that before i use sorting through database (5000 of record sort). Now i have 20,000 records for sorting data. Someone said to me to use Java for sorting. It is supposed to be better for your application.

I have two questions:

  1. Which one better for performance Use Java and database sort?
  2. Suppose i use Java sorting it independent of db.

Can Java sorting handle 20,000 records?

3
  • 2
    1. That depends. 2. Yes, java can handle sorting of 20000 records :). Commented Nov 25, 2013 at 10:42
  • 1
    if you are holding your data in databse, then yes database sort is better Commented Nov 25, 2013 at 10:44
  • Unless you are developing for a very limited hardware, e.g. smartcard, the number of 20,000 objects is rather small. Commented Nov 25, 2013 at 10:56

3 Answers 3

5

If you can, sort your data in your database. It is definitely faster that in memory. Sorting depends on the algorithm and not on any specific technology. In result, java may sort any number of records. What you are interested at however is how to sort your data most efficiently, which in your case is in the database.

If you need to learn how to sort date using java in memory you may take read the following: http://docs.oracle.com/javase/tutorial/collections/algorithms/index.html#sorting

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

Comments

0

Getting the data out of the database as you want is always better ( think about using an index on the sort fields to improve the performance)

Comments

0

The following general rule applies when manipulating data in databases:

  1. Can it be done with SQL? Do it with SQL.
  2. Can it be done with PLSQL? Do it with PLSQL.
  3. Do it with the programming language of your choice, it will be slow anyways.

Why is it bad to do it in Java?

To be able to do something like sorting within Java you obviously first have to get the data from the database into your program space and afterwards you need to write it back. This is an obvious overhead that is way too often ignored, and especially becomes difficult, if you work with huge amounts of data. Just think about how long it takes to pull out 2 GB from a database - worst case - over a network connection and then even send the result back.

If you go the SQL/PLSQL way, all data stays in the database and never needs to be forwarded to your program. This not only removes the overhead of transfer, but as well allows the database to handle this in the most optimized form - another overhead that is often ignored. If you pull out data, the DB doesn't know what you are going to do with it, so it just has to hand over everything to your code. If you do something like a sort on one table, the DB i.E. knows that sub-tables and links are not affected anyways, so there is no need to even read that data. Yet again a noticeable performance gain.
Just think about what might be faster: your code that you wrote in 5 minutes, or the DB code that hundreds of people wrote in over 10 years, trying to squeeze out even the last bit of performance possible?

In addition if you read data from a database, it will be transfered to you in an insecure way. So if someone does a man-in-the-middle attack while you look through a user's passwords, that man in the middle now knows those passwords as well. Or the other way round: if your program has a bug that can be exploited to gain access to the user's critical data, this is a security issue. If your code never has that ability in the first place, because all that data is handled internally by the database, then there is nothing that can even be a security issue in your code.

4 Comments

Arguably, using PLSQL is not a good practice because it abstracts business logic from your codebase, leaving it fragmented. Let alone that you are coupled to the database.
Depends on your task of course, but if you have a heavy DB solution, you are usually tied to one DB and code will be fragmented anyways.
...which is a bad design practice.
Sometimes performance is more important than abstract design. Especially if you talk about 20% more performance through code or buying new hardware for 10,000$

Your Answer

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