数据库排序与程序化java排序 [英] database sort vs. programmatic java sort

查看:513
本文介绍了数据库排序与程序化java排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过JPA从数据库(MySQL)获取数据,我希望它按某些列值排序。

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

那么,最佳实践是什么, :

So, what is the best practice, to:


  • 从对象列表(JPA)中检索数据库中的数据,然后
    使用一些java API以编程方式对其进行排序。

OR


  • 让数据库排序它通过使用排序选择查询。

提前致谢

推荐答案

如果要检索所有数据库数据的子集,例如在1000个屏幕上显示20行,则最好对数据库进行排序。这将更快,更容易,并允许您一次检索一页行(20,50,100)而不是所有行。

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.

如果您的数据集如果你想要实现一个复杂的排序,在代码中排序可能会更方便。通常这种复杂的排序可以在 SQL 中完成,但不像在代码中那么容易。

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.

缺点是,经验法则是通过 SQL 排序,并对规则有一些边缘情况。

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

这篇关于数据库排序与程序化java排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