【INDEX_SS】使用HINT使SQL用索引跳躍掃描(Index Skip Scan)方式快速獲取資料
索引跳躍掃描(Index Skip Scan)可以使用到複合索引的非字首索引列,達到改善效能的作用,前提是全表掃面的代價高於索引跳躍式掃描的代價。這裡給出使用HINT方法使SQL走索引跳躍掃描的方法。
1.初始化環境
1)建立表T
sec@ora10g> create table t(x number,y number);
Table created.
2)初始化1000條資料
sec@ora10g> insert into t select rownum,66 from dual connect by rownum<=1000;
1000 rows created.
sec@ora10g> commit;
Commit complete.
sec@ora10g> select * from t ;
X Y
---------- ----------
1 66
2 66
3 66
……省略部分輸出……
998 66
999 66
1000 66
1000 rows selected.
3)在表T上建立複合索引
sec@ora10g> create index t_i on t(x,y);
Index created.
4)對錶進行分析
sec@ora10g> analyze table t compute statistics;
Table analyzed.
2.使用HINT方法使SQL走索引跳躍掃描
sec@ora10g> explain plan for select /*+ index_ss(t t_i) */ * from t where y=66;
Explained.
sec@ora10g> @?/rdbms/admin/utlxpls.sql
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------
Plan hash value: 597150364
-------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 5000 | 1002 (1)| 00:00:13 |
|* 1 | INDEX SKIP SCAN | T_I | 1000 | 5000 | 1002 (1)| 00:00:13 |
-------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("Y"=66)
filter("Y"=66)
14 rows selected.
3.不使用HINT檢視SQL語句的執行計劃
sec@ora10g> explain plan for select * from t where y=66;
Explained.
sec@ora10g> @?/rdbms/admin/utlxpls.sql
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3046511974
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 5000 | 2 (0)| 00:00:01 |
|* 1 | INDEX FAST FULL SCAN| T_I | 1000 | 5000 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("Y"=66)
13 rows selected.
此時SQL使用的是INDEX FAST FULL SCAN方式來獲得的資料。
4.小結
瞭解並構造每一種SQL語句的執行計劃有助於我們深入瞭解SQL語句的執行方法,進而選擇最有效的方法檢索和處理資料。
Good luck.
secooler
11.09.13
-- The End --
1.初始化環境
1)建立表T
sec@ora10g> create table t(x number,y number);
Table created.
2)初始化1000條資料
sec@ora10g> insert into t select rownum,66 from dual connect by rownum<=1000;
1000 rows created.
sec@ora10g> commit;
Commit complete.
sec@ora10g> select * from t ;
X Y
---------- ----------
1 66
2 66
3 66
……省略部分輸出……
998 66
999 66
1000 66
1000 rows selected.
3)在表T上建立複合索引
sec@ora10g> create index t_i on t(x,y);
Index created.
4)對錶進行分析
sec@ora10g> analyze table t compute statistics;
Table analyzed.
2.使用HINT方法使SQL走索引跳躍掃描
sec@ora10g> explain plan for select /*+ index_ss(t t_i) */ * from t where y=66;
Explained.
sec@ora10g> @?/rdbms/admin/utlxpls.sql
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------
Plan hash value: 597150364
-------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 5000 | 1002 (1)| 00:00:13 |
|* 1 | INDEX SKIP SCAN | T_I | 1000 | 5000 | 1002 (1)| 00:00:13 |
-------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("Y"=66)
filter("Y"=66)
14 rows selected.
3.不使用HINT檢視SQL語句的執行計劃
sec@ora10g> explain plan for select * from t where y=66;
Explained.
sec@ora10g> @?/rdbms/admin/utlxpls.sql
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3046511974
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1000 | 5000 | 2 (0)| 00:00:01 |
|* 1 | INDEX FAST FULL SCAN| T_I | 1000 | 5000 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("Y"=66)
13 rows selected.
此時SQL使用的是INDEX FAST FULL SCAN方式來獲得的資料。
4.小結
瞭解並構造每一種SQL語句的執行計劃有助於我們深入瞭解SQL語句的執行方法,進而選擇最有效的方法檢索和處理資料。
Good luck.
secooler
11.09.13
-- The End --
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-707424/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【TUNE_ORACLE】列出走了INDEX SKIP SCAN的SQL參考OracleIndexSQL
- 【Oracle】 索引的掃描方式Oracle索引
- [20180725]index skip-scan operation.txtIndex
- [20210220]全索引掃描快速索引掃描的邏輯讀.txt索引
- MySQL8.0之跳躍範圍掃描MySql
- [20181201]奇怪的INDEX SKIP SCAN執行計劃.txtIndex
- [20190815]索引快速全掃描的成本.txt索引
- AppBoxFuture: 二級索引及索引掃描查詢資料APP索引
- 全表掃描和全索引掃描索引
- ABAP下載的病毒掃描Virus Scan
- 如何使用js獲取USB掃碼槍資料JS
- MySQL中的全表掃描和索引樹掃描MySql索引
- 微信小程式獲取index索引值的方法微信小程式Index索引
- W13Scan 掃描器挖掘漏洞實踐
- 全表掃描和全索引掃描繼續(PG-TiDB)索引TiDB
- PostgreSQL DBA(119) - pgAdmin(LIMIT:Index Scan vs Bitmap Index Scan)SQLMITIndex
- 20180316不使用INDEX FULL SCAN (MIN/MAX)Index
- 在SQL隱碼攻擊中使用DNS獲取資料SQLDNS
- PostgreSQL技術內幕(七)索引掃描SQL索引
- 理解資料庫掃描方法-利用掃描方法對資料儲存進行優化資料庫優化
- SQL Server索引查詢/掃描沒有出現key lookup的案例淺析SQLServer索引
- Rad爬蟲結合W13Scan掃描器挖掘漏洞爬蟲
- 【TUNE_ORACLE】列出走了INDEX FULL SCAN的SQL參考OracleIndexSQL
- 資料結構(一)--- 跳躍表資料結構
- Redis資料結構—跳躍表Redis資料結構
- datatables使用ajax獲取資料
- 實用小工具——快速獲取資料庫時間寫法資料庫
- 掃描線及其應用
- 索引掃描可能不如全表掃描的場景的理解__純粹資料量而言,不涉及CLUSTERING_FACTOR索引
- MySQL複製跳過錯誤--slave_skip_errors、sql_slave_skip_counter、slave_exec_modeMySqlError
- 資料結構:跳躍連結串列資料結構
- MySQL SQL最佳化 - 覆蓋索引(covering index)MySql索引Index
- 機器視覺應用中工業相機的掃描方式視覺
- ElasticSearch 獲取es資訊以及索引操作Elasticsearch索引
- INDEX建立方式對SQL的影響IndexSQL
- W13Scan 漏洞掃描器之XSS外掛模組編寫示例
- 掃描智慧捕獲 富士通ScanSnap iX1500新品試用
- Web漏洞掃描篇-Nessus使用Web
- 使用 KRAWL 掃描 Kubernetes 錯誤