Rest API SQL查询从太多行中超时,如何限制读者读取太多行

时间:2015-04-03 21:02:07

标签: c# sql

我使用简单的分页形式设置此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 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

你可以限制你的号码。 SQL查询级别本身的行数。只需使用TOP关键字来限制它。

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