database sort vs. programmatic java sort

后端 未结 9 2318
自闭症患者
自闭症患者 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:50

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

    0 讨论(0)
  • 2020-12-04 15:58

    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.

    0 讨论(0)
  • 2020-12-04 15:58

    Well, there is not really a straightforward way to answer this; it must be answered in the context.

    Is your application (middle tier) is running in the same node as the database?

    If yes, you do not have to worry about the latency between the database and middle tier. Then the question becomes: How big is the subset/resultset of your query? Remember that to sort this is middle tier, you will take a list/set of size N, and either write a custom comparator or use the default Collection comparator. Or, whatever. So at the outset, you are setback by the size N.

    But if the answer is no, then you are hit by the latency involved in transferring your resultset from DB to middle tier. And then if you are performing pagination, which is the last thing you should do, you are throwing away 90-95% of that resultset after cutting the pages.

    So the wasted bandwidth cannot be justified. Imagine doing this for every request, across your tenant organizations.

    However way you look at it, this is bad design.

    I would do this in the database, no matter what. Just because almost all applications today demand pagination; even if they don't sending massive resultsets over the wire to your client is a total waste; drags everybody down across all your tenants.

    One interesting idea that I am toying with these days is to harness the power of HTML5, 2-way data binding in browser frameworks like Angular, and push some processing back to the browser. That way, you dont end up waiting in the line for someone else before you to finish. True distributed processing. But care must be taken in deciding what can be pushed and what not.

    0 讨论(0)
提交回复
热议问题