久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

參數化 SQL 和 NULL 運行緩慢

Parameterized SQL and NULL running slow(參數化 SQL 和 NULL 運行緩慢)
本文介紹了參數化 SQL 和 NULL 運行緩慢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在從 .net 調用一些參數化的 sql.我不知道為什么,但是在檢查參數是否為 NULL 時,與不包含參數時相比,sql 運行速度很慢:

I'm calling some parametrized sql from .net. I'm not sure why but the sql runs quite slow when checking if the parameter is NULL compared to when its not included:

所以:

exec sp_executesql N'
 SELECT [id]
 FROM [tblAddress] (nolock)
 WHERE 1 = 1
 AND ([id] = @id OR @id  IS NULL)

',N'@id int',
@id=4395

比這個跑得更快:

exec sp_executesql N'
 SELECT [id]
 FROM [tblAddress] (nolock)
 WHERE 1 = 1
 AND ([id] = @id)

',N'@id int',
@id=4395

運行 SQL 探查器,超過 100 萬行的頂級查詢的持續時間為 175,讀取次數為 3720,但第二個查詢的持續時間為 1,只有 3 次讀取.

Running SQL profiler the duration of the top query over 1 million rows is 175 and its reads are 3720 but the second query's duration is 1 and only 3 reads.

為什么會有這樣的差異,如何改進?

Why such a difference and how could it be improved?

推薦答案

OR 子句不是 SARGABLE,所以使用的計劃有一個掃描,而不是像第二個那樣的搜索

An OR clause isn't SARGABLE so the plan used has a scan, not a seek like the 2nd one

試試這個:2 次搜索

SELECT [id]
 FROM [tblAddress]
 WHERE [id] = @id
UNION ALL
SELECT [id]
 FROM [tblAddress] 
 WHERE @id IS NULL)

注意:您不需要 NOLOCK 提示.或者 1=1

Note: you don't need the NOLOCK hint. Or 1=1

這篇關于參數化 SQL 和 NULL 運行緩慢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 色本道 | 欧美成人精品一区 | 日韩午夜激情 | 国产亚洲精品精品国产亚洲综合 | 精品中文字幕久久 | 久久精品亚洲精品国产欧美 | www.国产91| 91九色porny首页最多播放 | 欧美性猛交一区二区三区精品 | 精品久久99 | 日韩区| 99精品一区二区 | 亚洲国产欧美精品 | 国产偷久久一级精品60部 | 一二三区av | 欧美精品一区二区三区在线播放 | 天天干夜夜| 国产精品视频一二三区 | 在线看片国产 | 在线免费观看a级片 | 亚洲人成人一区二区在线观看 | 欧美aⅴ| 欧美精品一二三区 | 我爱操| 免费小视频在线观看 | 欧美极品一区二区 | 色资源在线视频 | 91视频一区二区三区 | 久草院线 | 色综合99 | 亚洲精品视频久久 | 黄色一级电影在线观看 | 中文字幕日韩专区 | 日韩视频二区 | 日本欧美视频 | 日韩在线一区二区三区 | 伊人精品国产 | 亚洲区中文字幕 | 男女视频免费 | 日韩精品久久一区二区三区 | 日日干夜夜干 |