Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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
Asp.net 如何以较低的性能执行大量数据库查询?_Asp.net_Database_Performance - Fatal编程技术网

Asp.net 如何以较低的性能执行大量数据库查询?

Asp.net 如何以较低的性能执行大量数据库查询?,asp.net,database,performance,Asp.net,Database,Performance,我有一个拍卖网站,不像ebay(大多数人都这么认为) 现在我的问题是:在开始页面上显示了很多拍卖(ASP.NET)。所有拍卖(比如说最小10,最大50)都有一个计时器。现在,如果计时器被重置(当有人出价时会发生什么),我几乎每秒钟都要查看一次拍卖(过滤器没有启动也没有结束) 因此,我将有一个查询,每秒返回大量内容,必须每秒更新10-50个文本框,并为每个访问该页面的用户更新这些文本框 有谁能解决性能好的问题吗?你只需要重置哪个文本框,其余的都可以使用gui定时器。 因此,您需要创建一个重置日志表

我有一个拍卖网站,不像ebay(大多数人都这么认为)

现在我的问题是:在开始页面上显示了很多拍卖(ASP.NET)。所有拍卖(比如说最小10,最大50)都有一个计时器。现在,如果计时器被重置(当有人出价时会发生什么),我几乎每秒钟都要查看一次拍卖(过滤器没有启动也没有结束)

因此,我将有一个查询,每秒返回大量内容,必须每秒更新10-50个文本框,并为每个访问该页面的用户更新这些文本框


有谁能解决性能好的问题吗?

你只需要重置哪个文本框,其余的都可以使用gui定时器。 因此,您需要创建一个重置日志表,并在每次有人重置(出价)时放置一行。 现在-每秒钟从lognumber>LAST\u NUMBER\u屏幕上已经得到的日志中选择*

您将获得仅更改项目的列表

我认为这可能是最有效的工作


只需确保删除此日志表中的旧记录即可。…

您只需获取已重置的文本框,其余的可以使用gui计时器。 因此,您需要创建一个重置日志表,并在每次有人重置(出价)时放置一行。 现在-每秒钟从lognumber>LAST\u NUMBER\u屏幕上已经得到的日志中选择*

您将获得仅更改项目的列表

我认为这可能是最有效的工作

