一个用 Oracle 函数索引进行优化的例子
2016-12-29 13:57
375 查看
表中有500万条记录,原来没有索引:set timing on
set autotrace traceonly
SQL> select count(*), to_char(time,'hh24') from userloginlog
2 where trunc(time) = trunc(sysdate) - 1
3 group by to_char(time,'hh24')
4 order by to_char(time,'hh24');
24 rows selected.
Elapsed: 00:00:06.70
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6179 Card=37215 Bytes=297720)
1 0 SORT (GROUP BY) (Cost=6179 Card=37215 Bytes=297720)
2 1 TABLE ACCESS (FULL) OF 'USERLOGINLOG' (TABLE) (Cost=6039 Card=37257 Bytes=298056)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
25154 consistent gets
24470 physical reads
0 redo size
763 bytes sent via SQL*Net to client
514 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
24 rows processed
查询使用全表扫描,执行需6.7秒。下面建立函数索引:create index idx_time on userloginlog (to_char(time,'hh24')) tablespace indexes;
create index idx_time2 on userloginlog (trunc(time)) tablespace indexes;执行同样的查询:SQL> select count(*), to_char(time,'hh24') from userloginlog
2 where trunc(time) = trunc(sysdate) - 1
3 group by to_char(time,'hh24')
4 order by to_char(time,'hh24');
24 rows selected.
Elapsed: 00:00:00.34
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=323 Card=37215 Bytes=297720)
1 0 SORT (GROUP BY) (Cost=323 Card=37215 Bytes=297720)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'USERLOGINLOG' (TABLE) (Cost=183 Card=37257 Bytes=298056)
3 2 INDEX (RANGE SCAN) OF 'IDX_TIME2' (INDEX) (Cost=64 Card=16143)
Statistics
----------------------------------------------------------
197 recursive calls
0 db block gets
341 consistent gets
1 physical reads
0 redo size
763 bytes sent via SQL*Net to client
514 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
6 sorts (memory)
0 sorts (disk)
24 rows processed查询使用索引扫描,执行需0.34秒,快了20倍。还有一点,建立索引后并没有执行 analyze table userloginlog compute statistics; 进行分析,索引就生效了,这是10g的改进吧。
set autotrace traceonly
SQL> select count(*), to_char(time,'hh24') from userloginlog
2 where trunc(time) = trunc(sysdate) - 1
3 group by to_char(time,'hh24')
4 order by to_char(time,'hh24');
24 rows selected.
Elapsed: 00:00:06.70
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=6179 Card=37215 Bytes=297720)
1 0 SORT (GROUP BY) (Cost=6179 Card=37215 Bytes=297720)
2 1 TABLE ACCESS (FULL) OF 'USERLOGINLOG' (TABLE) (Cost=6039 Card=37257 Bytes=298056)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
25154 consistent gets
24470 physical reads
0 redo size
763 bytes sent via SQL*Net to client
514 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
24 rows processed
查询使用全表扫描,执行需6.7秒。下面建立函数索引:create index idx_time on userloginlog (to_char(time,'hh24')) tablespace indexes;
create index idx_time2 on userloginlog (trunc(time)) tablespace indexes;执行同样的查询:SQL> select count(*), to_char(time,'hh24') from userloginlog
2 where trunc(time) = trunc(sysdate) - 1
3 group by to_char(time,'hh24')
4 order by to_char(time,'hh24');
24 rows selected.
Elapsed: 00:00:00.34
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=323 Card=37215 Bytes=297720)
1 0 SORT (GROUP BY) (Cost=323 Card=37215 Bytes=297720)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'USERLOGINLOG' (TABLE) (Cost=183 Card=37257 Bytes=298056)
3 2 INDEX (RANGE SCAN) OF 'IDX_TIME2' (INDEX) (Cost=64 Card=16143)
Statistics
----------------------------------------------------------
197 recursive calls
0 db block gets
341 consistent gets
1 physical reads
0 redo size
763 bytes sent via SQL*Net to client
514 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
6 sorts (memory)
0 sorts (disk)
24 rows processed查询使用索引扫描,执行需0.34秒,快了20倍。还有一点,建立索引后并没有执行 analyze table userloginlog compute statistics; 进行分析,索引就生效了,这是10g的改进吧。
相关文章推荐
- Oracle进行模拟测试数据的一个例子
- SQL优化基础 使用索引(一个小例子)
- SQL优化基础 使用索引(一个小例子)
- Qt使用一个事件队列对所有发出的事件进行维护(QObject的event()函数相当于dispatch函数),用EventLabel 继承QLabel作为例子(简单明了) good
- 怎样使用复合索引优化一个分析函数SQL
- 一个简单的oracle函数返回数组的例子[zz]
- 一个oracle函数返回数组的例子
- oracle 性能优化操作十四: 使用基于函数的索引
- 利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引)
- SQL优化基础:使用索引(一个小例子)
- Oracle 通过复合索引提高查询性能的一个真实客户例子
- 一个完整利用InternetOpen等系列函数进行下载的测试例子
- Oracle函数索引会进行数据检查?
- oracle 性能优化操作十五: 基于函数的索引要求等式匹配
- SQL优化基础 使用索引(一个小例子)
- SQL优化基础:使用索引(一个小例子)
- 一个简单的oracle函数返回数组的例子
- oracle创建一个函数例子
- SQL优化基础 使用索引(一个小例子)
- 【Oracle 函数索引】一次数据库的优化过程