簡體   English   中英

Rest API SQL查詢從太多行中超時,如何限制讀取器讀取太多行

[英]Rest API SQL query times out from too many rows, how to limit reader from reading too many rows

我使用簡單的分頁形式設置了此C#查詢,但是它執行的某些查詢可能具有數百萬行。 我最近使用各種參數對其進行了測試,並且當有超過200,000條記錄時,某些查詢會超時。 如何限制閱讀器每次讀取約50,000行?

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;
        }

您需要添加2個extra parameter (所需記錄的位置),並使用具有row_numberCTE

假設您想要從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

您可以限制您的號碼。 SQL查詢級別本身的行數。 只需使用TOP關鍵字對其進行限制。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM