database sort vs. programmatic java sort

后端 未结 9 2344
自闭症患者
自闭症患者 2020-12-04 15:28

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:

  • Retrieve the data from t
9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 15:47

    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.

提交回复
热议问题