如何讓table表的null列由不走索引變為可走索引
SQL> create table t_null(a int not null,b int);
Table created.
SQL> insert into t_null select level,level+2 from dual connect by level<=10;
10 rows created.
SQL> commit;
Commit complete.
SQL> create index idx_1 on t_null(a);
Index created.
SQL> create index idx_2 on t_null(b);
Index created.
SQL> explain plan for select a from t_null;
Explained.
--非空a列採用了索引全掃描
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------
Plan hash value: 3375905911
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10 | 130 | 1 (0)| 00:00:01 |
| 1 | INDEX FULL SCAN | IDX_1 | 10 | 130 | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------
Note
-----
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------
- dynamic sampling used for this statement (level=2)
12 rows selected.
SQL> explain plan for select b from t_null;
Explained.
--空b列採用了全表掃描
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------
Plan hash value: 2443327197
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10 | 130 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T_NULL | 10 | 130 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Note
-----
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------
- dynamic sampling used for this statement (level=2)
12 rows selected.
--對空b列新增where 條件b is not null,可走索引全掃描
SQL> explain plan for select a from t_null where a is not null;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------
Plan hash value: 3375905911
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10 | 130 | 1 (0)| 00:00:01 |
| 1 | INDEX FULL SCAN | IDX_1 | 10 | 130 | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------
Note
-----
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------
- dynamic sampling used for this statement (level=2)
12 rows selected.
--如對空b列進行聚集運算,如下count會自動filter out null value,走了索引全掃描
SQL> explain plan for select count(b) from t_null;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------
Plan hash value: 1382308919
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 1 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
| 2 | INDEX FULL SCAN| IDX_2 | 10 | 130 | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------
Note
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------
-----
- dynamic sampling used for this statement (level=2)
13 rows selected.
--刪除b列索引
SQL> drop index idx_2;
Index dropped.
--構建b列如下的索引,即如b列為空,則轉化為0
SQL> create index idx_2 on t_null(b,0);
Index created.
SQL> explain plan for select b from t_null;
Explained.
--修正後b列可走索引全掃描
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------
Plan hash value: 3964661047
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10 | 130 | 1 (0)| 00:00:01 |
| 1 | INDEX FULL SCAN | IDX_2 | 10 | 130 | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------
Note
-----
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------
- dynamic sampling used for this statement (level=2)
12 rows selected.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-754787/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 表為多列為null的表之索引示例Null索引
- 索引與null(一):單列索引索引Null
- 有索引卻走全表掃描的實驗分析索引
- 唯一索引,可以在索引列插入多個null嗎索引Null
- NULL列時,如何使得IS NULL或者IS NOT NULL可以使用索引來提高查詢效率Null索引
- SELECT COUNT(*) 索引會走 index fast full scan索引IndexAST
- 為什麼索引無法使用is null和is not null索引Null
- NULL與索引Null索引
- 帶你走進神一樣的Elasticsearch索引機制Elasticsearch索引
- 索引組織表(Index Organizied Table)索引Index
- 索引與null(二):組合索引索引Null
- CLUSTERING_FACTOR、回表、資料分佈傾斜走全表還是索引索引
- Oracle使用由字串索引的二維陣列Oracle字串索引陣列
- 索引組織表(index organized table ,IOT)索引IndexZed
- NULL 值與索引Null索引
- 表、索引遷移表空間alter table move索引
- 搜尋條件設定為Is Null一定不走索引嗎?Null索引
- 兩列複合索引中(交換索引中列的順序),選索引的原則是?因為索引名字ascii碼小?索引ASCII
- 資料庫聚簇索引——not null條件對唯一鍵索引成為聚簇索引的影響資料庫索引Null
- NULL 值與索引(二)Null索引
- SQL Server 索引和表體系結構(包含列索引)SQLServer索引
- Python - 讓requests不走系統 全域性 代理Python
- 資料列not null對索引影響一例Null索引
- MySQL中IS NULL、IS NOT NULL、!=不能用索引?胡扯!MySqlNull索引
- 走程式序員:那些不為人知的辛酸
- 資料庫索引欄位請不要為NULL資料庫索引Null
- 學習人工智慧不走捷徑,走大道的方式人工智慧
- 堅決不走小程式,而是走外掛化
- 認識SQLServer索引以及單列索引和多列索引的不同SQLServer索引
- 煩人的Null,你可以走開點了Null
- oracle為什麼不走索引Oracle索引
- 一個500萬記錄的table, 一欄位索引,不過會有空值,是保持null好,還是讓它有預設值好索引Null
- 索引失效系列——說說is null索引Null
- Oracle表table與索引index的分析及索引重建及統計資訊匯入匯出Oracle索引Index
- 走過企業智慧化的界碑:FusionData如何讓資料從資源變資產?
- openGauss 列存表PSort索引索引
- MySQL索引失效行鎖變表鎖MySql索引
- MySQL 字首索引——讓索引減負狂奔MySql索引