2015-04-03 55 views
0

我使用簡單的分頁形式設置了此C#查詢,但其執行的某些查詢可能有數百萬行。我最近使用各種參數對其進行了測試,其中一些查詢在超過200,000條記錄時超時。我怎麼能限制讀者每次讀〜50,000行?Rest API API查詢超出行數太多,如何限制讀取器讀取太多行

public DataTable GetTranReport(string aliasName, string pageString, string year, string month) 
     { 
      DataTable dataTable = new DataTable(); 
      dataTable.Columns.Add(new DataColumn("recid")); 
      dataTable.Columns.Add(new DataColumn("folder")); 
      dataTable.Columns.Add(new DataColumn("cust")); 
      dataTable.Columns.Add(new DataColumn("direction")); 


      //pagination variables (pageString must be 1+ in order to represent current page) 
      int pageInt; 
      Int32.TryParse(pageString, out pageInt); 

      if (dbConnection5.State.ToString() != "Open") 
      { 
       dbConnection5.Open(); 
      } 


      int itemNum = 0; 
      string selecteddate = string.Format("[" + year + month + "]"); 

      string query = string.Format("SELECT recid, folder, cust, direction FROM " + selecteddate + " WHERE cust = @aliasname order by thedate DESC;"); 


      SqlCommand command = new SqlCommand(query, dbConnection5); 
      command.Parameters.AddWithValue("@selecteddate", selecteddate); 
      command.Parameters.AddWithValue("@aliasname", aliasname); 
      SqlDataReader reader = command.ExecuteReader(); 
      int i = 0; 
      DataRow newTRRow; 
      if (reader.HasRows) 
      { 


       while (reader.Read()) 
       { 
        ++i; 
        if (pageInt > 1) 
        { 
         if (i >= ((pageInt * 10) - 10) && i < (10 * pageInt)) 
         { 
          itemNum += 1; 
          string itemString = string.Format("itemString" + itemNum); 
          newTRRow = dataTable.NewRow(); 
          newTRRow["recid"] = reader["recid"]; 
          newTRRow["folder"] = reader["folder"]; 
          newTRRow["customer"] = reader["customer"]; 
          newTRRow["direction"] = reader["direction"]; 

          dataTable.Rows.Add(newTRRow); 

         } 
        } 
        else 
        { 
         if (itemNum < 10) 
         { 
          itemNum += 1; 
          string itemString = string.Format("itemString" + itemNum); 
          newTRRow = dataTable.NewRow(); 
          newTRRow["recid"] = reader["recid"]; 
          newTRRow["folder"] = reader["folder"]; 
          newTRRow["customer"] = reader["customer"]; 
          newTRRow["direction"] = reader["direction"]; 

          dataTable.Rows.Add(newTRRow); 

         } 
        } 

       } 

      } 

      dataTable.Rows.Add(string.Format("Total number of records is: {0}", i)); 
      reader.Close(); 
      dbConnection5.Close(); 
      return dataTable; 
     } 

回答

1

你需要添加2 extra parameter(你想要記錄的位置),並使用CTE具有row_number

假設你想從rcArcB記錄,你可以這樣做:

with cte as(
      SELECT recid, folder, cust, direction, 
      row_number() over(order by recid) rn 
      FROM your_table 
      where --your conditions (it's not need to use order by here) 
     ) 
select * from cte 
where rn between rcA and rcB 
+0

謝謝,這工作完美。 – 2015-04-04 02:55:17

+0

歡迎你 – jfun 2015-04-04 07:49:16

0

你可以限制你的號碼。 SQL查詢級別本身的行。只需使用TOP關鍵字來限制它。

string query = string.Format("SELECT TOP 50000 recid, folder, cust, direction FROM " + selecteddate + " WHERE cust = @aliasname order by thedate DESC;");