此脚本依赖统计信息。

    当一个表比较大,列选择性超过 20%,列出现在 where 条件中并且没有创建索引,我们可以对该列创建索引从而提升 SQL 查询性能。以下脚本抓出 Scott 账户下表总行数大于 5 万行、列选择性超过 20%、列出现在 where 条件中并且没有创建索引。

    1. select owner,
    2. table_name,
    3. column_name,
    4. num_rows,
    5. Cardinality,
    6. selectivity
    7. from (select a.owner,
    8. a.table_name,
    9. a.column_name,
    10. b.num_rows,
    11. a.num_distinct Cardinality,
    12. round(a.num_distinct / b.num_rows * 100, 2) selectivity
    13. from dba_tab_col_statistics a, dba_tables b
    14. where a.owner = b.owner
    15. and a.table_name = b.table_name
    16. and a.owner = 'SCOTT')
    17. where selectivity >= 20
    18. and num_rows > 50000
    19. and (table_name, column_name) not in
    20. (select table_name, column_name
    21. from dba_ind_columns
    22. where table_owner = 'SCOTT' and column_position=1)
    23. and (table_name, column_name) in
    24. (select o.name, c.name
    25. from sys.col_usage$ u, sys.obj$ o, sys.col$ c, sys.user$ r
    26. where o.obj# = u.obj#
    27. and c.obj# = u.obj#
    28. and c.col# = u.intcol#
    29. and r.name = 'SCOTT');

    在 Scott 账户中,test 表总行数大于 5 万行,有两个列出现在 where 条件中,选择性大于 20%,而且没有创建索引,我们通过以上脚本将其抓出。

    1. select owner,
    2. table_name,
    3. column_name,
    4. num_rows,
    5. Cardinality,
    6. selectivity
    7. from (select a.owner,
    8. a.table_name,
    9. a.column_name,
    10. b.num_rows,
    11. a.num_distinct Cardinality,
    12. round(a.num_distinct / b.num_rows * 100, 2) selectivity
    13. from dba_tab_col_statistics a, dba_tables b
    14. where a.owner = b.owner
    15. and a.table_name = b.table_name
    16. and a.owner = 'SCOTT')
    17. where selectivity >= 20
    18. and num_rows > 50000
    19. and (table_name, column_name) not in
    20. (select table_name, column_name
    21. from dba_ind_columns
    22. where table_owner = 'SCOTT' and column_position=1)
    23. and (table_name, column_name) in
    24. (select o.name, c.name
    25. from sys.col_usage$ u, sys.obj$ o, sys.col$ c, sys.user$ r
    26. where o.obj# = u.obj#
    27. and c.obj# = u.obj#
    28. and c.col# = u.intcol#
    29. and r.name = 'SCOTT');
    1. OWNER TABLE_NAME COLUMN_NAME NUM_ROWS CARDINALITY SELECTIVITY
    2. -------- --------------- --------------- ---------- ----------- -----------
    3. SCOTT TEST OBJECT_ID 73020 73020 100
    4. SCOTT TEST OBJECT_NAME 73020 41002 56.15