SQL执行异常系列之——统计信息不准
Posted dayu.liu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL执行异常系列之——统计信息不准相关的知识,希望对你有一定的参考价值。
作为一线的dba,经常会遇到批量业务或联机业务sql中统计信息不准导致的执行计划异常的问题。虽较为常见,但还是记录一下,作为SQL执行异常专题的开篇一章。要了解执行计划,首先要理解统计信息,因为ORACLE主要是基于表的统计信息计算表访问、过滤、表关联的消耗,从而生成最终的执行计划,如果表的统计信息不准,自然而然也就会影响最终sql的执行结果。
一、统计信息的分类
二、在以上统计信息中,有关表的统计信息不准导致的执行计划存在异常的现象最为常见:
模拟测试步骤:
1.创建表static_test、static_test1表,并在表static_test上的object_id创建索引(以dba_objects为原表)
2.将表static_test1删除至只保留一条数据,收集统计信息,再插入dba_objects条数据。
3.两表做关联,查看执行计划。
4.重新收集表static_test1的统计信息,再重新查看执行计划。
**************************************
统计信息异常时,两表关联采用netsed loop方式
**************************************
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.
**************************************
统计信息正常时,两表关联采用netsed loop方式
**************************************
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执行异常系列之——统计信息不准的主要内容,如果未能解决你的问题,请参考以下文章