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
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
相关文章推荐
- 求无向图的最短路径问题
- Unix 與 MS-DOS 指令對照表
- 长时间后mysql自动关闭连接
- MFC:CCmdUI类的功能
- Top k问题的一个应用
- assert
- CentOS6.5中的网络配置(静态IP设置)
- c++11 中的 move 与 forward
- 队列的入队和出队操作
- Linux 所学命令
- UVALive 4764 简单dp水题(也可以暴力求解)
- Anim动画
- HDU 1509:Windows Message Queue【优先队列】
- Applicability Statement 2 (AS2) Module
- jdbc在web中的应用
- Linux 虚拟机 快捷键
- 手术Robot能取代医生吗?
- setup factory 9制作VB程序安装包
- node在windows下node-gyp编译可能会出现的问题
- 自我介绍