SQL 性能调优

Posted pealy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL 性能调优相关的知识,希望对你有一定的参考价值。

1. Select(1) 优于 Select(*) 

2.In and Exist

in是给外表和内表做hash链接,而Exist是对外表做Loop循环,每次loop循环再做内表查询,如果两个表大小相似,in和Exists差别不大.

如果两个表中一个表大一个表小,子查询大的用Exist,子查询小的用in.

3.计算表中指定时间段的行数,通过先挑出这段时间的最大最小值 然后count(id),如下:DataPointPerSensor.sql (33 minutes) DPNumberPerSensor.sql(16 minutes)

DataPointPerSensor.sql

技术分享
--this script used to calculate different sensor type of datapoint
select
count(Mll.ID) as [Loc]
from [Tracks].[dbo].[MonitorLocationLog] MLL
where MLL.RowCreatedOn >= 2016-01-01 
and MLL.RowCreatedOn <= 2017-01-01 

select 
count(1) as [Latitude]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=Latitude and 
MSL.RowCreatedOn >= 2016-01-01 
and MSL.RowCreatedOn <= 2017-01-01

select 
count(1) as [TemperatureExternal]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=TemperatureExternal and 
MSL.RowCreatedOn >= 2016-01-01 
and MSL.RowCreatedOn <= 2017-01-01

select 
count(1) as [TemperatureInternal]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=TemperatureInternal and 
MSL.RowCreatedOn >= 2016-01-01 
and MSL.RowCreatedOn <= 2017-01-01


select 
count(1) as [BatteryExternal]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=BatteryExternal and 
MSL.RowCreatedOn >= 2016-01-01 
and MSL.RowCreatedOn <= 2017-01-01



select 
count(1) as [BatteryInternal]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=BatteryInternal and 
MSL.RowCreatedOn >= 2016-01-01 
and MSL.RowCreatedOn <= 2017-01-01

select 
count(1) as [Rssi]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=Rssi and 
MSL.RowCreatedOn >= 2016-01-01 
and MSL.RowCreatedOn <= 2017-01-01


select 
count(1) as [Motion]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=Motion and 
MSL.RowCreatedOn >= 2016-01-01 
and MSL.RowCreatedOn <= 2017-01-01


select 
count(1) as [MotionInferred]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=MotionInferred and 
MSL.RowCreatedOn >= 2016-01-01 
and MSL.RowCreatedOn <= 2017-01-01
View Code

DPNumberPerSensor.sql

技术分享
--this script used to calculate different sensor type of datapoint
declare @firstLocID nvarchar(100)
declare @lastLocID nvarchar(100)
declare @firstSensorID nvarchar(100)
declare @lastSensorID nvarchar(100)


set @firstLocID=(select top 1 (ID) from [Tracks].[dbo].[MonitorLocationLog] MLL
where mll.RowCreatedOn>=2016-01-01
order by id)

set @lastLocID=(select top 1(ID) as lastID from [Tracks].[dbo].[MonitorLocationLog] MLL
where mll.RowCreatedOn<=2017-01-01
order by id desc)


set @firstSensorID=(select top 1 (ID) from [Tracks].[dbo].[MonitorSensorLog] MSL
where MSL.RowCreatedOn>=2016-01-01
order by id)

set @lastSensorID=(select top 1(ID) as lastID from [Tracks].[dbo].[MonitorSensorLog] MSL
where MSL.RowCreatedOn<=2017-01-01
order by id desc)

begin

select
count(Mll.ID) as [Loc]
from [Tracks].[dbo].[MonitorLocationLog] MLL
where MLL.ID >= @firstLocID
and MLL.ID <= @lastLocID 


select 
count(1) as [TemperatureExternal]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=TemperatureExternal and 
MSL.ID >= @firstSensorID
and MSL.ID <= @lastSensorID 

select 
count(1) as [TemperatureInternal]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=TemperatureInternal and 
MSL.ID >= @firstSensorID
and MSL.ID <= @lastSensorID 


select 
count(1) as [Light]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=Light and 
MSL.ID >= @firstSensorID
and MSL.ID <= @lastSensorID 

select 
count(1) as [BatteryExternal]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=BatteryExternal and 
MSL.ID >= @firstSensorID
and MSL.ID <= @lastSensorID 



select 
count(1) as [BatteryInternal]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=BatteryInternal and 
MSL.ID >= @firstSensorID
and MSL.ID <= @lastSensorID 

select 
count(1) as [Rssi]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=Rssi and 
MSL.ID >= @firstSensorID
and MSL.ID <= @lastSensorID 


select 
count(1) as [Motion]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=Motion and 
MSL.ID >= @firstSensorID
and MSL.ID <= @lastSensorID 


select 
count(1) as [MotionInferred]
from [Tracks].[dbo].[MonitorSensorLog] MSL
where msl.SensorType=MotionInferred and 
MSL.ID >= @firstSensorID
and MSL.ID <= @lastSensorID

end 
View Code

4.Union VS Union All
Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序。

Union All:对两个结果集进行并集操作,包含重复行,不进行排序。

INTERSECT:是两个查询结果的交集 对两个结果集进行交集操作,不包括重复行,重复的会被过滤,同时进行默认规则的排序。

Minus:对两个结果集进行差操作,返回的总是左边表中的数据且不包括重复行,重复的会被过滤,同时进行默认规则的排序。

来看下列:表scfrd_type

id         code

1             A

2             B

表scfrd_type1

id         code

2             B

3             C

查询语句select id,code fromscfrd_type  union select id,code from scfrd_type1。结果过滤了重复的行,如下:

id         code

1             A

2             B

3             C

查询语句select id,code fromscfrd_type  union  all select id,code from scfrd_type1。结果没有过滤了重复的行,如下:

id         code

1             A

2             B

2             B

3             C

查询语句select id,code fromscfrd_type  intersect select id,code from scfrd_type1。结果如下:

id         code

2             B

查询语句select id,code fromscfrd_type minus select id,code from scfrd_type1。结果如下:

id         code

1             A


以上是关于SQL 性能调优的主要内容,如果未能解决你的问题,请参考以下文章

《高性能SQL调优精要与案例解析》一书谈主流关系库SQL调优(SQL TUNING或SQL优化)核心机制之——索引(index)

Oracle SQL调优

14.VisualVM使用详解15.VisualVM堆查看器使用的内存不足19.class文件--文件结构--魔数20.文件结构--常量池21.文件结构访问标志(2个字节)22.类加载机制概(代码片段

性能调优单接口性能调优预备阶段

性能调优单接口性能调优预备阶段

性能调优 SQL - 如何?