Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.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# 如何减少MVC中第一个db请求的响应时间_C#_Sql Server_Asp.net Mvc_Entity Framework_Query Performance - Fatal编程技术网

C# 如何减少MVC中第一个db请求的响应时间

C# 如何减少MVC中第一个db请求的响应时间,c#,sql-server,asp.net-mvc,entity-framework,query-performance,C#,Sql Server,Asp.net Mvc,Entity Framework,Query Performance,我的第一个数据库调用有时花费太长时间来呈现视图/页面。它大约需要10-12秒,然后是第二次,它的速度是500毫秒,然后是200毫秒。下面是我在我的项目中的几个案例中运行的代码示例。我正在使用存储过程。我的项目结构是默认的mvc结构 我已检查数据库中正在运行的StoredProcess。它平均需要200-300毫秒,那么为什么完整的页面需要大约12秒才能加载。是我的主机问题还是我如何诊断这个问题 using (WebMatrix.Data.Database db1 = WebMatrix.Data

我的第一个数据库调用有时花费太长时间来呈现视图/页面。它大约需要10-12秒,然后是第二次,它的速度是500毫秒,然后是200毫秒。下面是我在我的项目中的几个案例中运行的代码示例。我正在使用存储过程。我的项目结构是默认的mvc结构

我已检查数据库中正在运行的StoredProcess。它平均需要200-300毫秒,那么为什么完整的页面需要大约12秒才能加载。是我的主机问题还是我如何诊断这个问题

using (WebMatrix.Data.Database db1 = WebMatrix.Data.Database.Open("SQLTestDB"))
{
var PromoDetails = db1.Query("exec GetListingPromosV2 @0", id).ToList();
List<VMListingPromos> Lst = new List<VMListingPromos>();

foreach (var item in PromoDetails)
{
    VMListingPromos Listing_Promo = new VMListingPromos();
    Listing_Promo.PromoTitle = item.PromoName;
    Listing_Promo.Amount = item.Value;
    Listing_Promo.ExpirationDate = (item.ExpirationDate == null ? "NoDate" :     item.ExpirationDate.ToString("MM/dd/yyyy"));
    Listing_Promo.StartDate = (item.StartDate == null ? "NoDate" : item.StartDate.ToString("MM/dd/yyyy"));
    Listing_Promo.ListingPromoID = item.ID;
    Listing_Promo.status = item.status;
    Lst.Add(Listing_Promo);
}
return Lst;
}
使用(WebMatrix.Data.Database db1=WebMatrix.Data.Database.Open(“SQLTestDB”))
{
var PromoDetails=db1.Query(“exec GetListingPromosV2@0”,id).ToList();
List Lst=新列表();
foreach(PromoDetails中的var项目)
{
VMListingPromos Listing_Promo=新的VMListingPromos();
清单_Promo.promotTitle=item.promotName;
Listing_Promo.Amount=item.Value;
清单_Promo.ExpirationDate=(item.ExpirationDate==null?“NoDate”:item.ExpirationDate.ToString(“MM/dd/yyyy”);
清单_Promo.StartDate=(item.StartDate==null?“NoDate”:item.StartDate.ToString(“MM/dd/yyyy”);
Listing_Promo.ListingPromoID=item.ID;
清单_Promo.status=item.status;
第一次添加(上市促销);
}
返回Lst;
}

听起来您可能在第一次请求时没有打开的db连接池。您可以尝试通过连接字符串设置最小数量的连接池,这样您的应用程序将始终保持打开的连接

web.config中的连接字符串示例:

在SQL Server Management Studio中,您可以看到什么时间?好的,让我检查一下。db我的sp占用了200-300毫秒。好的,让我检查一下。对不起,我检查过了,这并不意味着有什么不同