求助sqlserver顶尖高手:sqlserver分页语句执行了35分钟,求教

安德烈_T 2009-04-16 06:34:03
我用的是sqlserver2000.要对一张表的数据进行分页查询。
语句如下:

select count(*)
from mobileCollect where fdate between '2009-4-1' and '2009-4-15'--执行的结果是当前条件下有记录10654条。表中的总记录数是380445条
select top 15 * from
(select fcopcode,ftype,flevel,fmobile,fprovicecode,fcitycode,fnumtype,fimi,fdate,fid
from mobileCollect where fdate between '2009-4-1' and '2009-4-15' )
as searchR1 where searchR1.fid not in
(select top 15 searchR2.fid from
(select fid,fdate from mobileCollect where fdate between '2009-4-1' and '2009-4-15' )
as searchR2 order by searchR2.fdate desc ) order by searchR1.fdate desc--执行改分页语句,耗时35分钟50秒。

修改条件
select count(*)
from mobileCollect where fdate between '2009-2-1' and '2009-4-15'--将日期提前到2月1号。结果显示当前条件下数据总量78984条

select top 15 * from
(select
fcopcode,ftype,flevel,fmobile,fprovicecode,fcitycode,fnumtype,fimi,fdate,fid
from mobileCollect
where fdate between '2009-2-1' and '2009-4-15' )
as searchR1 where searchR1.fid not in
(select top 15 searchR2.fid
from
(select fid,fdate from mobileCollect where fdate between '2009-2-1' and '2009-4-15' )
as searchR2 order by searchR2.fdate desc ) order by searchR1.fdate desc--执行改条件下的分页程序耗时1秒。

这是为什么呢?

ps:我将查询的开始时间从4月14号开始往前调整。一直都是耗时很长。直到3月17号,才正常耗时1秒。3月17号的满足条件的数据总量是24131。
但是我再增加一天到3月18号,就不行了,又是耗时30分钟以上。3月18号满足条件的数据总量是22735。

求助啊
...全文
148 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ks_reny 2009-04-17
  • 打赏
  • 举报
回复
創建索引,查詢效率會大大提高.
htl258_Tony 2009-04-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 Andriivo 的回复:]
使用了索引是把问题解决了,但是在逻辑上怎么会出现那种问题呢?
[/Quote]
没有索引就会全表扫描,速度可以而知。
安德烈_T 2009-04-17
  • 打赏
  • 举报
回复
使用了索引是把问题解决了,但是在逻辑上怎么会出现那种问题呢?
  • 打赏
  • 举报
回复
1、在查询的字段fdate上建立索引,
2、对你的语句进行优化,里面使用了子查询,还进行连接,效率比较低。提高效率的见精华帖中内容
hwtong02 2009-04-16
  • 打赏
  • 举报
回复
支持
mugua604 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 Andriivo 的回复:]
没有索引啊
[/Quote]

那就是要在那你个字段上加上索引啊!
安德烈_T 2009-04-16
  • 打赏
  • 举报
回复
没有索引啊
mugua604 2009-04-16
  • 打赏
  • 举报
回复
估计是索引问题!
taoistong 2009-04-16
  • 打赏
  • 举报
回复
把上面的语句换一下就可以了,是我认为最潇洒的
taoistong 2009-04-16
  • 打赏
  • 举报
回复


declare @first binary(6)
select @first = first from sysindexes where id = object_id('fms_transport') and indid in(0,1)
declare @PageNum int
select @PageNum = convert(int, substring(@first,4,1) + substring(@first,3,1) +
substring(@first,2,1) + substring(@first,1,1) )
declare @FileNum int
select @FileNum = convert(int, substring(@first,6,1) + substring(@first,5,1))
select @FileNum, @PageNum

--执行DBCC Page
declare @sql varchar(1000)
select @sql = 'dbcc page (''' + db_name() + ''', ' + convert(varchar(10),@FileNum) + ', ' +
convert(varchar(10),@PageNum) + ', 1)'
select @sql
dbcc traceon(3604)
exec (@sql)


dbcc page ('eProcessDBTest', 1, 23496, 1)



哈哈 , 给你看个分页的

  • 打赏
  • 举报
回复
fdate 上有索引吗?
htl258_Tony 2009-04-16
  • 打赏
  • 举报
回复

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