displaytag sorting/paging performance vs. native SQL
displaytag is an easy-to-use JSP tag library for printing out HTML data tables with sorting and paging. But, there's a catch. In an enterprise application, collections are the result of some database query, so implementing sorting and paging entirely within the presentation layer is not going to be as efficient as doing both natively in the database with ORDER BY, LIMIT and OFFSET (assume PostgreSQL syntax). Instead, displaytag expects the entire collection to be in memory, displaying only the necessary ones, and sorting the Java collection itself.
I ran some tests to see exactly how much this affects performance. On a non-scientific test (YMMV) with a 250 row result set, sorting with displaytag added an 34% overhead over a native ORDER BY. There was a much more dramatic difference for paging, though. On a test where I loaded 250 rows from the DB and displayed only 25 (pagesize=25), displaytag took 141% longer on average than a "LIMIT 25" which only returns the 25 rows to actually be displayed. The overhead factor will scale with the total number of rows in the original, unlimited query.
So, displaytag clearly is not as efficient as sorting and paging in native SQL. But is it enough of a difference to matter? Well, it depends on your application--it's a classic tradeoff between performance and elegant design. Separation of concerns says paging results conceptually belongs in the presentation layer; in a layered J2EE application, you can push sorting/paging down through the business and persistence layers, but it isn't pretty.
My personal opinion, though, is that performance normally shouldn't be enough of a factor to dissuade you from using displaytag for sorting/paging results. If your query returns so many rows that you can't afford the overhead from the undisplayed rows, then you may not be thinking about paging the right way--paging should be thought of as purely a UI layer construct to save the user from scrolling or downloading a large HTML document, not to save the database from working too hard. How meaningful is it to jump from page 1 to page 47 out of 62 anyway? If your query returns more than, say, 300 rows (15 pages with 20 rows each), you probably should think about making the user provide additional search criteria first rather than blindly paging the output.
What about the other option--coupling the presentation layer directly to the database? It's easy to imagine an extension to displaytag that takes a SQL query rather than a Java collection, and handles sorting/paging natively by dynamically appending ORDER BY and LIMIT/OFFSET clauses. (Indeed, the MS toolset seems to actively encourage this pattern.)
This is fine for prototyping or simple apps that don't have much business logic beyond the basic CRUD transactions. But, this can rapidly become unmaintainable--not only are there many more places to touch if the DB schema changes, but you are also at risk of introducing bugs by accessing tables directly and potentially bypassing domain logic (business rules) tied to particular fields. Still, it may be worth considering tihs as a strategy for hand-optimizing queries with special performance requirements that outweigh maintainability, especially if the query is more relational than object-oriented in nature.
In summary, displaytag is a good, simple choice for many applications that need HTML tables with sorting and paging. There may be a performance hit, but a clean, maintainable design often is more
important.

4 Comments:
How about any SQL query makes 250,000 rows ?
It has 20 articles in a page, 20 page number in a footer page separator, total 625 page number.
If display-tag and it's source data doesn't work like Lazy style, It can't be used in a commercial web site or web portal, a kind of Naver or Daum in Korea.
By
gloomywoodpecker, at 1:55 AM
Is there any way by which i can jump to a page other than 1st page?
I want to show the same page after performing an action in the list. e.g. if i perform an action on page 15 then the same page should be displayed on the screen.
By
Vikrant Korde, at 3:57 AM
放鬆心情自由自在的來一趟花蓮旅遊,可以感受花蓮民宿各種不同的風味,所以來花蓮一定要住花蓮民宿哦!因為可從花蓮民宿主人那裡分享到不一樣的花蓮旅遊經驗及花蓮美食道地的花蓮小吃,所以來花蓮旅遊不一定就要享受花蓮高級餐廳的花蓮美食也可以多試試道地古早味的花蓮美味哦!來花蓮民宿可以放鬆整個心情,來花蓮住宿也可以放空自已,來花蓮也可以了解在地的文化哦!住民宿其實是很輕鬆的,住花蓮民宿其實是很容易,還有就是現在是高油價的時代,還自行開車來花蓮嗎?來花蓮租車會比較輕鬆哦!來花蓮房屋租車會比較省錢哦!或是可以請花蓮計程車帶您包車旅遊喔!!歡迎來住住美麗的花蓮民宿囉!!一定要讓您來花蓮旅遊並且讓您愛上花蓮民宿,還有團購美食好吃的蜂蜜蛋糕
By
Anonymous, at 12:18 PM
酒店打工
酒店兼職
台北酒店
打工兼差
酒店工作
酒店經紀
禮服店
酒店兼差
酒店上班
酒店PT
酒店
By
Anonymous, at 8:14 PM
Post a Comment
<< Home