只需确保从此日志表中删除旧记录。…

  • 页面上的所有计时器应每秒自动减少一个刻度(如果低于“显示秒数”阈值-否则每分钟更新一次分钟)。这是通过客户端javascript处理的,使用计时器触发更新
  • 每当拍卖的时间被重置(由于出价),它都会在数据库中以及服务器端缓存中更新。缓存用于存储时间重置、拍卖ID和拍卖的新结束时间
  • 大约每秒钟,页面向服务器发送一次Ajax请求(最好是JSON),请求所有拍卖ID和自上次页面请求以来时间已重置的所有拍卖的新结束时间(每次请求时存储在客户端的值)。根据返回值,客户端仅更新更新更新的拍卖。数据库只在初始页面加载时被查询-所有后续更新请求都会命中缓存
      • 页面上的所有计时器应每秒自动减少一个刻度(如果低于“显示秒数”阈值-否则每分钟更新一次分钟)。这是通过客户端javascript处理的,使用计时器触发更新
      • 每当拍卖的时间被重置(由于出价),它都会在数据库中以及服务器端缓存中更新。缓存用于存储时间重置、拍卖ID和拍卖的新结束时间
      • 大约每秒钟,页面向服务器发送一次Ajax请求(最好是JSON),请求所有拍卖ID和自上次页面请求以来时间已重置的所有拍卖的新结束时间(每次请求时存储在客户端的值)。根据返回值,客户端仅更新更新更新的拍卖。数据库只在初始页面加载时被查询-所有后续更新请求都会命中缓存

        • 您不必每秒查询SQL来更改文本框中的第二个计数器。您应该使用Javascript自动调整从开始时间算起剩下的秒数

          只有在重置SQL表中的开始时间后,才应更改开始时间。您可以使用AJAX更新文本框

          您不必每隔一秒钟查询一次SQL,以告知计时器何时被重置。您应该使用SQL缓存依赖项。基本上,SQL依赖项会告诉您其数据何时发生更改,而不是您必须每秒询问SQL是否有任何更改

          工作流程如下:

        • 使用SQL依赖项对象进行初始SQL查询,并指定“回调”方法
        • 缓存结果
        • 将缓存结果返回到页面,直到SQL数据发生更改
        • 当SQL数据发生更改时,SQL依赖项将调用您定义的“回调”方法
        • 回调方法清除SQL缓存,并重新查询SQL
        • 重复步骤2-6
        • 如果您不习惯使用委托和依赖项,这会有点复杂,但花时间是值得的,因为它消除了重复查询

          Sql缓存依赖项示例C#ASP.Net 2.0 在Global.aspx页面中,添加以下代码:

          <script runat="server">
          
              string mySqlConnection = "<<Enter your connection string here.>>";
              void Application_Start(object sender, EventArgs e)
              {
                  // Start subscribing to SQL Server 2005 Notification Services.
                  System.Data.SqlClient.SqlDependency.Start(mySqlConnection);
              }
          
              void Application_End(object sender, EventArgs e) 
              {
                  // Stop subscribing to SQL Server 2005 Notification Services.
                  System.Data.SqlClient.SqlDependency.Stop(mySqlConnection);
              }       
          
          </script>
          
          <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml" >
          <head runat="server">
              <title>Untitled Page</title>
          </head>
          <body>
              <form id="form1" runat="server">
              <div>
                  <asp:Label ID="lblCacheDisplay" runat="server" />
                  <br />
                  <asp:Label ID="lblWasQueryExecuted" runat="server" />
              </div>
              </form>
          </body>
          </html>
          
          using System;
          using System.Data;
          using System.Data.SqlClient;
          using System.Configuration;
          using System.Web;
          using System.Web.Security;
          using System.Web.UI;
          using System.Web.UI.WebControls;
          using System.Web.UI.WebControls.WebParts;
          using System.Web.UI.HtmlControls;
          
          public partial class _Default : System.Web.UI.Page 
          {    
              private static SqlDependency sqlDependency;
              private static bool cacheIsValid = false;            // cacheIsValid is set to "false" when the SQL data is changed.  
              private static string mySqlConnection = "<<Enter you connection string here.  It MUST use a different user than the Global.aspx connection string.>>";
          
              protected void Page_Load(object sender, EventArgs e)
              {
                  string myCachedData = (string)HttpContext.Current.Cache.Get("myCachedData");
                  if (myCachedData == null || !cacheIsValid)  // Remember that cached objects can be removed from the cache at any time by the garbage collector, you cannot assume that they exist!
                  {
                      myCachedData = GetMyDataFromSql();
                      cacheIsValid = true;
                      lblWasQueryExecuted.Text = "SQL was queried for this data.";
                  }
                  else
                  {
                      lblWasQueryExecuted.Text = "This data came from the Application-level cache.  It should be deleted if the SQL data changes.";
                  }
                  lblCacheDisplay.Text = myCachedData;        
              }
          
              public static string GetMyDataFromSql()
              {
                  string returnSqlData = String.Empty;
          
                  string storedProcedureName = "<<Enter you stored procedure name here.>>";
          
                  SqlCommand cmd = new SqlCommand();
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.CommandText = storedProcedureName;
                  cmd.Connection = new SqlConnection(mySqlConnection);
                  cmd.Connection.Open();
          
                  SqlDataReader sdr = cmd.ExecuteReader();        
                  if (sdr.Read())
                  {
                      returnSqlData = sdr.GetString(1);
                  }
          
                  // Only one Sql dependency needs to be created per application start.  Since each Sql dependency keeps a connection
                  // to the Sql database open at all times, we want to make sure we have only one Sql dependency at any given time.
                  if (!cacheIsValid)
                  {
                      HttpContext.Current.Cache.Remove("myCachedData");
                  }
                  object hasSqlDependency = HttpContext.Current.Cache.Get("myCachedData");
                  if (hasSqlDependency == null)
                  {
                      CreateSqlDependency();
                      HttpContext.Current.Cache.Add("myCachedData", returnSqlData, null, DateTime.MaxValue,
                              TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
                  }
          
                  cmd.Connection.Close();
          
                  return returnSqlData;
              }
          
              public static void SqlDependency_OnChange(Object sender, SqlNotificationEventArgs e)
              {
                  if (e.Type == SqlNotificationType.Change)
                  {
                      // The Sql data has changed, so the current cache is out-dated. Therefore, mark it as being invalid.
                      cacheIsValid = false;        // We do not have access to HttpContext.Current right now, so we cannot clear the cache ourselves right now.  Instead, we have to mark it as being invalid, and let other code update it.
          
                      // Recreate the Sql dependency, since it disappears after sqlDependency_OnChange is called.  This will keep the
                      // connection to the Sql database open, so we can continue to be notified if the SQL data changes.
                      CreateSqlDependency();            
                  }
              }
          
              private static void CreateSqlDependency()
              {        
                  SqlConnection sqlConn = new SqlConnection(mySqlConnection);
                  sqlConn.Open();
          
                  // If any tables in this query are modified (data changes, table definition changes, etc.), SqlDependency_OnChange will be called.  
                  SqlCommand cmdDependency = new SqlCommand("<<SELECT column FROM myTable>>", sqlConn);
                  sqlDependency = new SqlDependency(cmdDependency);
                  sqlDependency.OnChange += new OnChangeEventHandler(SqlDependency_OnChange);
          
                  // Even though we don't do anything with the results of this query, it still needs to be executed in order to set the Sql dependency.
                  // If you comment out this code, the Sql dependency will not be properly created, and SqlDependency_OnChange will never be notified of
                  // changes to the SQL data.
                  SqlDataReader objReader = cmdDependency.ExecuteReader();
                  objReader.Close();
                  sqlConn.Close();
              }
          }
          
          
          字符串mySqlConnection=“”;
          无效应用程序\u启动(对象发送方,事件参数e)
          {
          //开始订阅SQL Server 2005通知服务。
          System.Data.SqlClient.SqlDependency.Start(mySqlConnection);
          }
          无效应用程序\u结束(对象发送方,事件参数e)
          {
          //停止订阅SQL Server 2005通知服务。
          System.Data.SqlClient.SqlDependency.Stop(mySqlConnection);
          }       
          
          在Default.aspx页面中,添加以下代码:

          <script runat="server">
          
              string mySqlConnection = "<<Enter your connection string here.>>";
              void Application_Start(object sender, EventArgs e)
              {
                  // Start subscribing to SQL Server 2005 Notification Services.
                  System.Data.SqlClient.SqlDependency.Start(mySqlConnection);
              }
          
              void Application_End(object sender, EventArgs e) 
              {
                  // Stop subscribing to SQL Server 2005 Notification Services.
                  System.Data.SqlClient.SqlDependency.Stop(mySqlConnection);
              }       
          
          </script>
          
          <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml" >
          <head runat="server">
              <title>Untitled Page</title>
          </head>
          <body>
              <form id="form1" runat="server">
              <div>
                  <asp:Label ID="lblCacheDisplay" runat="server" />
                  <br />
                  <asp:Label ID="lblWasQueryExecuted" runat="server" />
              </div>
              </form>
          </body>
          </html>
          
          using System;
          using System.Data;
          using System.Data.SqlClient;
          using System.Configuration;
          using System.Web;
          using System.Web.Security;
          using System.Web.UI;
          using System.Web.UI.WebControls;
          using System.Web.UI.WebControls.WebParts;
          using System.Web.UI.HtmlControls;
          
          public partial class _Default : System.Web.UI.Page 
          {    
              private static SqlDependency sqlDependency;
              private static bool cacheIsValid = false;            // cacheIsValid is set to "false" when the SQL data is changed.  
              private static string mySqlConnection = "<<Enter you connection string here.  It MUST use a different user than the Global.aspx connection string.>>";
          
              protected void Page_Load(object sender, EventArgs e)
              {
                  string myCachedData = (string)HttpContext.Current.Cache.Get("myCachedData");
                  if (myCachedData == null || !cacheIsValid)  // Remember that cached objects can be removed from the cache at any time by the garbage collector, you cannot assume that they exist!
                  {
                      myCachedData = GetMyDataFromSql();
                      cacheIsValid = true;
                      lblWasQueryExecuted.Text = "SQL was queried for this data.";
                  }
                  else
                  {
                      lblWasQueryExecuted.Text = "This data came from the Application-level cache.  It should be deleted if the SQL data changes.";
                  }
                  lblCacheDisplay.Text = myCachedData;        
              }
          
              public static string GetMyDataFromSql()
              {
                  string returnSqlData = String.Empty;
          
                  string storedProcedureName = "<<Enter you stored procedure name here.>>";
          
                  SqlCommand cmd = new SqlCommand();
                  cmd.CommandType = CommandType.StoredProcedure;
                  cmd.CommandText = storedProcedureName;
                  cmd.Connection = new SqlConnection(mySqlConnection);
                  cmd.Connection.Open();
          
                  SqlDataReader sdr = cmd.ExecuteReader();        
                  if (sdr.Read())
                  {
                      returnSqlData = sdr.GetString(1);
                  }
          
                  // Only one Sql dependency needs to be created per application start.  Since each Sql dependency keeps a connection
                  // to the Sql database open at all times, we want to make sure we have only one Sql dependency at any given time.
                  if (!cacheIsValid)
                  {
                      HttpContext.Current.Cache.Remove("myCachedData");
                  }
                  object hasSqlDependency = HttpContext.Current.Cache.Get("myCachedData");
                  if (hasSqlDependency == null)
                  {
                      CreateSqlDependency();
                      HttpContext.Current.Cache.Add("myCachedData", returnSqlData, null, DateTime.MaxValue,
                              TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
                  }
          
                  cmd.Connection.Close();
          
                  return returnSqlData;
              }
          
              public static void SqlDependency_OnChange(Object sender, SqlNotificationEventArgs e)
              {
                  if (e.Type == SqlNotificationType.Change)
                  {
                      // The Sql data has changed, so the current cache is out-dated. Therefore, mark it as being invalid.
                      cacheIsValid = false;        // We do not have access to HttpContext.Current right now, so we cannot clear the cache ourselves right now.  Instead, we have to mark it as being invalid, and let other code update it.
          
                      // Recreate the Sql dependency, since it disappears after sqlDependency_OnChange is called.  This will keep the
                      // connection to the Sql database open, so we can continue to be notified if the SQL data changes.
                      CreateSqlDependency();            
                  }
              }
          
              private static void CreateSqlDependency()
              {        
                  SqlConnection sqlConn = new SqlConnection(mySqlConnection);
                  sqlConn.Open();
          
                  // If any tables in this query are modified (data changes, table definition changes, etc.), SqlDependency_OnChange will be called.  
                  SqlCommand cmdDependency = new SqlCommand("<<SELECT column FROM myTable>>", sqlConn);
                  sqlDependency = new SqlDependency(cmdDependency);
                  sqlDependency.OnChange += new OnChangeEventHandler(SqlDependency_OnChange);
          
                  // Even though we don't do anything with the results of this query, it still needs to be executed in order to set the Sql dependency.
                  // If you comment out this code, the Sql dependency will not be properly created, and SqlDependency_OnChange will never be notified of
                  // changes to the SQL data.
                  SqlDataReader objReader = cmdDependency.ExecuteReader();
                  objReader.Close();
                  sqlConn.Close();
              }
          }
          
          
          无标题页
          

          您不必每秒查询SQL来更改文本框中的第二个计数器。您应该使用Javascript自动调整从开始时间算起剩下的秒数

          只有在重置SQL表中的开始时间后,才应更改开始时间。您可以使用AJAX更新文本框

          您不必每隔一秒钟查询一次SQL,以告知计时器何时被重置。您应该使用SQL缓存依赖项。基本上,SQL依赖项会告诉您其数据何时发生更改,而不是您必须每秒询问SQL是否有任何更改