[20170424]函式COALESCE優於NVL.txt

lfree發表於2017-04-24

[20170424]函式COALESCE優於NVL.txt

--//以前如果某個欄位為NULL,在一些運算要給它賦值,比如0,實際上開發(包括自己更喜歡使用nvl),實際上COALESCE更加優於nvl,
--//參考連結做一個例子:
http://nimishgarg.blogspot.com/2015/01/why-prefer-coalesce-over-nvl.html

1.環境:
SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SCOTT@book> select nvl(1,2),nvl(null,2) from dual ;
  NVL(1,2) NVL(NULL,2)
---------- -----------
         1           2

SCOTT@book> select COALESCE(1,2),COALESCE(null,2) from dual ;
COALESCE(1,2) COALESCE(NULL,2)
------------- ----------------
            1                2

--//可以發現2者一樣,完全可以替換。COALESCE能支援多個引數,例子:
SCOTT@book> select COALESCE(1,null,2),COALESCE(null,null,2) from dual ;
COALESCE(1,NULL,2) COALESCE(NULL,NULL,2)
------------------ ---------------------
                 1                     2

create or replace function f_null return number
is
begin
   dbms_lock.sleep(1);
   return 0;
end;
/

2.測試:
SCOTT@book> select count(*) from emp where comm is null ;
  COUNT(*)
----------
        10
--//有10條comm is null的記錄。

SCOTT@book> set timing on
SCOTT@book> select emp.*,nvl(emp.comm,f_null) from emp;
     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO NVL(EMP.COMM,F_NULL)
---------- ---------- --------- ---------- ------------------- ---------- ---------- ---------- --------------------
      7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20                    0
      7499 ALLEN      SALESMAN        7698 1981-02-20 00:00:00       1600        300         30                  300
      7521 WARD       SALESMAN        7698 1981-02-22 00:00:00       1250        500         30                  500
      7566 JONES      MANAGER         7839 1981-04-02 00:00:00       2975                    20                    0
      7654 MARTIN     SALESMAN        7698 1981-09-28 00:00:00       1250       1400         30                 1400
      7698 BLAKE      MANAGER         7839 1981-05-01 00:00:00       2850                    30                    0
      7782 CLARK      MANAGER         7839 1981-06-09 00:00:00       2450                    10                    0
      7788 SCOTT      ANALYST         7566 1987-04-19 00:00:00       3000                    20                    0
      7839 KING       PRESIDENT            1981-11-17 00:00:00       5000                    10                    0
      7844 TURNER     SALESMAN        7698 1981-09-08 00:00:00       1500          0         30                    0
      7876 ADAMS      CLERK           7788 1987-05-23 00:00:00       1100                    20                    0
      7900 JAMES      CLERK           7698 1981-12-03 00:00:00        950                    30                    0
      7902 FORD       ANALYST         7566 1981-12-03 00:00:00       3000                    20                    0
      7934 MILLER     CLERK           7782 1982-01-23 00:00:00       1300                    10                    0
14 rows selected.
Elapsed: 00:00:14.02
--//14秒,基本每條1秒,實際上從這裡就可以看出nvl函式的缺陷,沒有采用斷路判斷,執行14次f_null函式。

SCOTT@book> select emp.*,COALESCE(emp.comm,f_null) from emp;
     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO COALESCE(EMP.COMM,F_NULL)
---------- ---------- --------- ---------- ------------------- ---------- ---------- ---------- -------------------------
      7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20                         0
      7499 ALLEN      SALESMAN        7698 1981-02-20 00:00:00       1600        300         30                       300
      7521 WARD       SALESMAN        7698 1981-02-22 00:00:00       1250        500         30                       500
      7566 JONES      MANAGER         7839 1981-04-02 00:00:00       2975                    20                         0
      7654 MARTIN     SALESMAN        7698 1981-09-28 00:00:00       1250       1400         30                      1400
      7698 BLAKE      MANAGER         7839 1981-05-01 00:00:00       2850                    30                         0
      7782 CLARK      MANAGER         7839 1981-06-09 00:00:00       2450                    10                         0
      7788 SCOTT      ANALYST         7566 1987-04-19 00:00:00       3000                    20                         0
      7839 KING       PRESIDENT            1981-11-17 00:00:00       5000                    10                         0
      7844 TURNER     SALESMAN        7698 1981-09-08 00:00:00       1500          0         30                         0
      7876 ADAMS      CLERK           7788 1987-05-23 00:00:00       1100                    20                         0
      7900 JAMES      CLERK           7698 1981-12-03 00:00:00        950                    30                         0
      7902 FORD       ANALYST         7566 1981-12-03 00:00:00       3000                    20                         0
      7934 MILLER     CLERK           7782 1982-01-23 00:00:00       1300                    10                         0
14 rows selected.
Elapsed: 00:00:10.02

--//10秒,實際上從這裡就可以看出函式COALESCE採用斷路判斷,comm存在10條null的記錄,呼叫10次f_null函式。
--//我這個例子不是很好,null很多,不過一樣能說明問題,如果大量呼叫COALESCE優勢就很明顯了。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2137853/,如需轉載,請註明出處,否則將追究法律責任。

相關文章