SUM優化(複合索引)
SUM優化(複合索引)
SUM聚合函式優化方法:
SUM列無NULL值 => SUM列增加索引 => INDEX FAST FULL SCAN
SUM列有NULL值 => SUM列增加複合索引(column,1) => INDEX FAST FULL SCAN
SUM列無NULL值 => SUM列增加索引 => INDEX FAST FULL SCAN
SUM列有NULL值 => SUM列增加複合索引(column,1) => INDEX FAST FULL SCAN
複合索引優化SUM聚合函式注意事項:
NULL值雖然不影響SUM結果,但是NULL值會影響索引的使用(因為NULL值列的索引值不會被儲存),複合索引列值不全為NULL時,NULL值可以被儲存,也就是這個時候可以使用索引
NULL值雖然不影響SUM結果,但是NULL值會影響索引的使用(因為NULL值列的索引值不會被儲存),複合索引列值不全為NULL時,NULL值可以被儲存,也就是這個時候可以使用索引
DOUDOU@doudou1> drop table test1 purge;
Table dropped.
DOUDOU@doudou1> create table test1 as select * from user_objects;
Table created.
DOUDOU@doudou1> insert into test1(object_id) values('');
1 row created.
--object_id is null values
DOUDOU@doudou1> select count(*) from test1 where object_id is null;
DOUDOU@doudou1> select count(*) from test1 where object_id is null;
COUNT(*)
----------
20480
----------
20480
--object_id is not null values
DOUDOU@doudou1> select count(*) from test1 where object_id is not null;
DOUDOU@doudou1> select count(*) from test1 where object_id is not null;
COUNT(*)
----------
262144
----------
262144
DOUDOU@doudou1> select sum(object_id) from test1;
Execution Plan
----------------------------------------------------------
Plan hash value: 3896847026
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 821 (1)| 00:00:10 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
| 2 | TABLE ACCESS FULL| TEST1 | 258K| 3281K| 821 (1)| 00:00:10 |
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 821 (1)| 00:00:10 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
| 2 | TABLE ACCESS FULL| TEST1 | 258K| 3281K| 821 (1)| 00:00:10 |
----------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
6 recursive calls
0 db block gets
3036 consistent gets
1 physical reads
0 redo size
537 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
DOUDOU@doudou1> create index idx_doudou on test1 (object_id,1);
Index created.
Execution Plan
----------------------------------------------------------
Plan hash value: 2562788052
--------------------------------------------------------------------------------
----
----
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|
|
--------------------------------------------------------------------------------
----
----
| 0 | SELECT STATEMENT | | 1 | 13 | 209 (1)| 00:00:
03 |
03 |
| 1 | SORT AGGREGATE | | 1 | 13 | |
|
|
| 2 | INDEX FAST FULL SCAN| IDX_DOUDOU | 258K| 3281K| 209 (1)| 00:00:
03 |
03 |
--------------------------------------------------------------------------------
----
----
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
5 recursive calls
0 db block gets
820 consistent gets
741 physical reads
0 redo size
537 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
總結:
1.NULL值在ORACLE中是一個神奇的值。所以想使用索引,定要考慮此列的值是否為空。
2.複合索引在一定情況下是可以優化有NULL值的列(複合索引所有列值都為NULL時,複合索引也不會被使用,因為這行的索引值不會被儲存)。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26442936/viewspace-1257548/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 一個複合索引的優化案例索引優化
- 複合索引與函式索引優化一例索引函式優化
- 查詢中讓優化器使用複合索引優化索引
- 增加複合索引優化SQL的簡單過程索引優化SQL
- ORACLE 9i資料庫優化案例(2) --- 單列索引變複合索引的優化Oracle資料庫優化索引
- SQL優化案例一則--複合索引沒有被使用SQL優化索引
- 又一個複合索引的SQL調優索引SQL
- MySQL複合索引MySql索引
- MySQL複合索引探究MySql索引
- MySQL建立複合索引MySql索引
- SQL優化(二)(聯合索引的使用)SQL優化索引
- 【oracle 效能優化】組合索引查詢。Oracle優化索引
- SQLServer索引優化(3):對於建組合索引的要求SQLServer索引優化
- SQLT 最佳化SQL 用複合索引代替單列索引的案例SQL索引
- MongoDB複合索引詳解MongoDB索引
- DataFrame刪除複合索引索引
- 理解索引:索引優化索引優化
- MySQL 優化之 index_merge (索引合併)MySql優化Index索引
- 【oracle 效能優化】組合索引之index_ssOracle優化索引Index
- oracle複合索引介紹(多欄位索引)Oracle索引
- MongoDB中複合索引結構MongoDB索引
- MSSQL優化之索引優化SQL優化索引
- PostgreSQL11preview-索引優化。filter智慧消除、分割槽索引智慧合併SQLView索引優化Filter
- Elasitcsearch索引優化索引優化
- MySQL索引優化MySql索引優化
- mysql優化索引MySql優化索引
- SQL優化-索引SQL優化索引
- MySQL 效能優化之索引優化MySql優化索引
- 常數複合索引應用案例索引
- SQL最佳化案例一則--複合索引沒有被使用SQL索引
- OGG複製程式延遲高,優化方法一(使用索引)優化索引
- MySQL調優之索引優化MySql索引優化
- Mysql索引優化(一)MySql索引優化
- MySQL 5.7 索引優化MySql索引優化
- Oracle 索引的優化Oracle索引優化
- Mysql索引優化之索引的分類MySql索引優化
- OGG複製程式延遲高,優化方法二(存在索引),SQL選擇不好的索引優化索引SQL
- Oracle複合索引的建立和注意事項Oracle索引