码迷,mamicode.com
首页 > 数据库 > 详细

SQL执行异常系列之——统计信息不准

时间:2018-09-18 22:37:30      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:loop   object_id   alt   cpu   就会   from   ops   HERE   信息   

作为一线的dba,经常会遇到批量业务或联机业务sql中统计信息不准导致的执行计划异常的问题。虽较为常见,但还是记录一下,作为SQL执行异常专题的开篇一章。要了解执行计划,首先要理解统计信息,因为ORACLE主要是基于表的统计信息计算表访问、过滤、表关联的消耗,从而生成最终的执行计划,如果表的统计信息不准,自然而然也就会影响最终sql的执行结果。

一、统计信息的分类

技术分享图片

 

二、在以上统计信息中,有关表的统计信息不准导致的执行计划存在异常的现象最为常见:

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------
Plan hash value: 3251985671

--------------------------------------------------------------------------------------
| Id  | Operation       | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |             |       1 |      23 |     344   (0)| 00:00:05 |
|   1 |  NESTED LOOPS       |             |       1 |      23 |     344   (0)| 00:00:05 |
|   2 |   TABLE ACCESS FULL| STATIC_TEST1    |       1 |      18 |     343   (0)| 00:00:05 |
|*  3 |   INDEX RANGE SCAN | STATIC_TEST_IDX |       1 |       5 |       1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("A"."OBJECT_ID"="B"."OBJECT_ID")

15 rows selected.

SQL> explain plan for select a.object_name,b.object_id
from static_test a,static_test1 b
where a.object_id=b.object_id;  2    3  

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------
Plan hash value: 1637592733

------------------------------------------------------------------------------------------------
| Id  | Operation             | Name           | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT         |               |     1 |    35 |   344     (0)| 00:00:05 |
|   1 |  NESTED LOOPS             |               |     1 |    35 |   344     (0)| 00:00:05 |
|   2 |   NESTED LOOPS             |               |     1 |    35 |   344     (0)| 00:00:05 |
|   3 |    TABLE ACCESS FULL         | STATIC_TEST1    |     1 |     5 |   343     (0)| 00:00:05 |
|*  4 |    INDEX RANGE SCAN         | STATIC_TEST_IDX |     1 |       |     1     (0)| 00:00:01 |
|   5 |   TABLE ACCESS BY INDEX ROWID| STATIC_TEST     |     1 |    30 |     1     (0)| 00:00:01 |
------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("A"."OBJECT_ID"="B"."OBJECT_ID")

17 rows selected.

SQL> explain plan for select a.object_name,b.object_id
from static_test a,static_test1 b
where a.object_id=b.object_id; 2 3


Explained.


SQL> select * from table(dbms_xplan.display);


PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------
Plan hash value: 1637592733


------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 35 | 344 (0)| 00:00:05 |
| 1 | NESTED LOOPS | | 1 | 35 | 344 (0)| 00:00:05 |
| 2 | NESTED LOOPS | | 1 | 35 | 344 (0)| 00:00:05 |
| 3 | TABLE ACCESS FULL | STATIC_TEST1 | 1 | 5 | 343 (0)| 00:00:05 |
|* 4 | INDEX RANGE SCAN | STATIC_TEST_IDX | 1 | | 1 (0)| 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| STATIC_TEST | 1 | 30 | 1 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------


Predicate Information (identified by operation id):
---------------------------------------------------


4 - access("A"."OBJECT_ID"="B"."OBJECT_ID")


17 rows selected.

 

 

SQL执行异常系列之——统计信息不准

标签:loop   object_id   alt   cpu   就会   from   ops   HERE   信息   

原文地址:https://www.cnblogs.com/dayu-liu/p/9671334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!