前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个用 Oracle 函数索引进行优化的例子

一个用 Oracle 函数索引进行优化的例子

作者头像
用户1148526
发布2018-01-03 15:23:46
1.7K0
发布2018-01-03 15:23:46
举报
文章被收录于专栏:Hadoop数据仓库Hadoop数据仓库

表中有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的改进吧。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年12月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档