ORACLE的分組統計之ROLLUP(一)

Oracle 9i之後,擴展了group by 的功能,可以知足大部分多維數據的分析統計功能,主要表現:ide

1. rollup,cube,grouping sets 擴展group by字句提供了豐富的多維分組統計功能;函數

2. 3個擴展分組函數:grouping,grouping_id,group_id提供擴展group by的輔助功能:提供區別結果行屬於哪一個分組級別,區分NULL值,創建有意義的報表,對彙總結果排序,過濾結果行等;性能

3.對擴展group by容許按重複列分組,組合列分組,鏈接分組等,另外,grouping sets能夠接受cube,rollup操做做爲參數。spa

ROLLUPorm

沒有rollup的union all功能排序

SQL> select a.dname, b.job, sum(b.sal) sum_sal
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by a.dname, b.job
union all
select a.dname, NULL, sum(b.sa l) sum_sal
from scott.dept a, scott.emp b
where a.deptno = b.deptno
group by a.dnam e
union all
select null, null, sum(b.sal) sum_sal
from scott.dept a, scott.emp b wh ere a.deptno = b.deptno;

顯示結果:索引

DNAME          JOB          SUM_SAL
-------------- --------- ----------
SALES          MANAGER         2850
SALES          CLERK            950
ACCOUNTING     MANAGER         2450
ACCOUNTING     PRESIDENT       5000
ACCOUNTING     CLERK           1300
SALES          SALESMAN        5600
RESEARCH       MANAGER         2975
RESEARCH       ANALYST         6000
RESEARCH       CLERK           1900
ACCOUNTING                     8750
RESEARCH                      10875

DNAME          JOB          SUM_SAL
-------------- --------- ----------
SALES                          9400
                              29025

已選擇13行。
執行計劃
----------------------------------------------------------
Plan hash value: 3113041979

--------------------------------------------------------------------------------
----------
| Id  | Operation                      | Name    | Rows  | Bytes | Cost (%CPU)|
Time     |
--------------------------------------------------------------------------------
----------
|   0 | SELECT STATEMENT               |         |    19 |   479 |    13  (70)|
00:00:01 |
|   1 |  UNION-ALL                     |         |       |       |            |
         |
|   2 |   HASH GROUP BY                |         |    14 |   392 |     5  (20)|
00:00:01 |
|   3 |    NESTED LOOPS                |         |    14 |   392 |     4   (0)|
00:00:01 |
|   4 |     TABLE ACCESS FULL          | EMP     |    14 |   210 |     3   (0)|
00:00:01 |
|   5 |     TABLE ACCESS BY INDEX ROWID| DEPT    |     1 |    13 |     1   (0)|
00:00:01 |
|*  6 |      INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)|
00:00:01 |
|   7 |   HASH GROUP BY                |         |     4 |    80 |     5  (20)|
00:00:01 |
|   8 |    NESTED LOOPS                |         |    14 |   280 |     4   (0)|
00:00:01 |
|   9 |     TABLE ACCESS FULL          | EMP     |    14 |    98 |     3   (0)|
00:00:01 |
|  10 |     TABLE ACCESS BY INDEX ROWID| DEPT    |     1 |    13 |     1   (0)|
00:00:01 |
|* 11 |      INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)|
00:00:01 |
|  12 |   SORT AGGREGATE               |         |     1 |     7 |            |
         |
|* 13 |    TABLE ACCESS FULL           | EMP     |    14 |    98 |     3   (0)|
00:00:01 |
--------------------------------------------------------------------------------
----------

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

   6 - access("A"."DEPTNO"="B"."DEPTNO")
  11 - access("A"."DEPTNO"="B"."DEPTNO")
  13 - filter("B"."DEPTNO" IS NOT NULL)


統計信息
----------------------------------------------------------
          0  recursive calls
          0  db block gets
         53  consistent gets
          0  physical reads
          0  redo size
        780  bytes sent via SQL*Net to client
        385  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         13  rows processed
能夠看到,須要屢次訪問emp,dept表以及dept表的索引。

修改爲:ip

select a.dname, b.job, sum(b.sal) sum_sal
  from scott.dept a, scott.emp b
 where a.deptno = b.deptno
 group by rollup(a.dname, b.job);

結果以下:get

DNAME          JOB          SUM_SAL
-------------- --------- ----------
SALES          CLERK            950
SALES          MANAGER         2850
SALES          SALESMAN        5600
SALES                          9400
RESEARCH       CLERK           1900
RESEARCH       ANALYST         6000
RESEARCH       MANAGER         2975
RESEARCH                      10875
ACCOUNTING     CLERK           1300
ACCOUNTING     MANAGER         2450
ACCOUNTING     PRESIDENT       5000

DNAME          JOB          SUM_SAL
-------------- --------- ----------
ACCOUNTING                     8750
                              29025


  
  
  
  
執行計劃
----------------------------------------------------------
Plan hash value: 503922295

--------------------------------------------------------------------------------
---------
| Id  | Operation                     | Name    | Rows  | Bytes | Cost (%CPU)| T
ime     |
--------------------------------------------------------------------------------
---------
|   0 | SELECT STATEMENT              |         |    14 |   392 |     5  (20)| 0
0:00:01 |
|   1 |  SORT GROUP BY ROLLUP         |         |    14 |   392 |     5  (20)| 0
0:00:01 |
|   2 |   NESTED LOOPS                |         |    14 |   392 |     4   (0)| 0
0:00:01 |
|   3 |    TABLE ACCESS FULL          | EMP     |    14 |   210 |     3   (0)| 0
0:00:01 |
|   4 |    TABLE ACCESS BY INDEX ROWID| DEPT    |     1 |    13 |     1   (0)| 0
0:00:01 |
|*  5 |     INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)| 0
0:00:01 |
--------------------------------------------------------------------------------
---------

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

   5 - access("A"."DEPTNO"="B"."DEPTNO")


統計信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
         23  consistent gets
          0  physical reads
          0  redo size
        750  bytes sent via SQL*Net to client
        385  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
         13  rows processed

已選擇13行。

 

 

可見,使用rollup只須要訪問emp表一次,經過dept表的主鍵得到rowid表查詢相關行,比union all性能要好。 hash

相關文章
相關標籤/搜索