@dbsnake-用合適的函式索引來避免看似無法避免的全表掃描
昨天聽了@dbsnake的SQL方法論,感覺比第一次要更有感覺,希望對實際工作能有幫助。
昨天講到一處利用reverse函式建立索引,避免全表掃描的case,頗有感觸,拿出來試一下。
SQL> create table rev (id number, name varchar2(5));
Table created.
SQL> select * from rev;
ID NAME
---------- -----
1 abc
2 bc
3 c
SQL> create index rev_idx0 on rev(name);
Index created.
SQL> set autot on
SQL> select id, name from rev where name like '%bc';
ID NAME
---------- -----
1 abc
2 bc
Execution Plan
----------------------------------------------------------
Plan hash value: 3205185662
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 34 | 3 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| REV | 2 | 34 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("NAME" LIKE '%bc')
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------------------------------------------
5 recursive calls
0 db block gets
16 consistent gets
0 physical reads
0 redo size
633 bytes sent via SQL*Net to client
492 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
2 rows processed
這裡建立了name的B樹索引,但由於使用了%bc為條件,所以不會用索引,這裡用了全表掃描。
如何能讓%bc條件使用索引呢?這裡講到%bc不能用索引的原因是因為索引鍵值按照索引二進位制的順序排序,%在前就無法精確定位,因此無法使用索引。既然%在後面可以使用索引,那就想辦法將%的條件放在後面組織。
SQL> create index rev_idx on rev(reverse(name));
Index created.
SQL> select id, name from rev where reverse(name) like reverse('%bc');
ID NAME
---------- -----
2 bc
1 abc
Execution Plan
----------------------------------------------------------
Plan hash value: 2418054352
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 34 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| REV | 2 | 34 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | REV_IDX | 1 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access(REVERSE("NAME") LIKE 'cb%')
filter(REVERSE("NAME") LIKE 'cb%')
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------------------------------------------
28 recursive calls
0 db block gets
14 consistent gets
0 physical reads
0 redo size
633 bytes sent via SQL*Net to client
492 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
2 rows processed
這裡用了reverse函式,相當於方向匹配字串bc,這樣就將前面的%放到了後面。從執行計劃看cost從3變為2。
注:
這裡可以看到無論哪次執行,物理讀都是0,原因我覺得就是第一次執行過一個select * from rev;,因為資料量比較小,第一次select之後,記錄就從data file快取到buffer cache,即使根據LRU演算法,負載不是太大的DB,很快的時間內這些資料還可能在其中,沒被age out,所以再次執行SQL時,就可能物理讀是0。
總結:
以上的示例就是@dbsnake講的“用合適的函式索引來避免看似無法避免的全表掃描“。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7192724/viewspace-773276/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用索引快速全掃描(Index FFS)避免全表掃描的若干場景索引Index
- oracle優化:避免全表掃描Oracle優化
- MySQL中的全表掃描和索引樹掃描MySql索引
- 索引全掃描和索引快速全掃描的區別索引
- Oracle中存取資料掃描Table及索引的方式(全表掃描,索引掃描等)Oracle索引
- oracle 全表掃描,索引範圍掃描與塊的理解Oracle索引
- 全表掃描的cost 與 索引掃描Cost的比較 – 無直方圖(10.1.0.3以後)索引直方圖
- 【MySQL】全索引掃描的bugMySql索引
- 有索引卻走全表掃描的實驗分析索引
- (轉)索引掃描還是全表掃描(Index Scan Or Full Table Scan)索引Index
- 轉)索引掃描還是全表掃描(Index Scan Or Full Table Scan)索引Index
- 避免SQL中的函式呼叫SQL函式
- 避免對mod函式的呼叫函式
- 查詢全表掃描的sqlSQL
- 掃描易的api函式API函式
- 優化全表掃描優化
- delete 與全表掃描delete
- 抓取全表掃描的表,篩選和分析
- Sql最佳化(十一) 避免對資料的重複掃描(1)SQL
- ORACLE全表掃描查詢Oracle
- oracle是如何進行全表掃描的Oracle
- 優化Oracle with全表掃描的問題優化Oracle
- 索引掃描可能不如全表掃描的場景的理解__純粹資料量而言,不涉及CLUSTERING_FACTOR索引
- 【Oracle】 索引的掃描方式Oracle索引
- 怎麼解決因全表掃描帶來的 Buffer Pool 汙染
- 使用索引掃描來進行排序索引排序
- 查詢全表掃描語句
- mysql下建立索引讓其index全掃描MySql索引Index
- 一條全表掃描sql語句的分析SQL
- 優化Oracle with全表掃描的問題(二)優化Oracle
- delete 刪除資料 全表掃描還是掃描所有塊的測試delete
- oracle實驗記錄(分割槽全表掃描(全區掃描) FTS 時候的成本計算)Oracle
- go語言介面避免無意被適配Go
- MySQL 避免行鎖升級為表鎖——使用高效的索引MySql索引
- Win10系統下掃描器程式無法掃描的解決方法Win10
- 執行計劃-資料訪問方式(全表掃描與4種索引的方式)索引
- noworkload下全表掃描cost的計算
- 使用全表掃描快取大表的相關問題快取