日期:2014-05-18  浏览次数:20433 次

【100分】真正的疑难问题,如何解释加一个排序就超时??如何解决这个问题???
如题,下面语句:
SQL code

--全表数据很多大概有2千万条,
--符合检索条件的数据58条
--相关字段都有索引
select top 100 * from tb with(nolock)
 where obid=10100 
   and id >456789 
   and dateTime < '2012-01-16 00:00:00'
--下面的order by语句不加,秒出,加上,就铁定超时,
--请问为什么?如何解决?
 order by id



------解决方案--------------------
ID字段有索引?还是聚集索引?
------解决方案--------------------
探讨
引用:

ID字段有索引?还是聚集索引?

id是主键,自增长字段,聚集索引

------解决方案--------------------
试试?
SQL code

WITH t AS
(select * from tb with(nolock)
 where obid=10100 
   and id >456789 
   and dateTime < '2012-01-16 00:00:00')
SELECT TOP 100 *
FROM t
ORDER BY id

------解决方案--------------------
探讨
引用:

引用:
引用:

ID字段有索引?还是聚集索引?

id是主键,自增长字段,聚集索引

那你不排序应该也是按ID排着的啊.

不行,试过了,去掉就会有重复,导数据会失败

------解决方案--------------------
SET SHOWPLAN_TEXT ON
GO
select top 100 * from tb --with(nolock)
 where obid=10100 
and id >456789 
and dateTime < '2012-01-16 00:00:00'

select top 100 * from tb --with(nolock)
 where obid=10100 
and id >456789 
and dateTime < '2012-01-16 00:00:00'
--下面的order by语句不加,秒出,加上,就铁定超时,
--请问为什么?如何解决?
 order by id


执行给结果。
------解决方案--------------------
SQL code
因为你 加了 with(nolock)
 查询出来的数据是 ID 的倒序(相当做了一次隐形的排序),如果不加是按照 ID 的正序 出的。这个是我最近才发现的。
再加 ORDER BY ID 相当于再次排序 。故慢。
还跟你的磁盘和tempdb库大小增长速度有关。

------解决方案--------------------
探讨
引用:

SET SHOWPLAN_TEXT ON
GO
select top 100 * from tb --with(nolock)
where obid=10100
and id >456789
and dateTime < '2012-01-16 00:00:00'

select top 100 * from tb --with(nolock)
wh……

------解决方案--------------------
能不能按照这个倒序进行导数据呢?