最佳解答者
Question about using row_number() for paging in sql 2008

問題
-
你好
我上網看了一些關於提升 asp.net website performance 的 article
當中他們提到使用 SQL 中的 ROW_NUMBER 去做PAGING.
我知道如果使用這毎功能我可以取到我想RETRIEVE 的 ROW RANGE
但如果我需要RETREIVE TOTAL NUMBER OF ROW
有沒有什麼功能我可以用 等我不用寫多一條QUERY 只是用來 RETRIEVE THE TOTAL NUMBER OF ROW呢?
E.G.
在某D situation 這個query 可能需要用3-5個join 的
如果我需要retrieve range of record 同個total number of record
我便要用同一條query 2 次
好似不太對咁...
希望大家可以比d 意見我
謝謝大家
解答
所有回覆
-
I think it is hard to do that by executing the query only once at server side. Base on your information, what I would do is to utilize Application State/Session State/Object Caching in ASP.NET. I will put all records to the ASP.NET application memory and do the filtering there.
Is there any suggestion from others?
-
Hi Raymond,
Thanks for your suggestion, Could you give me some more information about how to utilize Application State/Session State/Object Caching in ASP.NET please? So that I can have a look and see if it would work in my case.
The problem I had is I have too much data to retrieve back, it would be nice to only retrieve the range of records I need.
Thanks in advance. -
The idea is to maintain a data cache in your Web application so that you can retrieve the dataset directly from the memory between different page request and minimize the number of query to database. You can choose the follow technique for data caching in ASP.NET:
1. Session State - If you would like to maintain different dataset for each user session, you can choose session state since every user session is isolated from each other and the data cache cannot be shared among different user session.
2. Application State - If you would like to have one dataset shared among different user sessions, then you can choose application state. It is good to use if the backend data would update so often or even static and the memory resource used in application state is not a concern.
3. Object Caching - If you would like to have one dataset shared among different user sessions and the data needed to be refreshed, then you can use object caching. You can apply different object expiration policies when creating the object in the memory cache and you can also associate a dependency object to the cache so that you will receive a notification from database when data is being updated. Once you got the notification, you can execute your code to refresh the cached object.
You can still have the row_number in the query to populate your dataset so that you can apply manual paging in the presentation.
-
HI
Thanks for your information, the data will stored in session, does it mean it will store in RAM?, I has quite a lot of record to retrieve E.g. 50000 +, it may use quite a lot of the memory, when many user accessing the page.
Therefore I still prefer to use Row_Number, in that case I only retrieve 30 record at a time
From your suggestion if I stored the record into session in this case 50000 could you let me know when I should i am not quite sure when I should clear the session.
Thanks again. = ) -
By default, the session state will utilize local memory (memory on your web server) and in your case, I think applying filter at database level is better since you need to retrieve many records.
In addition, if the session has been idled for 20 minutes, it will automatically expire (you can change this setting the web.config file). Or you can call the method Session.Abandon() to expire the current session explicitly. However, please note that expiring a session does mean that the memory will be released immediately. The memory will still be occupied until garbage collection start.
-
Hi Raymond
Thanks very much for your information, I understood a lot more about session now.
The expiration time for the session has been set to 200 mins, I knew this is not good, unfortunately that is requested by many users.
If I want to apply the filter to at database, is there any way that I do not need to run the query twice, in order to retrieve the range of record I want and the total number of rows.
Thanks again.