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'))
这句得到结果的时间短得多.
不明白怎么回事
请各位指教一下.

解决方案 »

  1.   

    --现在各关键字段和c表时间字段建立索引,sql改为:
    select * from A表 as a where exists
     (select * from B表 b inner Join C表 as c on b.P_NO=c.P_NO
      where c.P_date between '2006-01-01' and '2006-11-30' and a.S_NO=b.S_NO)
      

  2.   

    between '2006-11-01' and '2006-11-30'
    用between时,最好加小时段'2006-11-30 23:59:59'
    '2006-11-30'等于'2006-11-30 00:00:00'
      

  3.   

    --用exists判断真假
    select * from A表 where exists
    (select 1 from B表 where S_NO=A表.S_NO and exists 
    (select 1 from C表 where P_NO=B表.P_NO and P_date between '2006-01-01' and '2006-11-30'))
    楼主可以测试一下速度
      

  4.   

    句子是一样的
    只是时间段不同
    只有11月份的数据Select的时候速度慢
    其他月份不会慢
    奇怪的情况是
    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'))
      

  5.   


    WangZWang(先来)
    的方法
    是快很多但
    我想知道
    我这个问题是什么搞出来的?
    很明显
    1~11月的数据
    总要比单独11月的数据多吧
    怎么会Select 1~11的速度要比 Select 11 的速度快呢?