Demonstrate the feasibility of DataReader Paging

Source: Internet
Author: User

I remember that it was one afternoon in. I was visiting the Internet and suddenly saw a piece of code, similar to the code above the landlord. The code was paged through DataReader. At that time, I was shocked. Is such code useless for a slightly larger system? As I understood at the time, while (dr. read (). If my system has millions of data records, it would take a long time for this while, and it would be too fast to transmit data. However, after my tests, the performance was very good, at least not as slow as we thought.

At that time, I used a 200-plus-W statistical table in our system for testing, just a simple select * from table, and then while traversing in the program, finally, we bound it to the GridView, which worked very well. I have a deep memory. During the day, in the company, the previous pages run well, and the page numbers that follow them, including the last pages, are of course the last pages. I am afraid that the system performance will be affected. When I got home at night, I tried it in the middle of the night, but it was not much different from the previous pages. At that time, I just wanted to test whether it was feasible or not, and I didn't use the notebook, but it should be returned within 5 seconds. In the preceding example, it should also look like 3, 4 seconds. It surprised me too much.

However, because the system framework uses the stored procedure and runs well, it has never been changed. That is to say, this paging solution is not actually used in projects under the real large data volume, but is often used in small projects.

For general systems, this generic paging solution is enough. For larger tables, you can use other methods, such as table sharding or other methods, to satisfy general applications.

Think about sending the code to the homepage, and it takes some time to supplement it.

The following is my test code:

Page: simple.

 
 
  1.     <asp:GridView ID="GridView1" runat="server"> 
  2.     </asp:GridView> 
  3.      
  4. <lcs:Pager ID="Pager1" runat="server" onpagechanged="Pager1_PageChanged" AlwaysShow="true" 
  5.     CurrentPageButtonPosition="Center"> 
  6. </lcs:Pager>  

Background code: it is also simple.

 
 
  1. private void BindRpt()  
  2. {  
  3.    int totalCount;  
  4.    double beg = DateTime.Now.Ticks;  
  5.    if (isDatareader)  
  6.    {  
  7.       GridView1.DataSource = LCS.Data.DbHelper.GetPager(  
  8.          Pager1.PageSize, Pager1.CurrentPageIndex, "Statistic", "*", "StatisticID", false, out totalCount, null, null); ;  
  9.    }  
  10.    else  
  11.    {  
  12.       totalCount = LCS.Data.DbHelper.GetCount("Statistic", "");  
  13.       GridView1.DataSource = LCS.Data.DbHelper.GetPager(  
  14.          Pager1.PageSize, Pager1.CurrentPageIndex, "Statistic", "*", "StatisticID", false, null);  
  15.    }  
  16.    Response.Write("
  17.     
  18.    GridView1.DataBind();  
  19.    Pager1.RecordCount = totalCount;  
  20. }  

The method implementation in my DbHelper is attached:

First, use datareader's

 
 
  1. Public static DataTable GetPager (int pageSize, int pageIndex,
  2. String tblName, string fldName, string fldSort, bool isDesc,
  3. Out int totalCount, string condition, params object [] parmsValues
  4. )
  5. {
  6. // Select * from talble where 11 = 1 order by limit desc
  7. // It is a standard SQL statement and does not need to be distinguished separately
  8. String SQL = "select" + fldName + "from" + tblName. ToString ()
  9. + (String. IsNullOrEmpty (condition ))? String. Empty: ("where 11 = 1" + condition ))
  10. + "Order by" + fldSort. ToString () + (isDesc? "DESC": "ASC ");
  11. Using (DbDataReader reader = ExecuteReader (SQL, parmsValues ))
  12. {
  13. DataTable dt = new DataTable ();
  14. Int fieldCount = reader. FieldCount;
  15. For (int I = 0; I <fieldCount; I ++)
  16. {
  17. DataColumn col = new DataColumn ();
  18. Col. ColumnName = reader. GetName (I );
  19. Col. DataType = reader. GetFieldType (I );
  20. Dt. Columns. Add (col );
  21. }
  22. TotalCount = 0;
  23. Int first = (pageIndex-1) * pageSize + 1;
  24. Int last = pageIndex * pageSize;
  25. While (reader. Read ())
  26. {
  27. TotalCount ++;
  28. If (totalCount> = first & last> = totalCount)
  29. {
  30. DataRow r = dt. NewRow ();
  31. For (int I = 0; I <fieldCount; I ++)
  32. {
  33. R [I] = reader [I];
  34. }
  35. Dt. Rows. Add (r );
  36. }
  37. }
  38. Return dt;
  39. }
  40. }

Let's look at the general:

 
 
  1. Public static DbDataReader GetPager (int pageSize, int pageIndex,
  2. String tblName, string fldName, string fldSort, bool isDesc, string condition)
  3. {
  4. Return ExecuteReader (Provider. GetPagerSql (pageSize, pageIndex, tblName, fldName, fldSort, isDesc, condition ));
  5. }
  6.  
  7. // I used an SQL string parameter formatting process internally, so there is a transit here.
  8.  
  9. Public static DbDataReader ExecuteReader (string format, params object [] parameterValues)
  10. {
  11. If (format = null | format. Length = 0) throw new ArgumentNullException ("commandText ");
  12. If (parameterValues! = Null) & (parameterValues. Length> 0 ))
  13. {
  14. // If a parameter exists, format the Parameter
  15. SQlParameterFormatter formatter = new SQlParameterFormatter ();
  16. Formatter. Provider = Provider;
  17. Formatter. Format (format, parameterValues );
  18. Return ExecuteReader (CommandType. Text, formatter. SQL, formatter. Parameters );
  19. }
  20. Else // directly out of use if no parameter exists
  21. {
  22. Return ExecuteReader (CommandType. Text, format, (DbParameter []) null );
  23. }
  24. }

