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

參數(shù)化 SQL 和 NULL 運行緩慢

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

問題描述

我正在從 .net 調(diào)用一些參數(shù)化的 sql.我不知道為什么,但是在檢查參數(shù)是否為 NULL 時,與不包含參數(shù)時相比,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 萬行的頂級查詢的持續(xù)時間為 175,讀取次數(shù)為 3720,但第二個查詢的持續(xù)時間為 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

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

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計算值創(chuàng)建計算值)
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屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 亚洲三级av | 国产综合视频 | 欧美三区| 一级午夜aaa免费看三区 | 国产精品久久久久久久久久免费看 | 天天综合91 | 在线观看亚洲专区 | 亚洲精品白浆高清久久久久久 | 欧美视频一区二区三区 | 久草网站 | 成年人在线 | 性色综合 | 亚洲国产中文在线 | 亚洲视频中文字幕 | 国产精品99久久久久久宅男 | 亚洲精品视频在线 | 色一级片| 日本午夜在线视频 | 婷婷精品| 亚洲高清在线 | 亚洲精品久久久久久国产精华液 | 中文字幕欧美在线观看 | 99精品国自产在线观看 | 国产美女一区二区三区 | 成人av电影在线 | 欧美中文字幕在线观看 | 亚洲夜夜爽 | 久久久成人精品 | 能看的av | 国产一区二区欧美 | 午夜视频网 | 久久精品无码一区二区三区 | 欧美精产国品一二三区 | 99久久日韩精品免费热麻豆美女 | 欧美日韩一区二区三区不卡视频 | 亚洲国产精品久久久 | 亚洲国产精品久久久久 | 精品成人一区二区 | 国产午夜影院 | 在线亚洲免费视频 | 插插插干干干 |