NVL或EXCEPTION NO_DATA_FOUND

时间:2011-04-06 18:17:33

标签: sql performance oracle exception-handling plsql

我在一个程序中填写了以下sql的表

SELECT NVL(SUM(COL1), 0),
       NVL(SUM(COL2), 0)
  INTO v_mytable.COLUMN1,
       v_mytable.COLUMN2
  FROM t1, t2
 WHERE t1.id = t2.id
   AND t1.date = t2.date

另外,对于99%的表行,这些列= 0,并且在大多数情况下,当两个列都返回0时,此查询需要很长时间才能执行。

使用异常处理是否更好如下:

BEGIN
  SELECT SUM(COL1),
         SUM(COL2)
    INTO v_mytable.COLUMN1,
         v_mytable.COLUMN2
    FROM t1, t2
   WHERE t1.id = t2.id
     AND t1.date = t2.date
EXCEPTION WHEN NO_DATA_FOUND THEN
  v_mytable.COLUMN1 := 0 ;
  v_mytable.COLUMN2 := 0 ;
END;

感谢。

4 个答案:

答案 0 :(得分:7)

这两个块完全不同。如果COL1和/或COL2始终为NULL,则SELECT语句不会抛出NO_DATA_FOUND错误。它只会在v_mytable.COLUMN1v_mytable.COLUMN2中添加NULL。

你可以做到

SELECT SUM(COL1),
       SUM(COL2)
  INTO v_mytable.COLUMN1,
       v_mytable.COLUMN2
  FROM t1, t2
 WHERE t1.id = t2.id
   AND t1.date = t2.date

v_mytable.COLUMN1 := NVL( v_mytable.COLUMN1, 0 );
v_mytable.COLUMN2 := NVL( v_mytable.COLUMN2, 0 );

然而,我不希望这会更快。

答案 1 :(得分:3)

鉴于这两者之间的选择,我会选择第一个。

我更喜欢使用异常处理程序来处理真正的异常/错误,而不是控制流程。

因人而异。

答案 2 :(得分:2)

如果没有返回任何行,则抛出NO_DATA_FOUND,如果在从查询返回的实际行中返回空值,则抛出NO_DATA_FOUND。这将抛出NO_DATA_FOUND:

select sysdate
into myVariable
from dual
where 1=0;

这不会抛出NO_DATA_FOUND:

select null
into myVariable
from dual;

也就是说,如果你只是想知道col1和col2为null的行,那么你可以考虑在pl / sql中使用集合,并使用批量收集,如:

select sum(col1) as sum_col1, sum(col2) as sum_col2, col3
bulk collect into v_mytable
FROM t1, t2
 WHERE t1.id = t2.id
   AND t1.date = t2.date
   AND col1 is not null
   AND col2 is not null
GROUP by col3;

没有循环,一举做好。仅供参考,您可以设置v_mytable,例如:

declare
  type t_rec is record
  (col1_sum number,
   col2_sum number,
   col3 number);
  v_rec t_rec;

  type t_tab is table of v_rec%type;
  v_mytable t_tab;

begin
...

稍后你可以循环遍历v_mytable,它只是原始t1,t2连接结果的1%(由于查询中的附加非null子句)。

希望有所帮助。

答案 3 :(得分:2)

如果你停止加入col值为0的行,你的SQL会运行得更快。下面是一个小测试来证明我的观点。

首先创建两个包含100,000行的表,其中99%的行的col值设置为0:

SQL> create table t1 (id,date1,col1)
  2  as
  3   select level
  4        , trunc(sysdate)
  5        , case mod(level,100) when 42 then 42 else 0 end
  6     from dual
  7  connect by level <= 100000
  8  /

Table created.

SQL> create table t2 (id,date2,col2)
  2  as
  3   select level
  4        , trunc(sysdate)
  5        , case mod(level,100) when 42 then 84 else 0 end
  6     from dual
  7  connect by level <= 100000
  8  /

Table created.

