oracle 单列索引 多列索引的性能测试
清除oralce 缓存:alter system flush buffer_cache;
环境:oracle 10g 、 400万条数据,频率5分钟一条
1.应用场景: 找出所有站点的最新一条数据。sql语句如下:
——————————————————————————————————————————————————
with aa as(
select t1.EQP_ID ,max(MEASURE_TIME) maxtm from PLU_WATER_DATA t1 where t1.MEASURE_TIME >= to_date('2014-01-28 00:00:00', 'yyyy-MM-dd hh24:mi:ss')
and t1.MEASURE_TIME < to_date('2014-01-28 02:00:00', 'yyyy-MM-dd hh24:mi:ss') group by t1.EQP_ID
)
, bb as
(
select T1.EQP_ID as STCD,
T1.MEASURE_TIME as TM,
T1.FLUX_VALUE as FLOW
from PLU_WATER_DATA T1
where t1.MEASURE_TIME >= to_date('2014-01-28 00:00:00', 'yyyy-MM-dd hh24:mi:ss')
and t1.MEASURE_TIME < to_date('2014-01-28 02:00:00', 'yyyy-MM-dd hh24:mi:ss')
)
select * from aa inner join bb on aa.EQP_ID = bb.stcd and aa.maxtm = bb.tm inner join vi_plustation cc on cc.stcd =aa.EQP_ID
——————————————————————————————————————————
执行结果:
a.创建tm+stcd多列索引、tm单列索引、stcd单列索引 (执行时间2秒)
b.创建tm+stcd多列索引、stcd单列索引 (执行时间20秒)
c.创建stcd单列索引 (执行时间N秒)
d.创建tm+stcd多列索引 (执行时间22秒)
e.创建tm+stcd+tn+tp+cod多列索引 (执行时间71秒)
f.创建tm单列索引 (执行时间2秒)
2.应用场景 : 找出单个站点的一段时间内的数据。sql语句如下:
select T1.EQP_ID as STCD,
T1.MEASURE_TIME as TM,
T1.FLUX_VALUE as FLOW
from PLU_WATER_DATA T1
where t1.MEASURE_TIME >= to_date('2014-01-28 00:00:00', 'yyyy-MM-dd hh24:mi:ss')
and t1.MEASURE_TIME < to_date('2014-01-28 23:00:00', 'yyyy-MM-dd hh24:mi:ss') and eqp_id = '1244'
执行结果:
查询条件在 tm和stcd上
a. 创建tm单列索引 (执行时间17秒)
b. 创建tm单列索引、stcd单列索引 (执行时间 3秒)
c. 创建tm单列索引、stcd单列索引、tm+stcd多列索引 (执行时间 0.5秒)
d. 创建tm+stcd多列索引 (执行时间 0.7秒)
3.结论:查询条件中stcd或者tm是单独作为where条件,如果使用了stcd+tm的多列索引,效率降低。如果stcd和tm是作为联合where条件,建立stcd和tm的单列索引,效率明显低于使用stcd+tm的多列索引。
a.当一个sql语句有站点分组跟时间查询嵌套使用时,推荐将stcd和tm分开建立单列索引。列如:
select t1.EQP_ID ,max(MEASURE_TIME) maxtm from PLU_WATER_DATA t1 where t1.MEASURE_TIME >= to_date('2014-01-28 00:00:00', 'yyyy-MM-dd hh24:mi:ss') and t1.MEASURE_TIME < to_date('2014-01-28 02:00:00', 'yyyy-MM-dd hh24:mi:ss') group by t1.EQP_ID)
像这样的语句如果建立的是 stcd+tm的多列索引,那么读取速度是7m,而如果使用stcd和tm分开建立单列索引,速度在2m内。
b.当一个sql语句 指定“站点”和“时间”时,推荐建立stcd+tm多列索引。例如
select T1.EQP_ID as STCD,T1.MEASURE_TIME as TM,T1.FLUX_VALUE as FLOW from PLU_WATER_DATA T1
where t1.MEASURE_TIME >= to_date('2014-01-28 00:00:00', 'yyyy-MM-dd hh24:mi:ss')
and t1.MEASURE_TIME < to_date('2014-01-28 23:00:00', 'yyyy-MM-dd hh24:mi:ss') and eqp_id = '1244'
像这样的语句如果建立的是stcd和tm分开建立单列索引,速度在3m,但如果建立了stcd+tm的多列索引,速度在0.7m
c.当一个sql语句 制定“时间”查询时,推荐建立 tm单列索引。例如
select T1.EQP_ID as STCD,T1.MEASURE_TIME as TM,T1.FLUX_VALUE as FLOW from PLU_WATER_DATA T1
where t1.MEASURE_TIME >= to_date('2014-01-28 00:00:00', 'yyyy-MM-dd hh24:mi:ss')
and t1.MEASURE_TIME < to_date('2014-01-28 23:00:00', 'yyyy-MM-dd hh24:mi:ss')
像这样的语句如果建立的是stcd+tm的复合索引,速度在5m,如果建立在tm的单列索引上,速度在1.5m
————————————————————————————————————————————————————————————————
实例: 10分钟风速的表,数据表多,做了月的动态分区,针对14个站点2天内最新一条数据的查询,建了,时间+站点的索引,查询结果卡死。 加了 单独时间的索引。速度飞起来。看来最新一条数据的查询,主要是用到了时间的单独索引上。
select rownum RN,tmp2.* from (select * from (select * from (select PSW091201,fsw090101, bb.psw090101,bb.A3011300307, ObserveTimeS, ObserveTimeX, OBSERVETIME , A3001200402 , A3020402200, A3020402200X, A3020402202 , A3020400700, A3020400700X, A3020400702 , A3005000100 from( select t1.PSW091201,t1.fsw090101, psw090101,A3011300307 ,ObserveTime as ObserveTimeS,ObserveTime as ObserveTimeX, case t1.A3001200402 when '0' then to_char( ObserveTime ,'yyyy-mm-dd hh24:mi') when '3' then '<span style=''color:red''>?</span>' else '<span style=''color:red''>'||to_char( ObserveTime ,'yyyy-mm-dd hh24:mi') ||'</span>' end as OBSERVETIME , t1.A3001200402 , case A3020402202 when '0' then trim(trailing '.' from to_char( A3020402200 ,'fm999990.999999')) when '3' then '<span style=''color:red''>?</span>' else '<span style=''color:red''>'||trim(trailing '.' from to_char( A3020402200 ,'fm999990.999999')) ||'</span>' end as A3020402200 ,A3020402200 A3020402200X, A3020402202 , case A3020400702 when '0' then trim(trailing '.' from to_char( A3020400700 ,'fm999990.999999')) when '3' then '<span style=''color:red''>?</span>' else '<span style=''color:red''>'||trim(trailing '.' from to_char( A3020400700 ,'fm999990.999999')) ||'</span>' end as A3020400700,A3020400700 A3020400700X, A3020400702 , '<a href="javascript:void(0);" onclick="openFileYC(''ZJYCZ'','''||A3005000100||''','''||to_char(ObserveTime,'yyyy-mm-dd')||''','''||psw090101||''')" >' ||A3005000100||'</a>' as A3005000100 from TSW091201 t1 inner join ocean.TSW090101 t2 on t1.fsw090101 = t2.PSW090101 and t2.TYPE= '自建' and 1=1inner join ( select fsw090101 ,max(observetime) maxobservetime from TSW091201 where 1=1 and observetime >= to_date('2016-03-26 15:40','yyyy-MM-dd hh24:mi:ss') and observetime <= to_date('2016-03-28 15:40','yyyy-MM-dd hh24:mi:ss') and (A3020402202= 0 ) group by fsw090101 )t3 on t1.fsw090101 = t3.fsw090101 and t1.observetime=t3.maxobservetime ) aa,
( select PSW090101,A3011300307 from ocean.tsw090101 where TYPE = '自建') bb
where aa.FSW090101(+) = bb.PSW090101) tb where 1=1 order by ObserveTimeS DESC) tmp where rownum<=20 ) tmp2
实例 : 浮标次表层流 去年一年数据查询,总数据有2486125条 查最新20条数据,需要11s。有FSW080101, OBSERVETIME, A3010000300 唯一索引 OBSERVETIME索引,以及动态月分区
select rownum RN,tmp2.* from (select * from (select * from (select PSW080303, fsw080101 ,psw080101, SBMC ,ObserveTimeS,ObserveTimeX,OBSERVETIME ,A3001200402 , A3010000300 ,A3010800700, A3010800700X,A3010800702 , A3010800800 , A3010800800X,A3010800802 , A3000700200 , A3000700200X,A3000700202 , A3005000100 from( select t1.PSW080303, t1.fsw080101 ,psw080101, SBMC ,ObserveTime as ObserveTimeS, ObserveTime as ObserveTimeX, case t1.A3001200402 when '0' then to_char( ObserveTime ,'yyyy-mm-dd hh24:mi') when '3' then '<span style=''color:red''>?</span>' else '<span style=''color:red''>'||to_char( ObserveTime ,'yyyy-mm-dd hh24:mi') ||'</span>' end as OBSERVETIME , t1.A3001200402 , A3010000300 , case A3010800702 when '0' then trim(trailing '.' from to_char( A3010800700 ,'fm999990.999999')) when '3' then '<span style=''color:red''>?</span>' else '<span style=''color:red''>'||trim(trailing '.' from to_char( A3010800700 ,'fm999990.999999')) ||'</span>' end as A3010800700,A3010800700 A3010800700X, A3010800702 , case A3010800802 when '0' then trim(trailing '.' from to_char( A3010800800 ,'fm999990.999999')) when '3' then '<span style=''color:red''>?</span>' else '<span style=''color:red''>'||trim(trailing '.' from to_char( A3010800800 ,'fm999990.999999')) ||'</span>' end as A3010800800 ,A3010800800 A3010800800X, A3010800802 , case A3000700202 when '0' then trim(trailing '.' from to_char( A3000700200 ,'fm999990.999999')) when '3' then '<span style=''color:red''>?</span>' else '<span style=''color:red''>'||trim(trailing '.' from to_char( A3000700200 ,'fm999990.999999')) ||'</span>' end as A3000700200 ,A3000700200 A3000700200X, A3000700202 , '<a href="javascript:void(0);" onclick="openFile(''DFB'','''||A3005000100||''','''||to_char(ObserveTime,'yyyy-mm-dd')||''')" >' ||A3005000100||'</a>' as A3005000100 from TSW080303 t1 inner join ocean.tsw080101 t2 on t1.fsw080101 = t2.psw080101 and t2.TYPE = '大浮标' and 1=1)) tb where 1=1 and ObserveTimeS>=to_date('2015-01-01 00:00','yyyy-mm-dd hh24:mi:ss') and ObserveTimeS<=to_date('2015-12-31 23:59','yyyy-mm-dd hh24:mi:ss') order by ObserveTimeS desc) tmp where rownum<=20 ) tmp2
2天内的最新数据,如果站点有100个。那么做站点的左连接,查询出来是比较慢的 5s差不多 需要优化
最新的20条记录,数据有50万 即使做了datetime动态分区,再使用了order by datetime desc。也是超级慢 需要优化 其实是慢在 inner join上 已解决
查月数据,有时间排序,数据超过1万条,查最新20条 查询的时间3s
执行7秒
select * from TSW080303 t inner join ocean.tsw080101 t2 on t.fsw080101 =t2.psw080101
where ObserveTime>=to_date('2015-01-01 00:00','yyyy-mm-dd hh24:mi:ss')
and ObserveTime<=to_date('2015-12-31 23:59','yyyy-mm-dd hh24:mi:ss')
order by ObserveTime desc
执行0秒
select t.*,(select sbmc from ocean.TSW080101 tt where tt.psw080101 = t.fsw080101) from TSW080303 t
where ObserveTime>=to_date('2015-01-01 00:00','yyyy-mm-dd hh24:mi:ss')
and ObserveTime<=to_date('2015-12-31 23:59','yyyy-mm-dd hh24:mi:ss')
and t.fsw080101 in (select psw080101 from ocean.tsw080101)
order by ObserveTime desc
执行1秒以内 最终优化结果。
select * from
(select * from TSW080303 t where ObserveTime>=to_date('2015-01-01 00:00','yyyy-mm-dd hh24:mi:ss')
and ObserveTime<=to_date('2015-12-31 23:59','yyyy-mm-dd hh24:mi:ss') order by ObserveTime desc) t
inner join
ocean.tsw080101 t2 on t.fsw080101 =t2.psw080101
结论:
其实性能是耗在 inner join以后再排序 就很慢了。所以要先排序,然后再进行inner join 就不会慢了。!!!
调优前:3秒
select rownum RN,tmp2.* from (select * from (select * from (select PSW080303, fsw080101 ,
psw080101, SBMC ,ObserveTimeS,ObserveTimeX,OBSERVETIME
, A3000700200X,A3000700202 , A3005000100 from( select t1.PSW080303, t1.fsw080101 ,psw080101,
SBMC ,ObserveTime as ObserveTimeS, ObserveTime as ObserveTimeX,case t1.A3001200402 when
'0' then to_char( ObserveTime ,'yyyy-mm-dd hh24:mi') when '3' then '<span style=''color:red''>?</span>'
else '<span style=''color:red''>'||to_char( ObserveTime ,'yyyy-mm-dd hh24:mi') ||'</span>' end as OBSERVETIME
,A3000700200 A3000700200X, A3000700202 ,
'<a href="javascript:void(0);"
onclick="openFile(''DFB'','''||A3005000100||''','''||to_char(ObserveTime,'yyyy-mm-dd')||''')" >'
||A3005000100||'</a>'
as A3005000100 from TSW080303 t1
inner join ocean.tsw080101 t2 on t1.fsw080101 = t2.psw080101
and t2.TYPE = '大浮标' and 1=1)) tb where 1=1
and ObserveTimeS>=to_date('2015-11-01 00:00','yyyy-mm-dd hh24:mi:ss')
and ObserveTimeS<=to_date('2015-12-31 23:59','yyyy-mm-dd hh24:mi:ss')
order by ObserveTimeS desc) tmp where rownum<=20 ) tmp2
调优后:0秒
select rownum RN,tmp2.* from (select * from (select * from (select PSW080303, fsw080101 ,
psw080101, SBMC ,ObserveTimeS,ObserveTimeX,OBSERVETIME
, A3000700200X,A3000700202 , A3005000100 from( select t1.*,psw080101,
SBMC from (select t1.PSW080303, t1.fsw080101 ,ObserveTime as ObserveTimeS, ObserveTime as ObserveTimeX,case t1.A3001200402 when
'0' then to_char( ObserveTime ,'yyyy-mm-dd hh24:mi') when '3' then '<span style=''color:red''>?</span>'
else '<span style=''color:red''>'||to_char( ObserveTime ,'yyyy-mm-dd hh24:mi') ||'</span>' end as OBSERVETIME
,A3000700200 A3000700200X, A3000700202 ,
'<a href="javascript:void(0);"
onclick="openFile(''DFB'','''||A3005000100||''','''||to_char(ObserveTime,'yyyy-mm-dd')||''')" >'
||A3005000100||'</a>'
as A3005000100 from TSW080303 t1 where 1=1
and ObserveTime>=to_date('2015-11-01 00:00','yyyy-mm-dd hh24:mi:ss')
and ObserveTime<=to_date('2015-12-31 23:59','yyyy-mm-dd hh24:mi:ss') order by ObserveTimeS desc )t1
inner join ocean.tsw080101 t2 on t1.fsw080101 = t2.psw080101
and t2.TYPE = '大浮标' and 1=1)) tb
) tmp where rownum<=20 ) tmp2
left join 的问题
调优前 4秒
select rownum RN,tmp2.* from
(
select * from
(select * from (
select bb.STCD,
bb.STNM, TM, QW, TQ, FX, FXZ, FS,
FL, ZFFS,ZFFL,NJD,XDSD,HPMQY,JY,REMARK from
(
select t1.STCD, STNM,
TM, QW, TQ, FX, FXZ, FS, FL,
ZFFS,ZFFL,NJD,XDSD,HPMQY,JY,T1.REMARK from ocean.WT_WeatherData_m t1
inner join ocean.WT_WeatherStation t2 on t1.STCD = t2.STCD
and t2.Type = '台湾气象站' and 1=1inner join ( select STCD, max(TM)
maxTM from ocean.WT_WeatherData_m where 1=1 and TM >=
to_date('2016-03-22 15:02','yyyy-MM-dd hh24:mi:ss') and
TM <= to_date('2016-03-24 15:02','yyyy-MM-dd hh24:mi:ss') group by STCD) t3
on t1.STCD = t3.STCD and t1.TM = t3.maxTM
) aa,
(
select STCD,STNM from ocean.WT_WeatherStation
where TYPE = '台湾气象站'
) bb
where aa.STCD(+) = bb.STCD
) tb where 1=1 order by TM DESC
) tmp
where rownum<=20
) tmp2
问题出在 order by TM DESC这个语句上
oracle 单列索引 多列索引的性能测试的更多相关文章
- 认识SQLServer索引以及单列索引和多列索引的不同
一.索引的概念 索引的用途:我们对数据查询及处理速度已成为衡量应用系统成败的标准,而采用索引来加快数据处理速度通常是最普遍采用的优化方法. 索引是什么:数据库中的索引类似于一本书的目录,在一本书中使 ...
- mysql索引之一:索引基础(B-Tree索引、哈希索引、聚簇索引、全文(Full-text)索引区别)(唯一索引、最左前缀索引、前缀索引、多列索引)
没有索引时mysql是如何查询到数据的 索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储10 ...
- mysql 联合索引和唯一索引
一般来说.如果有where a=? and b=? and c=? 的语句. 如果表也有DML, 我一般只在a 上建索引. 这也是代价平衡的结果. 一方面 只在a 上建索引那么是 index ran ...
- Oracle性能优化之oracle里表、索引、列的统计信息
一.表的统计信息 表的统计信息用于描述表的详细信息,包括记录数(num_rows).表块的数量(blocks).平均行长度(avg_row_len)等典型维度.这些维度可以通过数据字典表DBA_TAB ...
- Atitit. 单列索引与多列索引 多个条件的查询原理与设计实现
Atitit. 单列索引与多列索引 多个条件的查询原理与设计实现 1. MySQL只能使用一个索引1 1.1. 最左前缀1 1.2. 从另一方面理解,它相当于我们创建了(firstname,last ...
- Oracle中NULL值与索引
NULL值是关系数据库系统布尔型(true,false,unknown)中比较特殊类型的一种值,通常称为UNKNOWN或空值,即是未知的,不确定的.由于NULL存在着无数的可能,因此NULL值也不等于 ...
- Oracle总结【视图、索引、事务、用户权限、批量操作】
前言 在Oracle总结的第一篇中,我们已经总结了一些常用的SQL相关的知识点了...那么本篇主要总结关于Oralce视图.序列.事务的一些内容... 在数据库中,我们可以把各种的SQL语句分为四大类 ...
- Oracle索引之Btree索引
索引介绍 日常开发中,对于数据的查询如果需要优化,常听说要加个索引.但是为什么加了索引,数据的查询就快了呢?那是不是加了索引就一定会是有效或者有利的呢? Oracle中常见有BTREE索引,位图索引和 ...
- oracle 基础知识(十二)----索引
一, 索引介绍 索引与表一样,也属于段(segment)的一种.里面存放了用户的数据,跟表一样需要占用磁盘空间.索引是一种允许直接访问数据表中某一数据行的树型结构,为了提高查询效率而引入,是一个独立于 ...
随机推荐
- c# 引用百度地图
<script type="text/javascript"> //创建和初始化地图函数 var map = new BMap.Map("home" ...
- String、Stringbuffer、StringBuilder的区别(转载)
最近学习到StringBuffer,心中有好些疑问,搜索了一些关于String,StringBuffer,StringBuilder的东西,现在整理一下. 关于这三个类在字符串处理中的位置不言而喻,那 ...
- 移动web开发和移动app开发的区分
1.移动web开发 这部分跟web前端开发差别不大,使用的技术都是html+css+js.区别为手机浏览器是webkit的天下,pc端是IE的天 下.手机网页可以理解成pc网页的缩小版加一些触摸特性. ...
- CocoaPods报错:The dependency `Alamofire ` is not used in any concrete target
看到这个错误提示,首先看看自己的版本是不是 OS X EI Capitan,也就是10.10以后的版本,因为这个版本是比较新的版本,网络上找的那些安装cocoapod命令其实有些过时了,特别是创建po ...
- nginx 反向代理TCP mysql
stream {upstream mysql { hash $remote_addr consistent; server 10.26.112.12:3306 max_fails=3 fail_tim ...
- linux 删除进程的多种方法
kill pid kill -9 pid kill -15 pid pkill -f *.php kill -s 9 pid
- mysql if case条件更新
在mysql中,如果你要根据某个字段的值不一样,来更新另一个字段的值,可以用如下sql语句: 如果仅仅是两个分支,if语句就可以了 update tm set page_name=if(q_aswer ...
- Microsoft Win32 to Microsoft .NET Framework API Map
Microsoft Win32 to Microsoft .NET Framework API Map .NET Development (General) Technical Articles ...
- stl::map之const函数访问
如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...
- Verilog HDL那些事_建模篇笔记(实验九:VGA驱动)
1.了解VGA协议 VGA协议有5个输入信号,列同步信号(HSYNC Signal),行同步信号(VSYNC Signal),红-绿-蓝,颜色信号(RGB Signal). 一帧屏幕的显示是由行从上至 ...