SQL 性能问题 ASP.Net 应用

SQL Performance Issue ASP.Net App

我们基于 ASP.NET 的网络应用程序存在性能问题,并且仅在我们早上登录时可见一次。根据日志看起来像下面的查询需要超过 1 分 20 秒。下次用户登录应用程序并尝试访问同一页面时,不会发现任何问题。你能告诉我如何优化这个查询吗?有什么想法可以解决这个问题吗?

日志 -

"11/13/15","08:38:27","ExecuteSql - ---3---- ","8",""
"11/13/15","08:38:27","ExecuteSql - ---4---- : SQL : SELECT TOP 1 CONVERT(varchar(15), Period_End_Date, 107) as PDate FROM PBHISTORY..STATEMENT_OF_CHANGE 
                                               ORDER BY Period_End_Date DESC","8",""
"11/13/15","08:39:48","ExecuteSql - ---5---- ","8",""

SQL :

SELECT TOP 1 
    CONVERT(varchar(15), Period_End_Date, 107) as PDate 
FROM  
   PBHISTORY..STATEMENT_OF_CHANGE
ORDER BY 
   Period_End_Date DESC

C# ASP.NET -

public string GetDateRangeReportingDate(int reportId)
{
    var report = GetReportInfoById(reportId);
    string sql = string.Format(@"SELECT TOP 1 CONVERT(varchar(15), Period_End_Date, 107) as PDate FROM {0}..{1} ORDER BY Period_End_Date DESC", _historyDatabase, report.SourceTableName);

    var data = ExecuteSql(sql);

    while (data.Read())
    {
        return data["PDate"].ToString();
    }

    return null;
}

private SqlDataReader ExecuteSql(string sql)
{
     SqlDataReader reader;
     SqlConnection conn;

     var commandTimeOut = ConfigurationManager.AppSettings["PBReportCommandTimeout"].ToString();
     string connString = ConfigurationManager.ConnectionStrings["PBReportCS"].ConnectionString;

     conn = new SqlConnection(connString);
     conn.Open();

     SqlCommand cmd = new SqlCommand(sql, conn);
     cmd.CommandTimeout = int.Parse(commandTimeOut);

     reader = cmd.ExecuteReader();
     return reader;
}

我找到了上面提到的性能问题的解决方案。问题是 table 中的索引错误,我通过更改 table 的索引来解决这个问题,我们从那里获取生产记录。

此外,我通过使用 SQL 数据适配器和 using 语句修复了框架级实体 class。应用程序在生产环境中运行速度超快。谢谢你的帮助。

私有字符串ExecuteSqlNew(字符串sql) { 字符串 connectionString = ConfigurationManager.ConnectionStrings["PBReportCS"].ConnectionString; 字符串 commandTimeOut = ConfigurationManager.AppSettings["PBReportCommandTimeout"].ToString(); 数据集结果=新数据集(); 字符串 pDate = "";

        try
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    cmd.CommandTimeout = int.Parse(commandTimeOut);
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = cmd;
                    adapter.Fill(result);
                    adapter.Dispose();
                    conn.Close();
                    pDate = result.Tables[0].Rows[0]["PDate"].ToString();
                }
            }
        }
        catch(Exception ex)
        {
            throw ex;
        }
        return pDate;

}