SQLServer 優(yōu)化SQL語(yǔ)句 in 和not in的替代方案
發(fā)布日期:2022-01-29 18:51 | 文章來(lái)源:CSDN
SQL試圖將其轉(zhuǎn)換成多個(gè)表的連接,如果轉(zhuǎn)換不成功則先執(zhí)行IN里面的子查詢,再查詢外層的表記錄,如果轉(zhuǎn)換成功則直接采用多個(gè)表的連接方式查詢。由此可見(jiàn)用IN的SQL至少多了一個(gè)轉(zhuǎn)換的過(guò)程。一般的SQL都可以轉(zhuǎn)換成功,但對(duì)于含有分組統(tǒng)計(jì)等方面的SQL就不能轉(zhuǎn)換了。 推薦在業(yè)務(wù)密集的SQL當(dāng)中盡量不采用IN操作符
NOT IN 此操作是強(qiáng)列推薦不使用的,因?yàn)樗荒軕?yīng)用表的索引。推薦用NOT EXISTS 或(外連接+判斷為空)方案代替
在數(shù)據(jù)庫(kù)中有兩個(gè)表,一個(gè)是當(dāng)前表Info(id,PName,remark,impdate,upstate),一個(gè)是備份數(shù)據(jù)表bakInfo(id,PName,remark,impdate,upstate),將當(dāng)前表數(shù)據(jù)備份到備份表去,就涉及到not in 和in 操作了:
首先,添加10萬(wàn)條測(cè)試數(shù)據(jù)
復(fù)制代碼 代碼如下:
create procedure AddData
as
declare @id int
set @id=0
while(@id<100000)
begin
insert into dbo.Info(id,PName,remark,impdate,upstate)
values(@id,convert(varchar,@id)+'0','abc',getdate(),0)
set @id=@id+1
end
exec AddData
使用not in 和in操作:
復(fù)制代碼 代碼如下:
SET STATISTICS TIME ON
GO
--備份數(shù)據(jù)
insert into bakInfo(id,PName,remark,impdate,upstate)
select id,PName,remark,impdate,upstate from dbo.Info
where id not in(select id from dbo.bakInfo)
GO
SET STATISTICS TIME OFF
此操作執(zhí)行時(shí)間:
復(fù)制代碼 代碼如下:
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 0 毫秒,占用時(shí)間 = 3 毫秒。
SQL Server 執(zhí)行時(shí)間:
CPU 時(shí)間 = 453 毫秒,占用時(shí)間 = 43045 毫秒。
(100000 行受影響)
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 0 毫秒,占用時(shí)間 = 1 毫秒。
--更改當(dāng)前表狀態(tài)
update Info set upstate=1 where id in(select id from dbo.bakInfo)
此操作執(zhí)行時(shí)間:
復(fù)制代碼 代碼如下:
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 62 毫秒,占用時(shí)間 = 79 毫秒。
SQL Server 執(zhí)行時(shí)間:
CPU 時(shí)間 = 188 毫秒,占用時(shí)間 = 318 毫秒。
(100000 行受影響)
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 0 毫秒,占用時(shí)間 = 1 毫秒。
--刪除當(dāng)前表數(shù)據(jù)
delete from Info where upstate=1 and id in(select id from dbo.bakInfo)
此操作執(zhí)行時(shí)間:
復(fù)制代碼 代碼如下:
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 183 毫秒,占用時(shí)間 = 183 毫秒。
SQL Server 執(zhí)行時(shí)間:
CPU 時(shí)間 = 187 毫秒,占用時(shí)間 = 1506 毫秒。
(100000 行受影響)
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 0 毫秒,占用時(shí)間 = 1 毫秒。
使用join連接替代方案:
復(fù)制代碼 代碼如下:
SET STATISTICS TIME ON
GO
--備份數(shù)據(jù)
insert into bakInfo(id,PName,remark,impdate,upstate)
select id,PName,remark,impdate,upstate from
(SELECT Info.id,Info.PName, Info.remark, Info.impdate,Info.upstate, bakInfo.id AS bakID
FROM Info left JOIN
bakInfo ON Info.id = bakInfo.id ) as t
where t.bakID is null and t.upstate=0
GO
SET STATISTICS TIME OFF;
此操作執(zhí)行時(shí)間:
復(fù)制代碼 代碼如下:
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 247 毫秒,占用時(shí)間 = 247 毫秒。
SQL Server 執(zhí)行時(shí)間:
CPU 時(shí)間 = 406 毫秒,占用時(shí)間 = 475 毫秒。
(100000 行受影響)
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 0 毫秒,占用時(shí)間 = 1 毫秒。
--更改當(dāng)前表狀態(tài)
update Info set upstate=1
FROM Info INNER JOIN
bakInfo ON Info.id = bakInfo.id
此操作執(zhí)行時(shí)間:
復(fù)制代碼 代碼如下:
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 4 毫秒,占用時(shí)間 = 4 毫秒。
SQL Server 執(zhí)行時(shí)間:
CPU 時(shí)間 = 219 毫秒,占用時(shí)間 = 259 毫秒。
(100000 行受影響)
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 0 毫秒,占用時(shí)間 = 1 毫秒。
--刪除當(dāng)前表數(shù)據(jù)
復(fù)制代碼 代碼如下:
delete from Info
FROM Info INNER JOIN
bakInfo ON Info.id = bakInfo.id
where Info.upstate=1
此操作執(zhí)行時(shí)間:
復(fù)制代碼 代碼如下:
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 177 毫秒,占用時(shí)間 = 177 毫秒。
SQL Server 執(zhí)行時(shí)間:
CPU 時(shí)間 = 219 毫秒,占用時(shí)間 = 550 毫秒。
(100000 行受影響)
SQL Server 分析和編譯時(shí)間:
CPU 時(shí)間 = 0 毫秒,占用時(shí)間 = 1 毫秒。
可以看出使用join方案比使用not in 和in執(zhí)行時(shí)間要短很多了
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信