How should I deal with null parameters in a PL/SQL stored procedure when I want to use them in comparisons?

前端 未结 6 1004
孤独总比滥情好
孤独总比滥情好 2021-01-05 14:31

I have a stored procedure with a parameter name which I want to use in a where clause to match the value of a column i.e. something like

         


        
6条回答
  •  囚心锁ツ
    2021-01-05 15:12

    If col1 is indexed, it would be best (performance-wise) to split the query in two:

    SELECT  *
    FROM    mytable
    WHERE   col1 = name
    UNION ALL
    SELECT  *
    FROM    mytable
    WHERE   name IS NULL AND col1 IS NULL
    

    This way, Oracle can optimize both queries independently, so the first or second part won't be actually executed depending on the name passed being NULL or not.

    Oracle, though, does not index NULL values of fields, so searching for a NULL value will always result in a full table scan.

    If your table is large, holds few NULL values and you search for them frequently, you can create a function-based index:

    CREATE INDEX ix_mytable_col1__null ON mytable (CASE WHEN col1 IS NULL THEN 1 END)
    

    and use it in a query:

    SELECT  *
    FROM    mytable
    WHERE   col1 = name 
    UNION ALL
    SELECT  *
    FROM    mytable
    WHERE   CASE WHEN col1 IS NULL THEN 1 END = CASE WHEN name IS NULL THEN 1 END
    

提交回复
热议问题