Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

oracle

plsql

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 SQL?

Here's my best guess, and while it may turn out to be fast enough for my purposes, I'd love to learn a canonical way to basically do SQL Server's "exists" in Oracle:

select count(x_id) from x where x.col_a = value_a and x.col_b = value_b;

The count() would then be returned as a boolean in another tier. The main point is that I want Oracle to do the bare minimum for this query - I only need to know if there are any rows matching the criteria.

And yes, those columns will most definitely be indexed.

like image 255
Josh Kodroff Avatar asked Jul 06 '09 17:07

Josh Kodroff


People also ask

How to test for the existence of rows in Oracle?

Summary: in this tutorial, you will learn how to use the Oracle EXISTS operator to test for the existence of rows. The Oracle EXISTS operator is a Boolean operator that returns either true or false. The EXISTS operator is often used with a subquery to test for the existence of rows:

How do you check if a row exists in SQL?

The EXISTS operator is often used with a subquery to test for the existence of rows: SELECT * FROM table_name WHERE EXISTS (subquery); Code language: SQL (Structured Query Language) (sql) The EXISTS operator returns true if the subquery returns any rows, otherwise, it returns false.

How to check if a record exists in Oracle PL/SQL?

Check if record exists using the Count () function. The following Oracle PL/SQL block will use the count() function in implicit cursor to count the records for particular criteria. If the count is greater than 0 means, the records exist else not exist.

How do you check for existence in SQL?

The Oracle EXISTS operator is a Boolean operator that returns either true or false. The EXISTS operator is often used with a subquery to test for the existence of rows: SELECT * FROM table_name WHERE EXISTS (subquery); The EXISTS operator returns true if the subquery returns any rows, otherwise, it returns false.


2 Answers

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;
like image 190
Nick Pierpoint Avatar answered Nov 23 '22 23:11

Nick Pierpoint


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;
like image 25
Quassnoi Avatar answered Nov 23 '22 22:11

Quassnoi