SQL Performance Issue ASP.Net App

时间:2015-11-13 13:52:48

标签: c# sql asp.net query-performance

我们在基于ASP.NET的Web应用程序中存在性能问题,当我们在早上登录时它只能看到一次。根据Log看起来如下查询需要超过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;
}

1 个答案:

答案 0 :(得分:0)

我找到了上面提到的我的性能问题的解决方案。问题是表格中的错误索引,我通过更改表格的索引来修复此问题,我们在生产中获取记录。

此外,我通过使用SQL数据适配器和使用语句修复了Framework级实体类。 App在生产中运行速度极快。谢谢你的帮助。

私有字符串ExecuteSqlNew(string sql)         {             string connectionString = ConfigurationManager.ConnectionStrings [" PBReportCS"]。ConnectionString;             string commandTimeOut = ConfigurationManager.AppSettings [" PBReportCommandTimeout"]。ToString();             DataSet result = new DataSet();             string pDate ="&#34 ;;

        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;

}