1

This is not a question about optimizing a SQL command. I'm wondering what ways there are to ensure that a SQL connection is kept open and ready to handle a command as efficiently as possible.

What I'm seeing right now is I can execute a SQL command and that command will take ~1s, additional executions will take ~300ms. This is after the command has previously been executed against the SQL server (from another application instance)... so the SQL cache should be fully populated for the executed query prior to this applications initial execution. As long as I continually re-execute the query I see times of about 300ms, but if I leave the application idle for 5-10 minutes and return the next request will be back to ~1s (same as the initial request).

Is there a way to via the connection string or some property on the SqlConnection direct the framework to keep the connection hydrated and ready to efficiently handle queries?

4

5 回答 5

2

您是否检查过您的程序的执行计划。我相信执行计划被加载到服务器上的内存中,然后在特定时间段后或根据过程中访问的表等被清除。我们遇到过一些案例,简化存储过程(可能拆分它们)减少了数据库服务器在计算计划时必须做的工作量......并最终减少了第一次调用过程的时间......您可以发出命令每次强制存储过程重新编译以测试您是否正在减少初始调用时间......我们遇到过存储过程的复杂性使得数据库服务器必须不断地根据不同的参数重新编译的情况,这大大减慢了它的速度,

其他想法可能会间歇性地调用一个简单的 getDate() 或类似的方法,以便 sql 服务器处于唤醒状态(希望这是有道理的)......与将 asp.net 应用程序保存在 IIS 的内存中非常相似。

于 2009-07-03T09:45:53.430 回答
2

.NET 连接池中打开的连接的默认值为零。

您可以将连接字符串中的此值调整为 1 或更多:

"data source=dbserver;...Asynchronous Processing=true;Min Pool Size=1"

在 MSDN 中查看有关这些选项的更多信息。

于 2009-07-06T04:31:46.073 回答
0

默认情况下,连接池在 ADO .NET 中启用。这将通过应用程序使用的连接字符串。将连接池与 SQL Server 结合使用中的更多信息

于 2009-07-03T09:47:17.187 回答
0

如果您使用多个数据库连接,它可能会更有效。拥有一个数据库连接意味着最好的访问速度总是会受到顺序限制。而拥有 >1 个连接意味着您的编译器有机会进一步优化并发访问。我猜你正在使用.NET?

此外,如果您重复发出相同的 SQL 语句,则您的数据库服务器可能会在短时间内缓存结果,因此可以更快地返回结果集。

于 2009-07-03T09:49:24.077 回答
0

你通过不关闭它来保持它打开。:) 但这是不可取的,因为连接池将为您处理连接管理。你启用了吗?

于 2009-07-03T09:40:29.823 回答