我们正在使用 SQL Server 2008 R2,并且有一个非常大(100M+ 行)的表,其中包含一个主 id 索引和一个datetime包含非聚集索引的列。我们看到了一些非常不寻常的客户端/服务器行为,这些行为基于专门针对索引日期时间列的order by子句的使用。
我通读了以下帖子:https : //stackoverflow.com/questions/1716798/sql-server-2008-ordering-by-datetime-is-too-slow 但客户端/服务器上发生的事情比实际发生的更多这里开始描述。
如果我们运行以下查询(编辑以保护某些内容):
select *
from [big table]
where serial_number = [some number]
order by test_date desc
Run Code Online (Sandbox Code Playgroud)
每次查询都会超时。在 SQL Server Profiler 中,对服务器执行的查询如下所示:
exec sp_cursorprepexec @p1 output,@p2 output,NULL,N'select * .....
Run Code Online (Sandbox Code Playgroud)
现在,如果您将查询修改为:
declare @temp int;
select * from [big table]
where serial_number = [some number]
order by test_date desc
Run Code Online (Sandbox Code Playgroud)
SQL Server Profiler 向服务器显示执行的查询如下所示,它立即工作:
exec sp_prepexec @p1 output, NULL, N'declare @temp int;select * …Run Code Online (Sandbox Code Playgroud)