有一张很大的表,表中有install_time,machine_id 等列,install_time为索引:
  我想查的数据是。。 今天产生的machine_id并且排重,在今天以前没有出现过的 machine_id数量。。
  下面是我的SQL语句。
select count(distinct t1.machine_id)
  from log_install t1
 where t1.install_time >=
       to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss')
   and t1.install_time < to_date('2008-1-31 0:0:0', 'yyyy-mm-dd hh24:mi:ss')
   and not exists (select *
            from log_install t2
           where t2.install_time <= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss')
             and t1.machine_id = t2.machine_id)大家帮帮忙啊,看看还怎么改善才能在提高下效率啊!!!!!!!!

解决方案 »

  1.   

    select count(distinct t1.machine_id) 
      from log_install t1 
     where t1.install_time >= 
           to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
       and t1.install_time  < to_date('2008-1-31 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
       and not exists (select '1'
                from log_install t2 
               where t2.install_time  <= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
                 and t1.machine_id = t2.machine_id) 这样子看看
      

  2.   

    尝试下
    --不在t2,在t1的不重复的machine_idselect count(distinct t1.machine_id) 
      from log_install t1 ,(select machine_id ,主键或者肯定不为null的字段 as title from log_install where install_time   <= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss')) t2 
     where t1.machine_id = t2.machine_id(+)
     and t1.install_time >= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
       and t1.install_time   < to_date('2008-1-31 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
       
       and t2.title is null
      

  3.   

    不错,关注中!!!警E卫安全技术论坛: 
    www.japee.com.cn/jew/pages/Main.aspx
      

  4.   

    关键是这个条件:
     and not exists (select * 
                from log_install t2 
               where t2.install_time  <= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
                 and t1.machine_id = t2.machine_id) 
    导致install_time是不会用上索引。
    如果有个数据表log_install_machine,可以记录log_install在<='2008-1-30 0:0:0'的不重复machine_id(可以在log_install建触发器对log_install_machine表进行更新),那么可以改成:select count(0) 
      from log_install t1 
           ,log_install_machine t2
     where t1.install_time >= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
       and t1.install_time  < to_date('2008-1-31 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
       and t1.machine_id <> t2.machine_id
      

  5.   

    select machine_id ,count(*) from
    (
    select t1.machine_id 
      from log_install t1 
     where t1.install_time >= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
       and t1.install_time  < to_date('2008-1-31 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
       and not exists (select machine_id 
                from log_install t2 
               where t2.install_time  <= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
                 and t1.machine_id = t2.machine_id)
    )
    group by machine_id;还有not exists (select machine_id 
                from log_install t2 
               where t2.install_time  <= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss') 
                 and t1.machine_id = t2.machine_id)
    对查询效率的影响也比较大,应该想办法改掉
      

  6.   


    建 machine_id 索引。
      

  7.   

    学习//select machine_id ,count(*) from 

    select t1.machine_id  
      from log_install t1  
     where t1.install_time >= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss')  
       and t1.install_time   < to_date('2008-1-31 0:0:0', 'yyyy-mm-dd hh24:mi:ss')  
       and not exists (select machine_id  
                from log_install t2  
               where t2.install_time   <= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss')  
                 and t1.machine_id = t2.machine_id) 

    group by machine_id; 还有not exists (select machine_id  
                from log_install t2  
               where t2.install_time   <= to_date('2008-1-30 0:0:0', 'yyyy-mm-dd hh24:mi:ss')  
                 and t1.machine_id = t2.machine_id) 
    对查询效率的影响也比较大,应该想办法改掉