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

后端 未结 4 1992
北荒
北荒 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:11

    SELECT  NULL
    FROM    x
    WHERE   x.col_a = value_a
            AND x.col_b = value_b
            AND rownum = 1
    

    COUNT(*) is certainly not the best way since it will need to count all the rows, while ROWNUM = 1 returns as soon as it finds the first matching row.

    Here's the PL/SQL code:

    DECLARE
            ex INT;
    BEGIN
            BEGIN
                    SELECT  NULL
                    INTO    ex
                    FROM    dual
                    WHERE   1 = 1
                            AND rownum = 1;
                    DBMS_OUTPUT.put_line('found');
            EXCEPTION
            WHEN no_data_found THEN
                    DBMS_OUTPUT.put_line('not found');
            END;
    END;
    

提交回复
热议问题