当前位置:网站首页>Parameter sniffing (1/2)
Parameter sniffing (1/2)
2022-07-07 09:55:00 【knight_ hf】
This problem will be solved in parameter words SQL sentence ( For example, stored procedures ) And SQL Server When the plan caching mechanism in is combined . This article is divided into 2 Parts of , The first 1 Part will introduce Parameters of sniffer (Parameter Sniffing) Overview of , The first 2 In part, let's introduce how to solve this problem .
What is parameter sniffing (Parameter Sniffing)
stay SQL Server When you execute parameters SQL When inquiring , The query optimizer will compile the execution plan based on the first provided parameter value . Then the generated execution plan is cached in the plan cache for later reuse . That is to say SQL Server This plan will be reused directly in the future , Regardless of the different parameter values you provide each time . We need to identify 2 Class parameter values :
- Parameter compilation value (Compile time values)
- Parameter operation value (Runtime values)
The parameter compilation value is the value used to query the optimizer to generate the physical execution plan . The parameter operation value is the value provided to execute the planned operation . These values are consistent for the first execution , But the next execution , These values are likely to be different . This will cause serious performance problems , Because the execution plan is optimized only for compiling values , It is not optimized for the different running values you provide next .
If you provide a specific value during the first query execution , Then the query optimizer selects nonclustered index lookup and bookmark lookup operators to get all query columns from your table . Such an execution plan only makes sense for a specific value , If it is not a specific value , Your logic reading will be very high ,SQL Server Will select full table scan , Ignore defined nonclustered indexes .SQL Server Choose this 2 The decision point of a plan is the so-called critical point (Tipping Point) .
If the bookmark lookup plan is cached ,SQL Server You won't care about the input value , Blindly reuse cached plans . In this case SQL Server The protection mechanism of is invalid , Only execute cached plans from the plan cache . As a side effect , Yours IO cost ( Logical capital ) It'll burst your watch , The performance of queries will be very poor . Let's demonstrate this situation , The following script will create a simple table , At the bottom of the table 2 There is an uneven data distribution ( Just the second 1 The entry value is 1, The rest 1499 All values are 2).
1 -- Create a test table 2 CREATE TABLE Table1 3 ( 4 Column1 INT IDENTITY, 5 Column2 INT 6 ) 7 GO 8 9 CREATE NONCLUSTERED INDEX idx_Test ON Table1(Column2) 10 11 -- Insert 1500 records into Table1 12 INSERT INTO Table1 (Column2) VALUES (1) 13 14 SELECT TOP 1499 IDENTITY(INT, 1, 1) AS n INTO #Nums 15 FROM 16 master.dbo.syscolumns sc1 17 18 INSERT INTO Table1 (Column2) 19 SELECT 2 FROM #nums 20 DROP TABLE #nums 21 GO
Based on this uneven data distribution and critical point , For the same logical query, there will be 2 Different execution plans , Click on the toolbar
The display contains the actual execution plan :
1 SELECT * FROM dbo.Table1 WHERE Column2=1 2 SELECT * FROM dbo.Table1 WHERE Column2=2

Now when you create a stored procedure , The query optimizer will generate an execution plan based on the parameter values provided for the first time , Then it will be blindly reused in the next execution .
1 -- Create a new stored procedure for data retrieval 2 CREATE PROCEDURE RetrieveData 3 ( 4 @Col2Value INT 5 ) 6 AS 7 SELECT * FROM Table1 8 WHERE Column2 = @Col2Value 9 GO
1 SET STATISTICS IO ON 2 EXEC dbo.RetrieveData @Col2Value = 1 -- int 3 EXEC dbo.RetrieveData @Col2Value = 2 -- int


Now when you use 1 Value when running stored procedures , Only return 1 Bar record , The query optimizer selects bookmarks in the execution plan to find . Queries only produce 3 A logical reading . But when you use 2 Value when running stored procedures , Cached plans are reused , Bookmark search is performed repeatedly 1499 Time . Execute on each record ! The query is now generated 1505 A logical reading . This is completely different from the implementation just now . When you look 2 In the implementation plan ,SELECT Operator properties , In the parameter list, you can see :
As you can see, they are different , The parameter compilation value is 1, The parameter operation value is 2. That is to say, the execution in front of you is based on the parameter value 1 And optimized , But in fact, the parameter value you pass to the stored procedure is 2. This is it. SQL Server Inside Parameters of sniffer (Parameter Sniffing) problem .
Summary
As you can see , stay SQL Server It's easy to encounter this problem in . Every time you use parameters SQL Inquire about ( Like in a stored procedure ), When the table data is unevenly distributed , When the provided nonclustered index does not overwrite the query column , You will encounter this problem . Here we only introduce this problem , I will show you in the next article How to deal with this problem , namely SQL Server What solutions have been provided to you to solve this problem .
—— Reproduced in : http://www.cnblogs.com/woodytu/p/4551004.html
边栏推荐
- C# XML的应用
- Bean 作⽤域和⽣命周期
- Can't connect to MySQL server on '(10060) solution summary
- In fact, it's very simple. It teaches you to easily realize the cool data visualization big screen
- MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
- web3.0系列之分布式存储IPFS
- Thinkphp3.2 information disclosure
- 2016 CCPC Hangzhou Onsite
- Win10安装VS2015
- How will fashion brands enter the meta universe?
猜你喜欢

AI moves from perception to intelligent cognition

The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations

小程序滑动、点击切换简洁UI

Impression notes finally support the default markdown preview mode

小程序弹出半角遮罩层

【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
![[4G/5G/6G专题基础-147]: 6G总体愿景与潜在关键技术白皮书解读-2-6G发展的宏观驱动力](/img/21/6a183e4e10daed90c66235bdbdc3bf.png)
[4G/5G/6G专题基础-147]: 6G总体愿景与潜在关键技术白皮书解读-2-6G发展的宏观驱动力

Lecture 1: stack containing min function

JS逆向教程第一发
![[untitled]](/img/5b/61efbaded29250bc8d921b0cf087c8.png)
[untitled]
随机推荐
thinkphp数据库的增删改查
Hcip first day notes sorting
Use 3 in data modeling σ Eliminate outliers for data cleaning
能源路由器入门必读:面向能源互联网的架构和功能
【frida实战】“一行”代码教你获取WeGame平台中所有的lua脚本
MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
[untitled]
**Grafana installation**
20排位赛3
La différence entre viewpager 2 et viewpager et la mise en œuvre de la rotation viewpager 2
基础篇:带你从头到尾玩转注解
2016 CCPC Hangzhou Onsite
Can flycdc use SqlClient to specify mysqlbinlog ID to execute tasks
Basic chapter: take you through notes
农牧业未来发展蓝图--垂直农业+人造肉
如何成为一名高级数字 IC 设计工程师(5-3)理论篇:ULP 低功耗设计技术精讲(下)
Thinkphp3.2 information disclosure
Application of C # XML
# Arthas 简单使用说明
How will fashion brands enter the meta universe?
