开发者

Where do I sort?

I have a database, which I query, and I'm unsure of where to perform the sorting of the results, so far I've have the f开发者_Python百科ollowing options.

  • At the MySQL query.
  • At list level(Using a LinkedList)
  • Sorting an unsorted list using comparators before showing the results (basically in the jsp)

The List is composed by ObjectDTO so where would it be more efficient. Any ideas?


Database. Using indexes and other information about the data, db's are very good at this.


You should do the sorting in the database if at all possible.

  • The database can use indexes. If there is a suitable index available then the results can be read from disk already in sorted order, resulting in a performance increase - no extra O(n log(n)) sorting step is required.
  • If you only need the first x results you also minimize data transfer (both reduced network transfer, and also reduced disk access if there is a suitable index).


Best is at the mySQL query.

a) It is easy to do

b) If you use an index the sort happens when the index is created or when new rows are inserted automatically (sometimes an index needs a reorganization but this is a db admins daily business. This applies if the table is very huge.).

e) If the index includes the columns used in the where clause the access in general is faster

d) You do not need to read the whole table each time to do the sort for yourself

e) Even if you have no index I believe the DB can do the sorting best

Hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