您的位置:首页 > 其它

What causes Cardinality to be wrong?

2015-08-03 20:14 519 查看
What causes Cardinality to be wrong?

1>Data Skews

2>Multiple single column predicates on a table --->>>解决方案为收集扩展统计信息

3>A function wrapped where clause predicate

4>No statistics or Stale Statistics ----->>>解决方案为收集统计信息
5>Multiple columns used in a join---->>>解决方案为1>Create extended statistics on the join columns on each table
2>Remember column group statistics only work with equality predicates and in lists
6>Complicated expression

如何查询统计信息陈旧:

SELECT table_name, stale_stats FROM user_tab_ statistics;

NO --->>>意味着统计信息良好
YES --->>>意味着统计信息陈旧
--  --->>>NULL意味着没有统计信息
Note if DML has occurred very recently use dbms_stats.flush_database_monitoring_info

如果创建扩展统计信息?

Select dbms_stats.create_extended_stats(Null,'TABLE_NAME','(COL1,COL2)') From dual;
Exec dbms_stats.gather_table_stats( Null,'TABLE_NAME');
Select column_name, num_distinct, histogram From user_tab_col_statistics Where table_name = 'XXX';

NOTE: Column Group statistics only works with equality predicates & in-lists

3>A function wrapped where clause predicate解决方案如下:

SELECT * FROM Customers WHERE UPPER(CUST_LAST_NAME) = ‘SMITH’;
exec dbms_stats.gather_table_stats(null,'customers',method_opt =>'for all columns size skewonly for columns (UPPER(CUST_LAST_NAME))');

Select column_name, num_distinct, histogram From user_tab_col_statistics Where table_name = 'CUSTOMERS';

Automatic Column Group Creation
1. Start column group usage capture
SQL> exec dbms_stats.seed_col_usage(null,null,300);
Capture column group usage from queries executing in the next 5 mins
2. Run your workload
3. Check we have column usage information for our table
SQL> select dbms_stats.report_col_usage(user, 'customers') from dual;

EQ means column was used in single table equality predicate.
FILTER means column used in single table filter predicates.
GROUP_BY used in group by expression.
4. Create extended stats for customers based on usage
SQL> select dbms_stats.create_extended_stats(user, 'customers') from dual;

Column group statistics will now be automatically maintained every time you gather statistics on this table.

Complex Expressions
Query: SELECT count(*) FROM sales WHERE cust_id < 2222 AND prod_id > 5;

Cause                                                               Solution
Stale or missing statistics                                         DBMS_STATS
Data Skew                                                           Re-gather statistics to get histogram*
Multiple single column predicates on a table                        Create a column group using DBMS_STATS.CREATE_EXTENDED_STATS
Function wrapped column                                             Create statistics on the funct wrapped column using DBMS_STATS.CREATE_EXTENDED_STATS
Multiple columns used in a join                                     Create a column group on join columns using DBMS_STATS.CREATE_EXTENDED_STAT
Complicated expression containing columns from multiple tables      Use dynamic sampling level 4 or higher

Cardinality = num_rows / num_distinct

• If there is a data skew the selectivity could be way off

• Create a histogram to correct the selectivity calculation

• Oracle automatically creates a histogram if it suspects a data skew

Extended Optimizer Statistics provides a mechanism to collect statistics on a group of columns

•Full integration into existing statistics framework

•Automatically maintained with column statistics

•Instantaneous and transparent benefit for any migrated application
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: