Quickest query to check for the existence of a row in Oracle?

后端 未结 4 1983
北荒
北荒 2021-02-05 22:31

I\'m using Oracle, and I have a very large table. I need to check for the existence of any row meeting some simple criteria. What\'s the best way to go about this using simple

4条回答
  •  死守一世寂寞
    2021-02-05 23:12

    I think using EXISTS gives a more natural answer to the question than trying to optimise a COUNT query using ROWNUM.

    Let Oracle do the ROWNUM optimisation for you.

    create or replace function is_exists (
            p_value_a varchar2,
            p_value_b varchar2)
            return boolean
    is
    
       v_exists varchar2(1 char);
    
    begin
    
        begin
            select 'Y' into v_exists from dual
            where exists
                (select 1 from x where x.col_a = p_value_a and x.col_b = p_value_a);
    
        exception
    
            when no_data_found then
    
                v_exists := null;
    
        end;
    
        return v_exists is not null;
    
    end is_exists;
    

提交回复
热议问题