【TUNE_ORACLE】列出SQL謂詞中需要建立索引的列SQL參考
實驗環境
搭建平臺:VMware Workstation
OS:RHEL 6.10
Grid&DB:Oracle 11.2.0.4
SQL參考
--******該指令碼依賴統計資訊!!必須先收集統計資訊!!******
注:
統計收集方法詳見: http://blog.itpub.net/69992972/viewspace-2784605/
--1. 只判斷出現在SQL謂詞條件中的列是否需要建立索引
select owner,
column_name,
num_rows,
cardinality,
selectivity,
'Need index' as notice
from (select b.owner,
a.column_name,
b.num_rows,
a.num_distinct cardinality,
round(a.num_distinct / b.num_rows * 100, 2) selectivity
from dba_tab_col_statistics a, dba_tables b
where a.owner = b.owner
and a.table_name = b.table_name
and a.owner = 'TEST'
and a.table_name = 'TAB')
where selectivity >= 20
and column_name not in
(select column_name
from dba_ind_columns
where table_owner = 'TEST'
and table_name = 'TAB')
---------------- 出現在where條件中(需要提前重新整理監控資訊dbms_stats.flush_database_monitoring_info):
and column_name in
(select c.name
from sys.col_usage$ u, sys.obj$ o, sys.col$ c, sys.user$ r
where o.obj# = u.obj#
and c.obj# = u.obj#
and c.col# = u.intcol#
and r.name = 'TEST'
and o.name = 'TAB');
--2. 出現在where條件中,選擇性大於20%,總行數大於5W的沒有建立索引的列
select owner,
table_name,
column_name,
num_rows,
cardinality,
selectivity,
'Need index' as notice
from (select a.owner,
a.table_name,
a.column_name,
b.num_rows,
a.num_distinct cardinality,
round(a.num_distinct / b.num_rows * 100, 2) selectivity
from dba_tab_col_statistics a, dba_tables b
where a.owner = b.owner
and a.table_name = b.table_name
and a.owner = 'TEST')
where selectivity >= 20
and num_rows > 50000
and (table_name, column_name) not in
(select table_name, column_name
from dba_ind_columns
where table_owner = 'TEST'
and column_position = 1)
---------------- 出現在where條件中(需要提前重新整理監控資訊dbms_stats.flush_database_monitoring_info):
and (table_name, column_name) in
(select o.name, c.name
from sys.col_usage$ u, sys.obj$ o, sys.col$ c, sys.user$ r
where o.obj# = u.obj#
and c.obj# = u.obj#
and c.col# = u.intcol#
and r.name = 'TEST');
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69992972/viewspace-2785577/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【TUNE_ORACLE】列出索引被哪些SQL引用的SQL參考Oracle索引SQL
- 【TUNE_ORACLE】列出必須建立直方圖的列SQL參考Oracle直方圖SQL
- 【TUNE_ORACLE】列出可以建立組合索引的SQL(回表訪問少數字段)的SQL參考Oracle索引SQL
- 【TUNE_ORACLE】列出可以建立組合索引的SQL(回表再過濾選擇性高的列)的SQL參考Oracle索引SQL
- 【TUNE_ORACLE】列出走了低選擇性索引的SQL參考Oracle索引SQL
- 【TUNE_ORACLE】列出走了Filter的SQL參考OracleFilterSQL
- 【TUNE_ORACLE】列出LOOP套LOOP的PL/SQL程式碼SQL參考OracleOOPSQL
- 【TUNE_ORACLE】列出一條SQL中多次出現的表名的SQL參考OracleSQL
- 【TUNE_ORACLE】列出帶有自定義函式的SQL的SQL參考Oracle函式SQL
- 【TUNE_ORACLE】列出有標量子查詢的SQL參考OracleSQL
- 【TUNE_ORACLE】列出走了INDEX FULL SCAN的SQL參考OracleIndexSQL
- 【TUNE_ORACLE】列出走了TABLE ACCESS FULL的SQL參考OracleSQL
- 【TUNE_ORACLE】列出走了INDEX SKIP SCAN的SQL參考OracleIndexSQL
- 【TUNE_ORACLE】列出走了笛卡爾積的SQL參考OracleSQL
- 【TUNE_ORACLE】檢視索引的叢集因子SQL參考Oracle索引SQL
- 【TUNE_ORACLE】檢視錶,列和索引的統計資訊SQL參考Oracle索引SQL
- 【TUNE_ORACLE】列出返回行數較多的巢狀迴圈(NESTED LOOPS)SQL的SQL參考Oracle巢狀OOPSQL
- 【TUNE_ORACLE】ROWID切片SQL參考OracleSQL
- 【TUNE_ORACLE】列出走了錯誤的排序合併連線的SQL參考Oracle排序SQL
- 【TUNE_ORACLE】列出NL(NESTED LOOPS)被驅動表走了全表掃描的SQL參考OracleOOPSQL
- 【TUNE_ORACLE】查出所有有“select *”語句的SQL參考OracleSQL
- 【TUNE_ORACLE】檢視錶的總塊數SQL參考OracleSQL
- 【TUNE_ORACLE】檢視每個列的選擇性和基數SQL參考OracleSQL
- 【TUNE_ORACLE】定製化執行計劃SQL參考OracleSQL
- 【TUNE_ORACLE】定製化收集統計資訊SQL參考OracleSQL
- SQL優化參考SQL優化
- 【TUNE_ORACLE】檢視系統CPU和IO情況SQL參考OracleSQL
- 【TUNE_ORACLE】檢查統計資訊是否過期SQL參考OracleSQL
- SQL 改寫系列七:謂詞移動SQL
- SQL 改寫系列六:謂詞推導SQL
- 【SQL】Oracle查詢轉換之謂詞推送SQLOracle
- 大資料SQL中的Join謂詞下推,真的那麼難懂?大資料SQL
- 用SQL建立索引的方法步驟SQL索引
- 【TUNE_ORACLE】檢視Oracle的壞塊在空閒空間中還是在已用空間中的SQL參考OracleSQL
- HANA SQL參考及Oracle對照SQLOracle
- 列出oracle dbtime得sql語句OracleSQL
- 如何判斷sql中的索引是否生效SQL索引
- SQL表的建立SQL