当前位置:网站首页>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
边栏推荐
- Pit using BigDecimal
- 请教个问题,我用sql-client起了个同步任务,从MySQL同步到ADB,历史数据有正常同步过去
- Flex flexible layout
- Hcip first day notes sorting
- 如何成为一名高级数字 IC 设计工程师(5-3)理论篇:ULP 低功耗设计技术精讲(下)
- Main (argc, *argv[]) details
- How to use clipboard JS library implements copy and cut function
- **Grafana installation**
- 小程序弹出半角遮罩层
- [4G/5G/6G专题基础-147]: 6G总体愿景与潜在关键技术白皮书解读-2-6G发展的宏观驱动力
猜你喜欢
What development models did you know during the interview? Just read this one
【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
Garbage disposal method based on the separation of smart city and storage and living digital home mode
细说Mysql MVCC多版本控制
PLC信号处理系列之开关量信号防抖FB
web3.0系列之分布式存储IPFS
JS逆向教程第一发
Pytest learning - dayone
[4G/5G/6G专题基础-147]: 6G总体愿景与潜在关键技术白皮书解读-2-6G发展的宏观驱动力
AI从感知走向智能认知
随机推荐
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
Write it into the SR table in the way of flinksql. It is found that the data to be deleted has not been deleted. Refer to the document https://do
CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)
JS reverse tutorial second issue - Ape anthropology first question
Use 3 in data modeling σ Eliminate outliers for data cleaning
哈夫曼编码压缩文件
Basic chapter: take you through notes
CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
Flinkcdc failed to collect Oracle in the snapshot stage. How do you adjust this?
请教个问题,我用sql-client起了个同步任务,从MySQL同步到ADB,历史数据有正常同步过去
Bean 作⽤域和⽣命周期
[bw16 application] Anxin can realize mqtt communication with bw16 module / development board at instruction
【无标题】
Impression notes finally support the default markdown preview mode
Analyze Android event distribution mechanism according to popular interview questions (I)
2020浙江省赛
Write VBA in Excel, connect to Oracle and query the contents in the database
Difference between process and thread
Luogu p2482 [sdoi2010] zhuguosha
flink. CDC sqlserver. 可以再次写入sqlserver中么 有连接器的 dem