// Finally, let's look at the method for generating paging SQL strings.

 
 
  1. public string GetPagerSql( int pageSize, int pageIndex,  
  2. string tblName,string fldName,string fldSort, bool isDesc,string condition)  
  3. {  
  4.    string strSort = isDesc ? " DESC" : " ASC";  
  5.    if (pageIndex == 1)  
  6.    {  
  7.       return "select top " + pageSize.ToString() + " " + fldName + " from " + tblName.ToString()  
  8.       + ((string.IsNullOrEmpty(condition)) ? string.Empty : (" where " + condition))  
  9.       + " order by " + fldSort.ToString() + strSort;  
  10.    }  
  11.    else  
  12.    {  
  13.       System.Text.StringBuilder strSql = new System.Text.StringBuilder();  
  14.       strSql.AppendFormat("select top {0} {1} from {2} ", pageSize,fldName, tblName);  
  15.       strSql.AppendFormat(" where {1} not in (select top {0} {1} from {2} ", pageSize * (pageIndex - 1),  
  16.       (fldSort.Substring(fldSort.LastIndexOf(',') + 1, fldSort.Length - fldSort.LastIndexOf(',') - 1)), tblName);  
  17.       if (!string.IsNullOrEmpty(condition))  
  18.       {  
  19.          strSql.AppendFormat(" where {0} order by {1}{2}) and {0}", condition, fldSort, strSort);  
  20.       }  
  21.       else  
  22.       {  
  23.          strSql.AppendFormat(" order by {0}{1}) ", fldSort, strSort);  
  24.       }  
  25.       strSql.AppendFormat(" order by {0}{1}", fldSort, strSort);  
  26.       return strSql.ToString();  
  27.    }  

Finally, for a connection to directly look at the results: http://jyt.dai8.net: 89/test_cb.aspx

Don't kill my computer.

After my tests, the conventional method is faster than that of datareader. If we look at the value alone, the gap is quite large, with a big difference of more than 10 times, A small one is 3 or 4 times worse, but it is enough for practicality. Most of the time, users do not feel it, especially those clients or internal use of the enterprise. Basically, there are no concurrent projects.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.