一个名为ed2表中有10万条数据.
实现的功能: 查找有没id=20001的记录.
有2种方法:
1:select * from ed2 where id=200012:
用一个循环(模拟)
select id from ed2 
将每次取出的id值与20001比较我想请问一下,这两种方法哪个会快点,原因为什么呢?

解决方案 »

  1.   

    如果id上有索引,应该是2,因为系统不用去从索引找对应数据块的rowid得到其它字段信息,从索引上就得到了id.
    但开游标,游标循环,关游标也耗资源.也不好说.
      

  2.   

    set timing on
    然后试试看不就知道了
      

  3.   

    1:select * from ed2 where id=20001
    这个找有没有id=20001的好,找不到则为空
    也可以这样:select count(id) from ed2 where id=20001
    这样的话,没找到就是0,
      

  4.   

    我选1,不过可以改进一下,例如:
    select count(*) from dual where exists (select id from ed2 where id=20001)如果id上有索引,就看执行计划是否使用到了索引,如果没有,就加索引提示。最好是用index_ffs提示,只扫描索引不扫描表。
    我觉得用游标更耗资源、时间。
      

  5.   

    select /*+ index_ffs(a idx_de2_id) */ count(*) from dual where exists (select a.id from ed2 a where a.id=20001)