【问题标题】:How to decrease response time of first db request in MVC如何减少 MVC 中第一个 db 请求的响应时间
【发布时间】:2017-05-26 08:12:40
【问题描述】:

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

我已经检查了在数据库中运行 StoredProcedure。平均需要 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;
}

【问题讨论】:

  • 您在 SQL Server Management Studio 中获得的时间是什么?
  • 好的,请让我检查一下
  • 在 db 中我的 sp 需要 200-300 毫秒

标签: c# sql-server asp.net-mvc entity-framework query-performance


【解决方案1】:

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

web.config 中的示例连接字符串: &lt;add name="WriteConnection" connectionString="Data Source=my-server-name;Database=my_database;User ID=user;Password=password;Application Name=app_name;Min Pool Size=1" providerName="System.Data.SqlClient" /&gt;

参考:https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-1.1/8xx3tyca(v=vs.71)

【讨论】:

  • 好的,让我检查一下
  • 对不起,但我检查了它并没有太大的区别
猜你喜欢
  • 1970-01-01
  • 2021-07-16
  • 2011-12-30
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 2021-03-27
相关资源
最近更新 更多