**基数与总行数的比值再乘以100%就是某个列的选择性。** **当 一个列出现在 where 条件中,该列没有创建索引并且选择性大于 20%, 那么该列就必须创建索引, 从而提升 SQL 查询性能。 当然了, 如果表只有几百条数据, 那我们就不用创建索引了。** 选择性查询脚本
select a.column_name, b.num_rows, a.num_distinct Cardinality, round(a.num_distinct / b.num_rows * 100, 2) selectivity, a. histogram, a.num_buckets 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 = 'TEST' ;
抓出必须创建索引的列 首先,该列必须出现在where 条件中,怎么抓出表的哪个列出现在where 条件中呢?有两 种方法, 一种是可以通过V$SQL_PLAN 抓取, 另一种是通过下面的脚本抓取。 先执行下面的存储过程, 刷新数据库监控信息
begin dbms_stats.flush_database_monitoring_info; end; /
运行完上面的命令之后, 再运行下面的查询语句就可以查出哪个表的哪个列出现在where 条件中。 (意思是该表跑过的SQL语句中,出现在where条件里且未建过索引的列,然后我们再进一步判断适不适合建索引)
select r.name owner, o.name table_name, c.name column_name, equality_preds,--等值过滤 equijoin_preds,--等值JOIN比如where a.id=b.id nonequijoin_preds,--不等JOIN range_preds, like_preds,--LIKE过滤 null_preds,--NULL过滤 timestamp 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 = 'TEST';
接下来我们查询出选择性大于等于20%的列
select a.column_name, b.num_rows, a.num_distinct Cardinality, round(a.num_distinct / b.num_rows * 100, 2) selectivity, a. histogram, a.num_buckets 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 = 'TEST' and a.num_distinct/b.num_rows >= 0.2 ;
最后, 确保这些列没有创建索引
select table_owner,table_name,column_name,index_name from dba_ind_columns where table_owner ='TEST' and table_name = 'TEST'; no rows selected
