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

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

问题描述

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

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

那么,最佳做法是:

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

  • 让数据库使用排序选择查询对其进行排序.

提前致谢

推荐答案

如果您要检索所有数据库数据的一个子集,例如在屏幕上显示 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天全站免登陆