有一张表,名为表A,字段order_seq为主键,现在有一条语句:
select *
from 表A where order_seq = (Select min(order_seq) From 表A Where down_flag = 'N' and cancel_flag = 'N')当表A的数据量很大时,此条语句的速度会迅速降低,请教高手有没有办法将上面的语句进行优化,提高查询的速度.

解决方案 »

  1.   

    这条件有索引没有?
    down_flag = 'N' and cancel_flag = 'N'
    如果选择性低,并且不需要频繁更新表A
    可考虑建立一个位置索引
      

  2.   

    不是“位置”是bitmap(位图)索引
    或者试试这个语句
    SELECT *
      FROM 表A a
     WHERE NOT EXISTS (SELECT NULL
              FROM 表A b
             WHERE down_flag = 'N' AND
                   cancel_flag = 'N' AND
                   b.order_seq < a.order_seq)
      

  3.   

    select colname1,... from (select colname1,...,min(order_seq) from 表A where down_flag = 'N' and cancel_flag = 'N' group by colname1,...)
      

  4.   

    我在字段down_Flag创建位图索引后,速度反而变慢;另外用NOT EXISTS语句速度也变慢了
      

  5.   

    我估计你要的是这个:select *
      from (
    select t.*, row_number()over(order by order_seq asc) rn
      from table_a  t
     where down_flag = 'N' and cancel_flag = 'N'
    )
     where  rn =1
      

  6.   

    7楼 要是order_seq的 min 对应的数据不是一条 那不挂啦
      

  7.   


    有一张表,名为表A,字段order_seq为主键其实是楼主是要这个:select *
      from (
    select t.*, rownum rn
      from table_a  t
     where down_flag = 'N' and cancel_flag = 'N'
     order by order_seq asc
    )
    where rn = 1
      

  8.   

    你这样循环使用“表A"当然慢了,直接使用这个就可以找到结果:
    Select * From 表A 
    Where down_flag = 'N' 
    and cancel_flag = 'N'如果你的是不同的表,表A和表B,使用exists会比你那个好,比in也好一点。表B有时间加上时间会更好。可参考如下:
    select * from 表A n
    where  exists (
        Select * From 表B m
        Where m.down_flag = 'N' 
        and m.cancel_flag = 'N'
        and m.order_seq=n.order_seq
        // time between to_date() and to_date()
        )
      

  9.   

    select *
      from (
    select t.*
      from table_a  t
     where down_flag = 'N' and cancel_flag = 'N'
     order by order_seq asc
    )
    where rownum = 1
      

  10.   

    用了codearts的方法,性能提升了50%以上