The fastest way to check if some records in a database table?

前端 未结 6 1447
耶瑟儿~
耶瑟儿~ 2020-12-14 22:44

I have a huge table to work with . I want to check if there are some records whose parent_id equals my passing value . currently what I implement this is by using \"select

6条回答
  •  佛祖请我去吃肉
    2020-12-14 23:46

    There's no real difference between:

    select 'y' 
      from dual 
     where exists (select 1 
                     from child_table 
                    where parent_key = :somevalue)
    

    and

    select 'y' 
      from mytable 
     where parent_key = :somevalue 
       and rownum = 1;
    

    ... at least in Oracle10gR2 and up. Oracle's smart enough in that release to do a FAST DUAL operation where it zeroes out any real activity against it. The second query would be easier to port if that's ever a consideration.

    The real performance differentiator is whether or not the parent_key column is indexed. If it's not, then you should run something like:

    select 'y' 
      from dual 
     where exists (select 1 
                     from parent_able 
                    where parent_key = :somevalue)
    

提交回复
热议问题