資料庫連表查詢中的nvarchar類型字段,tb_Users.Certificatesis not null條件,is not null 會導緻查詢速度慢很多(因為和“=”号條件周遊方式不一樣)。
替換為 “LEN(tb_Users.Certificates) >0”,利用 Users.Certificates為空時整個計算傳回false,達到篩選效果。有其他更好的處理方式,有興趣可以留言讨論一下。
當然,datetime類型也是可以用這個方式的:
declare @test datetime
set @test=getdate()
if(LEN(@test) >0)
begin
print 'true'
end
else
begin
print 'false'
end
結果為true.
declare @test datetime
set @test=null
if(LEN(@test) >0)
begin
print 'true'
end
else
begin
print 'false'
end
結果為false.
原因:
使用LEN(tb_Users.Certificates) >0隻有1次掃描,44實體查找執行:

用is not null ,43實體掃描,352實體查找執行
--檢視sql執行時間
SET STATISTICS PROFILE ON
SET STATISTICS IO ON
SET STATISTICS TIME ON
--執行的sql語句
SET STATISTICS PROFILE OFF
SET STATISTICS IO OFF
SET STATISTICS TIME OFF