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)的一种.里面存放了用户的数据,跟表一样需要占用磁盘空间.索引是一种允许直接访问数据表中某一数据行的树型结构,为了提高查询效率而引入,是一个独立于 ...
随机推荐
- java 队列基础操作
http://www.cnblogs.com/fuck1/p/5996116.html 队列(简称作队,Queue)也是一种特殊的线性表,队列的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别 ...
- 数论 UVALive 2911
这道题是一道数论题. 题目的意思是告诉m.p.a.b,并且告诉你xi满足的两个条件.让你求出 xp1 + xp2 +...+ xpm 的最大值(其中p<=12,切p是偶数). 这里需要对于xi所 ...
- debug不过的程序
下面的程序debug是不能通过的. 至于为什么我还不知道. assume cs:codesg codesg segment start: mov ax,2000h mov ss, ax mov sp, ...
- CPU的栈机制的一个小问题
比如要实现下面这个功能. 我们如果要在10000H处写入自行数据2266H,不能用“mov 内存单元, 寄存器”这类指令.怎么做? 代码: mov ax, 1000h mov ss, ax mov s ...
- jquery实现AJAX的4种方法
当我们用javascript写ajax程序写得很“开心”的时候,突然有人告诉你有一种东西叫jquery,它会告诉你不直接和 HttpRequest是多么的快乐,同时你再也不需要再烦恼纠结的ajax乱码 ...
- Java BIO、NIO、AIO 学习(转)
转自 http://stevex.blog.51cto.com/4300375/1284437 先来个例子理解一下概念,以银行取款为例: 同步 : 自己亲自出马持银行卡到银行取钱(使用同步IO时,Ja ...
- tomcat7 启动项目报错 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()
JDK版本:jdk1.8.0_77 Tomcat 版本:apache-tomcat-7.0.47 异常重现步骤: 1.完成项目部署 2.启动Tomcat 异常头部信息:java.lang.NoSuch ...
- php7 编译安装 apache
http://blog.csdn.net/21aspnet/article/details/47708763 根据此教程的步骤但是碰到了若干问题 1. 执行./configure的时候报错 大部分可 ...
- 关于flume配置加载
最近项目在用到flume,因此翻了下flume的代码, 启动脚本: nohup bin/flume-ng agent -n tsdbflume -c conf -f conf/配置文件.conf -D ...
- HTML5桌面通知:notification
最近由于公司业务需要,领导要求IM消息有像网页微信那样有新消息桌面右下角弹出一个提示框的效果!由于自己才疏学浅,一时还没明白微信是怎么实现的!所以只能问百度(因为懒得FQ)咯! 在网上搜索了N久,心都 ...