views:

613

answers:

7

hi, I want to get data from the database(MySQL) by JPA, I wand it sorted by some column value,

So, what is the best practice, to:

Retrieve the data from the database as list of objects (JPA), then sort it programmatically using some java APIs.

OR

Let the database sort it by using a sorting select query.

??

thanx in advance

+1  A: 

Let the database do the sorting. It's built to do the "dirty" work for you...

Mickel
+3  A: 

I'm almost positive that it will be faster to allow the Database to sort it. There's engineers who spend a lot of time perfecting and optimizing their search algorithms, whereas you'll have to implement your own sorting algorithm which might add a few more computations.

Crowe T. Robot
+1  A: 

I would let the database do the sort, they are generally very good at that.

Otávio Décio
+12  A: 

In general, you're better off using ORDER BY in your SQL query -- this way, if there is an applicable index, you may be getting your sorting "for free" (worst case, it will be the same amount of work as doing it in your code, but often it may be less work than that!).

Alex Martelli
+16  A: 

If you are retrieving a subset of all the database data, for example displaying 20 rows on screen out of 1000, it is better to sort on the database. This will be faster and easier and will allow you to retrieve one page of rows (20, 50, 100) at a time instead of all of them.

If your dataset is fairly small, sorting in your code may be more convenient if you want implement a complex sort. Usually this complex sort can be done in SQL but not as easily as in code.

The short of it is, the rule of thumb is sort via SQL, with some edge cases to the rule.

Gennadiy
+1 for nuance. asdf
Carl Manaster
+7  A: 

This is not completely on point, but I posted something recently that relates to database vs. application-side sorting. The article is about a .net technique, so most of it likely won't be interesting to you, but the basic principles remain:

Deferring sorting to the client side (e.g. jQuery, Dataset/Dataview sorting) may look tempting. And it actually is a viable option for paging, sorting and filtering, if (and only if):

1. the set of data is small, and

1. there is little concern about performance and scalability

From my experience, the systems that meet this kind of criteria are few and far between. Note that it’s not possible to mix and match sorting/paging in the application/database—if you ask the database for an unsorted 100 rows of data, then sort those rows on the application side, you’re likely not going to get the set of data you were expecting. This may seem obvious, but I’ve seen the mistake made enough times that I wanted to at least mention it.

It is much more efficient to sort and filter in the database for a number of reasons. For one thing, database engines are highly optimized for doing exactly the kind of work that sorting and filtering entail; this is what their underlying code was designed to do. But even barring that—even assuming you could write code that could match the kind of sorting, filtering and paging performance of a mature database engine—it’s still preferable to do this work in the database, for the simple reason that it’s more efficient to limit the amount of data that is transferred from the database to the application server.

So for example, if you have 10,000 rows before filtering, and your query pares that number down to 75, filtering on the client results in the data from all 10,000 rows being passed over the wire (and into your app server’s memory), where filtering on the database side would result in only the filtered 75 rows being moved between database and application. his can make a huge impact on performance and scalability.

The full post is here: http://psandler.wordpress.com/2009/11/20/dynamic-search-objects-part-5sorting/

Phil Sandler
+1  A: 

Let the database sort it. Then you can have paging with JPA easily without readin in the whole resultset.

Thorbjørn Ravn Andersen