SQL优化之不走索引_走rule很快,CBO很慢
来源:互联网 发布:成都java培训哪家好 编辑:程序博客网 时间:2024/04/30 04:06
SQL> create table test(id int);
表已创建。
SQL> insert into test select 1 from table1;
已创建1186623行。
SQL> insert into test select 2 from table2;
已创建70行。
SQL> commit;
提交完成。
SQL> create index i_test_1 on test(id);
索引已创建。
SQL> analyze table test compute statistics;
表已分析。
SQL> set autot trace
SQL> set timing on
SQL> select * from test where id=2;
已选择70行。
已用时间: 00: 00: 00.03
执行计划
----------------------------------------------------------
Plan hash value: 1357081020
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 593K| 1158K| 534 (9)| 00:00:07 |
|* 1 | TABLE ACCESS FULL| TEST | 593K| 1158K| 534 (9)| 00:00:07 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ID"=2)
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
1811 consistent gets
0 physical reads
0 redo size
1290 bytes sent via SQL*Net to client
444 bytes received via SQL*Net from client
6 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
70 rows processed
未走索引,统计信息没有反应真实情况
SQL> select /*+ rule */ * from test where id=2;
已选择70行。
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value: 3245211066
-------------------------------------
| Id | Operation | Name |
-------------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | INDEX RANGE SCAN| I_TEST_1 |
-------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("ID"=2)
Note
-----
- rule based optimizer used (consider using cbo)
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
1290 bytes sent via SQL*Net to client
444 bytes received via SQL*Net from client
6 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
70 rows processed
可以看出,走索引是很快的,这个字段是倾斜的,收集下直方图就能正确的执行计划
SQL> analyze table test compute statistics for table for all indexes for all indexed columns;
表已分析。
SQL> select * from test where id=2;
已选择70行。
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value: 3245211066
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 70 | 140 | 3 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| I_TEST_1 | 70 | 140 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("ID"=2)
统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
1290 bytes sent via SQL*Net to client
444 bytes received via SQL*Net from client
6 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
70 rows processed
收集直方图后走了正确的执行计划
- SQL优化之不走索引_走rule很快,CBO很慢
- SQL优化之不走索引_走rule很快,CBO很慢
- SQL优化之不走索引_统计信息不对
- SQL优化之不走索引_加hint也不走索引
- SQL 优化之该走索引却不走索引的分析
- sql优化案例-从走索引变成走分区消除
- 什么样的sql不走索引
- SQL不走索引的情况
- Oracle SQL不走索引小记
- 不走索引原因
- Oracle 不走索引
- 走主键索引的查询sql变慢的问题
- SQL 优化之该走索引却不走索引的分析(二)
- SQL 优化之该走索引却不走索引的分析(二)
- mysql优化,导致查询不走索引的原因总结
- Oracle查询不走索引
- 不走索引的原因
- mysql不走索引总结
- poj2387- Til the Cows Come Home
- 不要创新,要模仿?
- C++学习笔记(13)----static_cast、dynamic_cast、reinterpret_cast、和const_cast----(2)
- json的处理java后台和前台之间
- linux增加删除虚拟IP
- SQL优化之不走索引_走rule很快,CBO很慢
- Ubuntu右键添加"以管理员身份打开","在终端中打开"
- 雷锋读图:导致用户离开网站的8大负面因素
- java学习笔记(一)
- MFC程序使用CComboBox控件详解
- 版本控制系统Git
- Array.prototype.slice.call(arguments,1)原理
- Extjs-表格 Ext.grid.GridPanel 分页------------------------------------------------
- 电商价格战的冰与火:当局者清,旁观者迷