SQL奇怪问题,某段数据Select比别的数据段速度慢!!
有相关表和字段
表A S_NO
表B S_NO、P_NO
表C P_NO、P_Date
里面有1~11月的相关数据
使用
select * from A表 where S_NO in(select S_NO from B表 where P_NO in (select P_NO from C表 where P_date between '2006-01-01' and '2006-11-30'))
这一个SQL语句得到结果的时间要比
select * from A表 where S_NO in(select S_NO from B表 where P_NO in (select P_NO from C表 where P_date between '2006-11-01' and '2006-11-30'))
这句得到结果的时间短得多.
不明白怎么回事
请各位指教一下.
有相关表和字段
表A S_NO
表B S_NO、P_NO
表C P_NO、P_Date
里面有1~11月的相关数据
使用
select * from A表 where S_NO in(select S_NO from B表 where P_NO in (select P_NO from C表 where P_date between '2006-01-01' and '2006-11-30'))
这一个SQL语句得到结果的时间要比
select * from A表 where S_NO in(select S_NO from B表 where P_NO in (select P_NO from C表 where P_date between '2006-11-01' and '2006-11-30'))
这句得到结果的时间短得多.
不明白怎么回事
请各位指教一下.
其他月份不会慢
奇怪的情况是
1.如果Between '2006-10-01' and '2006-11-30'的话
速度就很快了.时间花不了1秒.
2.如果是 Between '2006-11-01' and '2006-11-30'的话,
速度变慢.时间要花22秒
3.如果把C表11月的数据改为12月或别的月.速度变快.时间花不了1秒
先
Update C set P_Date=P_Date+30 where P_Date Between '2006-11-01' and '2006-11-30'
再
select * from A表 where S_NO in(select S_NO from B表 where P_NO in (select P_NO from C表 where P_date between '2006-12-01' and '2006-12-30'))
2 可以采用这个方法能快很多
select a.* from A表 a,B表 b,c表 c where a.S_NO=b.S_NO and b.P_NO=c.P_NO and c.P_date between '2006-01-01' and '2006-11-30'
select P_NO from C表 where P_date between '2006-01-01' and '2006-11-30'
与
select P_NO from C表 where P_date between '2006-11-01' and '2006-11-30'
是否速度差距很大。如果是,试试:
1
如果你的如果字段是字符型,改为 DateTime 型。如果是 Access 数据库,between '2006-11-01' and '2006-11-30' 要同时改为 between #2006-11-01# and #2006-11-30#2
建立一个基于日期字段的索引
*****************************************************************************
欢迎使用CSDN论坛阅读器 : CSDN Reader(附全部源代码)
http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html