- 最后登录
- 2017-11-10
- 在线时间
- 228 小时
- 威望
- 8
- 金钱
- 936
- 注册时间
- 2012-1-10
- 阅读权限
- 10
- 帖子
- 14
- 精华
- 0
- 积分
- 8
- UID
- 160
|
1#
发表于 2012-3-3 22:56:30
|
查看: 5367 |
回复: 6
这两天遇到一个问题,请看以下两个SQL的执行计划:
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
PL/SQL Release 9.2.0.8.0 - Production
CORE 9.2.0.8.0 Production
TNS for Solaris: Version 9.2.0.8.0 - Production
NLSRTL Version 9.2.0.8.0 - Production
SQL> explain plan for
2 select * from nok_nmc.nok_bts_hour_per_total
3 where PERIOD_START>=to_date('20120227','yyyymmdd')
4 and period_start<to_date('20120228','yyyymmdd');
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost | Pstart| Pstop |
------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 611K| 585M| 50996 | | |
| 1 | TABLE ACCESS FULL | NOK_BTS_HOUR_PER_TOTAL | 611K| 585M| 50996 | 38 | 38 |
------------------------------------------------------------------------------------------------
Note: cpu costing is off, PLAN_TABLE' is old version
9 rows selected.
SQL> explain plan for
2 select * from nok_nmc.nok_bts_hour_per_total
3 where PERIOD_START>=trunc(sysdate-5)
4 and period_start<trunc(sysdate-4);
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost | Pstart| Pstop |
---------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 13140 | 12M| 3915 | | |
| 1 | FILTER | | | | | | |
| 2 | PARTITION RANGE ITERATOR | | | | | KEY | KEY |
| 3 | TABLE ACCESS BY LOCAL INDEX ROWID| NOK_BTS_HOUR_PER_TOTAL | 13140 | 12M| 3915 | KEY | KEY |
| 4 | INDEX RANGE SCAN | NOK_BTS_HOUR_PER_INDX | 23651 | | 66 | KEY | KEY |
---------------------------------------------------------------------------------------------------------------
Note: cpu costing is off, PLAN_TABLE' is old version
12 rows selected.
SQL>
据管理该数据库的DBA表示,其中的nok_bts_hour_per_total表和其上的索引都已经分析,看了CBO估算的基数,使用FTS的查询的估算更接近实际条数,实际应该为50W条。进一步检查数据字典视图,发现dba_indexes和dba_ind_partitions视图里该索引的global_stats字段都是NO,重新用dbms_stats包收集该索引信息,但两句SQL的计划和上面依旧没有明显的差别。
问题:1、为什么使用to_date函数和trunc+sysdate函数会有这么大的差别?2、为什么使用索引扫描的计划中,CBO没能估计到只需要扫描38#分区?
若还需要其他信息请说明,谢谢。 |
|