提供基于成本的优化程序表统计信息:

SQL> exec dbms_stats.gather_table_stats(user,'t1')

PL/SQL procedure successfully completed.

SQL> exec dbms_stats.gather_table_stats(user,'t2')

PL/SQL procedure successfully completed.

运行查询时收集统计信息:

SQL> set serveroutput off
SQL> alter session set statistics_level = all
  2  /

Session altered.

现在您的查询运行如下:

SQL> SELECT NVL(SUM(t1.COL1), 0)
  2       , NVL(SUM(t2.COL2), 0)
  3    FROM t1
  4       , t2
  5   WHERE t1.id = t2.id
  6     AND t1.date1 = t2.date2
  7  /

NVL(SUM(T1.COL1),0) NVL(SUM(T2.COL2),0)
------------------- -------------------
              42000               84000

1 row selected.

SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'))
  2  /

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------------------------------
SQL_ID  6q5h7h8ht5232, child number 0
-------------------------------------
SELECT NVL(SUM(t1.COL1), 0)      , NVL(SUM(t2.COL2), 0)   FROM t1      , t2  WHERE t1.id = t2.id    AND
t1.date1 = t2.date2

Plan hash value: 446739472

-----------------------------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------
|   1 |  SORT AGGREGATE     |      |      1 |      1 |      1 |00:00:00.37 |     560 |       |       |          |
|*  2 |   HASH JOIN         |      |      1 |    100K|    100K|00:00:00.24 |     560 |  4669K|  1437K| 7612K (0)|
|   3 |    TABLE ACCESS FULL| T1   |      1 |    100K|    100K|00:00:00.01 |     280 |       |       |          |
|   4 |    TABLE ACCESS FULL| T2   |      1 |    100K|    100K|00:00:00.01 |     280 |       |       |          |
-----------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("T1"."ID"="T2"."ID" AND "T1"."DATE1"="T2"."DATE2")


21 rows selected.

你可以看到HASH JOIN需要加入100K行,这是花费大部分时间的地方。现在排除0值:

SQL> SELECT NVL(SUM(t1.COL1), 0)
  2       , NVL(SUM(t2.COL2), 0)
  3    FROM t1
  4       , t2
  5   WHERE t1.id = t2.id
  6     AND t1.date1 = t2.date2
  7     and t1.col1 != 0
  8     and t2.col2 != 0
  9  /

NVL(SUM(T1.COL1),0) NVL(SUM(T2.COL2),0)
------------------- -------------------
              42000               84000

1 row selected.

SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'))
  2  /

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------------------------------
SQL_ID  bjr7wrjx5tjvr, child number 0
-------------------------------------
SELECT NVL(SUM(t1.COL1), 0)      , NVL(SUM(t2.COL2), 0)   FROM t1      , t2  WHERE t1.id = t2.id    AND
t1.date1 = t2.date2    and t1.col1 != 0    and t2.col2 != 0

Plan hash value: 446739472

-----------------------------------------------------------------------------------------------------------------
| Id  | Operation           | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------
|   1 |  SORT AGGREGATE     |      |      1 |      1 |      1 |00:00:00.02 |     560 |       |       |          |
|*  2 |   HASH JOIN         |      |      1 |  25000 |   1000 |00:00:00.02 |     560 |  1063K|  1063K| 1466K (0)|
|*  3 |    TABLE ACCESS FULL| T1   |      1 |  50000 |   1000 |00:00:00.01 |     280 |       |       |          |
|*  4 |    TABLE ACCESS FULL| T2   |      1 |  50000 |   1000 |00:00:00.01 |     280 |       |       |          |
-----------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("T1"."ID"="T2"."ID" AND "T1"."DATE1"="T2"."DATE2")
   3 - filter("T1"."COL1"<>0)
   4 - filter("T2"."COL2"<>0)


23 rows selected.

你可以看到HASH JOIN现在只需加入1000行,导致输出速度更快。

希望这有帮助。

的问候,
罗布。