union 優化方法
union 優化方法
union優化思路:
union=>remove duplicate=>sort=>index(don't remove sort)=>index fast full scan(read many index blocks once)
union總結:
返回值僅是索引列值的情況下,給返回值列加索引是可以優化union!
union and union all 簡介
union:合併結果集並去除重複(remove duplicate)記錄
union all:合併結果集並不去除重複記錄
實驗如下:
SQL> create table t_union as select * from dba_objects;
Table created.
SQL> alter table t_union modify object_id not null;
Table altered.
SQL> create table t_union_1 as select * from dba_objects;
Table created.
SQL> alter table t_union_1 modify object_id not null;
Table altered.
SQL> select object_id from t_union
2 union
3 select object_id from t_union_1;
81512 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 299238292
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 169K| 2153K| | 1460 (52)| 00:00:18 |
| 1 | SORT UNIQUE | | 169K| 2153K| 3352K| 1460 (52)| 00:00:18 |
| 2 | UNION-ALL | | | | | | |
| 3 | TABLE ACCESS FULL| T_UNION | 82154 | 1042K| | 325 (1)| 00:00:04 |
| 4 | TABLE ACCESS FULL| T_UNION_1 | 87476 | 1110K| | 325 (1)| 00:00:04 |
-----------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2332 consistent gets
0 physical reads
0 redo size
1185377 bytes sent via SQL*Net to client
60189 bytes received via SQL*Net from client
5436 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
81512 rows processed
=>TempSpc和1 sorts (memory)證明了union是需要排序的;UNION-ALL代表了我們執行合併操作<=
SQL> select object_id from t_union
2 union all
3 select object_id from t_union_1;
163023 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3974255051
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 169K| 2153K| 650 (51)| 00:00:08 |
| 1 | UNION-ALL | | | | | |
| 2 | TABLE ACCESS FULL| T_UNION | 82154 | 1042K| 325 (1)| 00:00:04 |
| 3 | TABLE ACCESS FULL| T_UNION_1 | 87476 | 1110K| 325 (1)| 00:00:04 |
--------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
13034 consistent gets
0 physical reads
0 redo size
2370239 bytes sent via SQL*Net to client
119963 bytes received via SQL*Net from client
10870 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
163023 rows processed
=>sorts值為0,也證明了union all不需要排序的;UNION-ALL代表了我們執行合併操作<=
給返回值列,新增索引看union and union all是否得到優化
SQL> create index t1 on t_union(object_id);
Index created.
SQL> create index t2 on t_union_1(object_id);
Index created.
SQL> select object_id from t_union
2 union
3 select object_id from t_union_1;
81512 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3228413298
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 169K| 2153K| | 919 (52)| 00:00:12 |
| 1 | SORT UNIQUE | | 169K| 2153K| 3352K| 919 (52)| 00:00:12 |
| 2 | UNION-ALL | | | | | | |
| 3 | INDEX FAST FULL SCAN| T1 | 82154 | 1042K| | 54 (0)| 00:00:01 |
| 4 | INDEX FAST FULL SCAN| T2 | 87476 | 1110K| | 54 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
378 consistent gets
0 physical reads
0 redo size
1185377 bytes sent via SQL*Net to client
60189 bytes received via SQL*Net from client
5436 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
81512 rows processed
1、索引優化了union ==>INDEX FAST FULL SCAN
2、索引不能消除union的排序 ==>1 sorts (memory) 、TempSpc
SQL> select object_id from t_union
2 union all
3 select object_id from t_union_1;
163023 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 937658497
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 169K| 2153K| 109 (51)| 00:00:02 |
| 1 | UNION-ALL | | | | | |
| 2 | INDEX FAST FULL SCAN| T1 | 82154 | 1042K| 54 (0)| 00:00:01 |
| 3 | INDEX FAST FULL SCAN| T2 | 87476 | 1110K| 54 (0)| 00:00:01 |
------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
11221 consistent gets
0 physical reads
0 redo size
2370239 bytes sent via SQL*Net to client
119963 bytes received via SQL*Net from client
10870 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
163023 rows processed
1、索引不能很好的優化union all
參考:《收穫,不止ORACLE》
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26442936/viewspace-775041/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL優化案例-union代替or(九)SQL優化
- Oracle優化案例-union代替or(九)Oracle優化
- MySQL union的一種優化MySql優化
- Oracle SQL語句優化之UNIONOracleSQL優化
- sql 優化過程之union 替換 orSQL優化
- Oracle優化案例-又見union代替or(二十)Oracle優化
- Oracle union all 不走索引的優化Oracle索引優化
- UNION效率比UNION ALL效率高——SQL優化之Everything is possibleSQL優化
- 【轉】UNION效率比UNION ALL效率高——SQL優化之Everything is possibleSQL優化
- 【SQL優化】UNION替換OR效率測試及總結SQL優化
- union all 最佳化案例
- Sql優化方法SQL優化
- Oracle優化方法Oracle優化
- 【NLP】常用優化方法優化
- MySQL 優化常用方法MySql優化
- Web 效能優化方法Web優化
- Oracle優化的方法Oracle優化
- 04 最優化方法優化
- 運籌優化(十三)--大規模優化方法優化
- SQL最佳化案例-union代替or(九)SQL
- 循序漸進調優union相關的sqlSQL
- java效能優化方案9——優化自定義hasCode()方法和equals()方法Java優化
- sql中union和union allSQL
- SQL優化的方法論SQL優化
- SQL優化常用方法11SQL優化
- SQL優化常用方法10SQL優化
- SQL優化常用方法16SQL優化
- SQL優化常用方法2SQL優化
- SQL優化常用方法5SQL優化
- SQL優化常用方法8SQL優化
- SQL優化常用方法3SQL優化
- SQL優化常用方法19SQL優化
- SQL優化常用方法20SQL優化
- SQL優化常用方法18SQL優化
- SQL優化常用方法12SQL優化
- SQL優化常用方法46SQL優化
- SQL優化常用方法52SQL優化
- SQL優化常用方法33SQL優化