试下,这样执行时间select distinct a.* from (select  t.recordid,
              t.suid,
              t.distance,
              t.empty_distance,
              t.price
from counter_single t
where t.recordid >0
and t.utc >= 1109606400
and t.utc <= 1113953400) a

解决方案 »

  1.   

    谢谢回复!执行时间为:30.254秒对于这个数量级的查询,最好的解决方法是避免使用distinct关键字,以避免sort unique这样的耗费资源操作相信有替代distinct的方案。
      

  2.   

    用了Distinct后你的索引将失效
      

  3.   

    select  a.* from (select  t.recordid,
                  t.suid,
                  t.distance,
                  t.empty_distance,
                  t.price
    from counter_single t
    where t.recordid >0
    and t.utc >= 1109606400
    and t.utc <= 1113953400 group by  t.recordid,
                  t.suid,
                  t.distance,
                  t.empty_distance,
                  t.price) a我不知道是否可以改善,你可以试试
      

  4.   

    select counter_single.* from counter_single where rowid in (select max(rowid) from counter_single group by recordid);
    试试这个
      

  5.   

    select t.* from counter_single t where rowid in (select max(rowid) from counter_single group by recordid) and t.recordid >0 and t.utc >= 1109606400 and t.utc <= 1113953400;不好意思,刚才没把条件写全,用到rowid可能会快点
      

  6.   

    recordid和SUID,不是主键吗?
    不知道你想完成什么功能,
      

  7.   

    以下语句也许能满足你的需要:
    select t.recordid,
                  t.suid,
                  t.distance,
                  t.empty_distance,
                  t.price
    from counter_single t
    where t.recordid >0
    and t.rowid=(select max(rowid) from counter_single b 
    where t.recordid=b.recordid
    and t.suid = b.suid
    and t.distance=b.distance
    and t.empty_distance=b.empty_distance
    and t.price=b.price
    )
    and t.utc >= 1109606400
    and t.utc <= 1113953400
      

  8.   

    select * from counter_single ywhere y.suid>0 and y.rowid in ( select max(rowid) from counter_single t
    where t.recordid >0
    and t.utc >= 1109606400
    and t.utc <= 1113953400group by      t.recordid,
                  t.suid,
                  t.distance,
                  t.empty_distance,
                  t.price)
      

  9.   

    500万条数据,数据量太大了,使用distinct以后,又不能用索引,
    对于这样的海量数据,要想解决效率问题,可能就要另想办法了,
    建一个实体化视图(物化视图)的话,我想能满足你的要求,
    在基础表插入,删除,更新时自动更新视图,
    不过这就要对插入,删除,更新操作带来负面的影响.
    鱼和熊掌....只有看你怎么取舍了.
      

  10.   

    select t.recordid,
                  t.suid,
                  t.distance,
                  t.empty_distance,
                  t.price
    from counter_single t
    where t.recordid >0
    and t.rowid=(select max(rowid) from counter_single b 
    where t.recordid=b.recordid
    and t.suid = b.suid
    and t.distance=b.distance
    and t.empty_distance=b.empty_distance
    and t.price=b.price
    )
    and t.utc >= 1109606400
    and t.utc <= 1113953400
      

  11.   

    distinct关键字不只是对你要查询的第一列进行筛选,而是对你要查询的每一列进行筛选,再根据记录重组,这样当然会慢。
    不知道你想去掉那列或那几列的重复数据?
    如果是想去掉五列的重复数据,那就忍了吧!
      

  12.   

    用rowid不会带来任何性能上的改进.
    谁说distinct不会进行索引扫描了?我支持 railgunman(堕落男人) 的建议!另外楼主,请把你的执行计划以及帖出来.以及oracle版本号.
    为了方便大家帮你看.
      

  13.   

    utc上建立索引吧。
    另外,distinct是可以利用索引的。它是先利用索引,然后再在结果视图中排除重复。