1. 程式人生 > >SQL 性能調優

SQL 性能調優

poi close all closed temp lap opened reat sensor

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 性能調優