土拨鼠tc

导航

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这个语句上

posted on 2014-10-30 00:21  土拨鼠tc  阅读(3508)  评论(0编辑  收藏  举报