Sorting on the server or on the client?

前端 未结 10 641
一生所求
一生所求 2020-12-04 22:31

I had a discussion with a colleague at work, it was about SQL queries and sorting. He has the opinion that you should let the server do any sorting before returning the rows

相关标签:
10条回答
  • 2020-12-04 22:41

    It depends... Is there paging involved? What's the max size of the data set? Is the entire dataset need to be sorted the same one way all the time? or according to user selection? Or, (if paging is involved), is it only the records in the single page on client screen need to be sorted? (not normally acceptable) or does the entire dataset need to be sorted and page one of the newly sorted set redisplayed?

    What's the distribution of client hardware compared to the processing requirements of this sort operation?

    bottom line is; It's the overall user experience (measured against cost of course), that should control your decision... In general client machines are slower than servers, and may cause additional latency. ... ... But how often will clients request additional custom sort operations after initial page load? (client sort of data already on client is way faster than round trip...) But sorting on client always requires that entire dataset be sent to client on initial load... That delays initials page display.. which may require lazy loading, or AJAX, or other technical complexities to mitigate...

    Sorting on server otoh, introduces additional scalability issues and may require that you add more boxes to the server farm to deal with additional load... if you're doing sorting in DB, and reach that threshold, that can get complicated. (To scale out on DB, you have to implement some read-only replication scheme, or some other solution that allows multiple servers (each doing processing) to share read only data)..

    0 讨论(0)
  • 2020-12-04 22:45

    Generally I agree with the views expressed above that server-side sorting is usually the way to go. However, there are sometimes reasons to do client-side sorting:

    • The sort criteria are user-selectable or numerous. In this case, it may not be a good idea to go adding a shedload of indices to the table - especially if insert performance is a concern. If some sort criteria are rarely used, an index isn't necessarily worth it since inserts will outnumber selects.
    • The sort criteria can't be expressed in pure SQL [uncommon], or can't be indexed. It's not necessarily any quicker client-side, but it takes load of the server.

    The important thing to remember is that while balancing the load between powerful clients and the server may be a good idea in theory, only the server can maintain an index which is updated on every insert. Whatever the client does, it's starting with a non-indexed unsorted set of data.

    0 讨论(0)
  • 2020-12-04 22:52

    I prefer custom sorting on the client, however I also suggest that most SQL statements should have some reasonable ORDER BY clause by default. It causes very little impact on the database, but without it you could wind up with problems later. Often times without ever realizing it, a developer or user will begin to rely on some initial default sort order. If an ORDER BY clause wasn't specified, the data is only in that order by chance. At some later date an index could change or the data might be re-organized and the users will complain because the initial order of the data might have changed out from under them.

    0 讨论(0)
  • 2020-12-04 22:54

    Situations vary, and measuring performance is important.

    Sometimes it's obvious - if you have a big dataset and you're interested in a small range of the sorted list (e.g. paging in a UI app) - sorting on the server saves the data transfer.

    But often you have one DB and several clients, and the DB may be overloaded while the clients are idle. Sorting on the client isn't heavy, and in this situation it could help you scale.

    0 讨论(0)
  • 2020-12-04 22:55

    As usual, "It Depends" :)

    If you have a stored procedure, for instance, that sends results to your presentation layer (whether a report, grid, etc.), it probably doesn't matter which method you go with.

    What I typically run across, though, are views which have sorting (because they were used directly by a report, for instance) but are also used by other views or other procedures with their own sorting.

    So as a general rule, I encourage others to do all sorting on the client-side and only on the server when there's reasonable justification for it.

    0 讨论(0)
  • 2020-12-04 22:56

    Do it on the server.

    If the dataset is big your server will deal with it much better then the client. Modern database servers have a powerful structure of indexes, caches and materialization that your humble browser or client app dont have

    If the dataset is small it will not have any performance or resource usage impact doing it on client or server.

    all of this considering your client application is well designed and you cannot be in the scenarios when if you do the sort on client and the sorting parameters change (like when the client says 'oooh, Now i want this major jasper report which you references in 356 different places with 23 different parameters now ordered by last name instead of date of birth'

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