Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取大数据时导致连接时间执行超时错误的存储过程_C#_Sql Server_Asp.net Mvc_Entity Framework_Asp.net Core - Fatal编程技术网

C# 获取大数据时导致连接时间执行超时错误的存储过程

C# 获取大数据时导致连接时间执行超时错误的存储过程,c#,sql-server,asp.net-mvc,entity-framework,asp.net-core,C#,Sql Server,Asp.net Mvc,Entity Framework,Asp.net Core,我调用一个SQL Server存储过程来从数据库中获取大量记录,但它在运行时会导致超时执行错误。我们怎样才能用这种方式使它快速 我正在实体框架中使用存储过程调用。存储过程接受输入参数,输出参数是表中的总记录,用于在前面分页。我想获得每页10条记录的所有数据。数据库表名为Player\u account\u flow和store,还提供了存储过程名。当数据加载时,我正在映射我的db模型 using (SqlConnection conn = dbContext.Database.GetDbConn

我调用一个SQL Server存储过程来从数据库中获取大量记录,但它在运行时会导致超时执行错误。我们怎样才能用这种方式使它快速

我正在实体框架中使用存储过程调用。存储过程接受输入参数,输出参数是表中的总记录,用于在前面分页。我想获得每页10条记录的所有数据。数据库表名为Player\u account\u flow和store,还提供了存储过程名。当数据加载时,我正在映射我的db模型

using (SqlConnection conn = dbContext.Database.GetDbConnection() as SqlConnection)
{
    conn.Open();                    

    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = "Pro_GetPageData"; 
        cmd.CommandType = CommandType.StoredProcedure; 

        // input parameters to procedure
        cmd.Parameters.AddWithValue("@TableName", "Player_Account_Flow"); 
        cmd.Parameters.AddWithValue("@OrderString", "Create_Time Desc"); 
        cmd.Parameters.AddWithValue("@ReFieldsStr", "*");
        cmd.Parameters.AddWithValue("@PageIndex", 1); 
        cmd.Parameters.AddWithValue("@PageSize", 10); 
        cmd.Parameters.AddWithValue("@WhereString", query);                                        

        cmd.Parameters.AddWithValue("@TotalRecord", SqlDbType.Int); 
        cmd.Parameters["@TotalRecord"].Direction = ParameterDirection.Output; 

        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                var map = new PlayerAccountFlow();
                map.Id = (Int32)reader["Id"];
                map.AccountId = (Int32)reader["AccountId"];
                map.Type = (byte)reader["Type"];
                map.BeforeAmout = (decimal)reader["BeforeAmout"];
                map.AfterAmout = (decimal)reader["AfterAmout"];
                map.Amout = (decimal)reader["Amout"];
                map.Source = (Int32)reader["Source"];
                map.Memo = reader["Memo"].ToString();                            
                map.CreateTime = (DateTime)reader["Create_Time"];
                playerAccountFlowsList.Add(map);
            }
        }

        obj = cmd.Parameters["@TotalRecord"].Value;
    }
}

PagingData<PlayerAccountFlow> pagingData = new PagingData<PlayerAccountFlow>();
pagingData.Countnum = (Int32)obj;
pagingData.Data = playerAccountFlowsList;
return pagingData;

在这种情况下,您应该重新访问存储过程,例如多个联接。您还应该问问自己是否应该立即加载所有数据。我建议显示初始数据,并根据用户请求加载更多数据。这意味着您可以对数据库使用简单的计数,并根据结果计算页面,然后仅从选定的范围获取数据。

这里有很多内容需要研究。您可以设置比默认值更长的命令超时时间。您可以查看存储过程,看看它为什么运行得这么慢

对于CommandTimeout,只需以秒为单位给它一个值。在下面的例子中,这将是2分钟。尽管让他们等待几分钟来显示结果是一种糟糕的用户体验。我建议优化存储过程,首先查看索引、多个连接、子查询等。另外,您是在用户界面上显示那个巨大的数据集,还是将其拆分为页面?您可以将存储过程更改为仅返回当前页,并在用户转到下一页时再次调用它。当您只返回整个数据的一小部分时,编写良好的存储过程应该要快得多

using (SqlCommand cmd = new SqlCommand())
    {
        cmd.Connection = conn;
        cmd.CommandTimeout = 120;
        cmd.CommandText = "Pro_GetPageData"; 
        cmd.CommandType = CommandType.StoredProcedure; 




这里的任何人都面临这个问题,我需要有效的建议,可以帮助使用此存储过程快速从数据库中获取记录,或者在命令行cmd.CommandType=CommandType.StoredProcedure;,之后欣赏其他替代解决方案;,能否添加cmd.CommandTimeout=0;它的工作,我在几天前测试过这个,但是获取记录的速度太慢,这就是问题所在。我的记录很大,大约12438515。它在未来也会增加。因此速度不快。在这种情况下,您需要微调存储过程Pro_GetPageData。根据您最初的问题,我给出了解决超时问题的解决方案。是的,我感谢您给出了解决方案。我的存储过程在sql中手动运行时非常快。所以我认为这里的一些错误不是在SQLstored过程中,而是在SQLstored过程中非常快。我以前见过类似的情况。看看这个。还有这个,有一些好主意可以尝试