SQL 效能調優

來源:互聯網
上載者:User

標籤:blog   track   ash   ber   inter   varchar   minus   hash   並集   

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 datapointselectcount(Mll.ID) as [Loc]from [Tracks].[dbo].[MonitorLocationLog] MLLwhere MLL.RowCreatedOn >= ‘2016-01-01‘ and MLL.RowCreatedOn <= ‘2017-01-01‘ select count(1) as [Latitude]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘Latitude‘ and MSL.RowCreatedOn >= ‘2016-01-01‘ and MSL.RowCreatedOn <= ‘2017-01-01‘select count(1) as [TemperatureExternal]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘TemperatureExternal‘ and MSL.RowCreatedOn >= ‘2016-01-01‘ and MSL.RowCreatedOn <= ‘2017-01-01‘select count(1) as [TemperatureInternal]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘TemperatureInternal‘ and MSL.RowCreatedOn >= ‘2016-01-01‘ and MSL.RowCreatedOn <= ‘2017-01-01‘select count(1) as [BatteryExternal]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘BatteryExternal‘ and MSL.RowCreatedOn >= ‘2016-01-01‘ and MSL.RowCreatedOn <= ‘2017-01-01‘select count(1) as [BatteryInternal]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘BatteryInternal‘ and MSL.RowCreatedOn >= ‘2016-01-01‘ and MSL.RowCreatedOn <= ‘2017-01-01‘select count(1) as [Rssi]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘Rssi‘ and MSL.RowCreatedOn >= ‘2016-01-01‘ and MSL.RowCreatedOn <= ‘2017-01-01‘select count(1) as [Motion]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘Motion‘ and MSL.RowCreatedOn >= ‘2016-01-01‘ and MSL.RowCreatedOn <= ‘2017-01-01‘select count(1) as [MotionInferred]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere 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 datapointdeclare @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] MLLwhere mll.RowCreatedOn>=‘2016-01-01‘order by id)set @lastLocID=(select top 1(ID) as lastID from [Tracks].[dbo].[MonitorLocationLog] MLLwhere mll.RowCreatedOn<=‘2017-01-01‘order by id desc)set @firstSensorID=(select top 1 (ID) from [Tracks].[dbo].[MonitorSensorLog] MSLwhere MSL.RowCreatedOn>=‘2016-01-01‘order by id)set @lastSensorID=(select top 1(ID) as lastID from [Tracks].[dbo].[MonitorSensorLog] MSLwhere MSL.RowCreatedOn<=‘2017-01-01‘order by id desc)beginselectcount(Mll.ID) as [Loc]from [Tracks].[dbo].[MonitorLocationLog] MLLwhere MLL.ID >= @firstLocIDand MLL.ID <= @lastLocID select count(1) as [TemperatureExternal]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘TemperatureExternal‘ and MSL.ID >= @firstSensorIDand MSL.ID <= @lastSensorID select count(1) as [TemperatureInternal]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘TemperatureInternal‘ and MSL.ID >= @firstSensorIDand MSL.ID <= @lastSensorID select count(1) as [Light]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘Light‘ and MSL.ID >= @firstSensorIDand MSL.ID <= @lastSensorID select count(1) as [BatteryExternal]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘BatteryExternal‘ and MSL.ID >= @firstSensorIDand MSL.ID <= @lastSensorID select count(1) as [BatteryInternal]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘BatteryInternal‘ and MSL.ID >= @firstSensorIDand MSL.ID <= @lastSensorID select count(1) as [Rssi]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘Rssi‘ and MSL.ID >= @firstSensorIDand MSL.ID <= @lastSensorID select count(1) as [Motion]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘Motion‘ and MSL.ID >= @firstSensorIDand MSL.ID <= @lastSensorID select count(1) as [MotionInferred]from [Tracks].[dbo].[MonitorSensorLog] MSLwhere msl.SensorType=‘MotionInferred‘ and MSL.ID >= @firstSensorIDand MSL.ID <= @lastSensorIDend 
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 效能調優

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.