Java – advantages and disadvantages of data sorting in database?
Let's say I have a table with a varchar type table And I need to get the data in the table alphabetically from the field
What is the best method (performance): add fields to SQL queries or sort data when it has been obtained?
I use Java (hibernate), but I can't tell anything about the DB engine It can be any popular relational database (such as MySQL or MS SQL server or Oracle or hsql dB or any other database)
The number of records in the table may vary greatly, but it is assumed that there are 5K records
UPD: how does the L2 sleep cache (for example, ehcache) support sorting data?
Solution
If the field is indexed, the average DB is more efficient than Java in this task Also note that if it is a pure display, it usually does not retrieve all these rows at once, but a subset of it so that it can be displayed in pages You can also do this at the DB level Sorting data in Java will require dragging and dropping the entire table into JAVA memory, which you do not want to do
In Hibernate, you can use criteria #addorder() and pagination to order results using criteria #setfirstresult() and criteria #setmaxresults()
List users = session.createCriteria(User.class) .addOrder(Order.asc("username")) .setFirstResult(0) // Index of first row to be retrieved. .setMaxResults(10) // Amount of rows to be retrieved. .list();