当前位置:网站首页>Parameter sniffing (2/2)
Parameter sniffing (2/2)
2022-07-07 10:05:00 【knight_ hf】
stay Parameters of sniffer (Parameter Sniffing)(1/2) in , I introduced SQL Server The basic concept and problems behind parameter sniffing . As you can see , When the cached plan is SQL Server When reusing blindly , Can cause serious performance problems . Today I will show you how to deal with this problem , That is to use different technologies to overcome it .
Indexes (Index)
Last time we discussed that the root cause of the parameter sniffing problem is : In the execution plan ,SQL Statement sometimes produces bookmark lookup , Sometimes a table is generated / Clustered index scan . If you can modify the index in the database , The simplest way to solve this problem is to provide the corresponding Overwrite nonclustered indexes . Here we will include the required columns for bookmark search , In the leaf layer of nonclustered indexes . After doing this , You can get Plan stability : Regardless of any parameters of the input provided , The query optimizer can compile the same execution plan —— It is used here Index lookup ( Nonclustered indexes ) Operator .
1 DROP INDEX idx_Test ON Table1 2 CREATE NONCLUSTERED INDEX idx_Test ON Table1(Column2) INCLUDE(Column1) 3 4 SELECT * FROM dbo.Table1 WHERE Column2=1 5 SELECT * FROM dbo.Table1 WHERE Column2=2
If you can't modify your index design , Try the following :
Recompile (Recompilation)
SQL Server The first option provided to you is to perform the recompilation of the plan . It provides 2 There are different options for you to use :
- Recompile all , The whole stored procedure
- There is a problem SQL Statement recompile , That is to say Statement level recompilation ( from SQL Server 2005 Available from )
Let's explain this in detail through examples 2 An option . The following statement will recompile the entire stored procedure :
1 -- Create a new stored procedure for data retrieval 2 CREATE PROCEDURE RetrieveDataR 3 ( 4 @Col2Value INT 5 ) 6 WITH RECOMPILE 7 AS 8 SELECT * FROM Table1 9 WHERE Column2 = @Col2Value 10 GO
When you execute such a stored procedure , The query optimizer recompiles the stored procedure before each execution . Therefore, the execution plans you get are based on the currently entered parameter values . As a side effect of recompilation , Your execution plan will not be cached , Caching an execution plan that is recompiled every time is meaningless . When you have a large complex stored procedure used at the stored procedure level RECOMPILE Options , It doesn't make much sense to do so , Because of you. Whole Store recompile every time , And the stored procedure is for compilation and reuse , So as to improve the execution efficiency .
1 EXEC dbo.RetrieveDataR @Col2Value = 1 -- int 2 EXEC dbo.RetrieveDataR @Col2Value = 2 -- int
If your parameter sniffing problem only occurs in a specific SQL sentence . Then there is no need to recompile the whole stored procedure . So from SQL Server2005 Start , The offer is called Statement level recompilation (Statement Level Recompilation) . You can recompile those that need to be recompiled SQL Sentence plus RECOMPILE Query prompts instead of the entire stored procedure . Let's take a look at the following code :
1 -- Create a new stored procedure for data retrieval 2 CREATE PROCEDURE RetrieveDataR2 3 ( 4 @Col2Value INT 5 ) 6 AS 7 SELECT * FROM Table1 8 WHERE Column2 = @Col2Value 9 10 SELECT * FROM Table1 11 WHERE Column2 = @Col2Value 12 OPTION (RECOMPILE) 13 GO
The first in the above example 2 individual SQL Statements will be recompiled when the stored procedure is executed . The first 1 Statements are compiled at the beginning of execution , And generate a plan cache for subsequent reuse . When you don't want to modify the index of the database , This method is the recommended method for handling parameter sniffing .
1 EXEC dbo.RetrieveDataR2 @Col2Value = 2 -- int
OPTIMIZE FOR
Except stored procedures or SQL Recompile query prompt of statement ,SQL Server Also provide OPTIMIZE FOR Query prompt . With this query prompt, you can tell the query optimizer which parameter value , Optimize the execution plan , Let's look at the following example :
1 -- Create a new stored procedure for data retrieval 2 CREATE PROCEDURE RetrieveDataOF 3 ( 4 @Col2Value INT 5 ) 6 AS 7 SELECT * FROM Table1 8 WHERE Column2 = @Col2Value 9 OPTION (OPTIMIZE FOR (@Col2Value = 1)) 10 GO
From the definition of stored procedure, you can see ,SQL The execution plan of the statement is in the parameter @Col2Value The value is 1 It needs to be optimized . No matter what value you provide to this parameter , You get value 1 Optimized compilation plan . In this way, you have been right SQL Server Zoom in , Because the query optimizer has no other options —— It must be a parameter value 1 Generate optimized execution plan . When you know that the query plan needs to be optimized for the specified parameters , You can use this method to make SQL Server Optimize the execution plan of this parameter . After you restart SQL Server Or perform cluster failover , You can predict your implementation plan .
To further ensure the effectiveness of this option , You need to be familiar with your data distribution , When will the data distribution change . If the data distribution has changed , You have to modify the query prompt , See if it's still appropriate . You can't completely trust the query optimizer , Because you have used OPTIMIZE FOR Query prompt resets the selection of the query optimizer . Remember this . In addition, we are providing OPTIMIZE FOR While querying the prompt ,SQL Server Also provide OPTIMIZE FOR UNKNOWN Query hints . If you decide to use OPTIMIZE FOR UNKNOWN Query hints , The query optimizer uses the density in the table statistics to make parameter estimation . If the logical reading exceeds critical point , Still use tables / An index scan ……
Summary
In this article, I show you in SQL Server Different ways to deal with parameter sniffing in . The most common cause of this problem is poor index design , After the parameter value is passed in, the optimizer selects bookmark search in the execution plan . If such an execution plan is reused by the cache , Yours I/O The cost will explode . In the build environment , I saw that for this reason 100GB The logic of reading . stay SQL Add a simple RECOMPILE Query tips can solve this problem , Queries only add a small amount of logical reads .
If you can't modify the database index design , You can use stored procedures or SQL Use... In sentences RECOMPILE Query hints . Plans compiled as a side effect will not be cached . In addition to other query tips ,SQL Server Also provide OPTIMIZE FOR and OPTIMIZE FOR UNKNOWN Query prompt . When you use these query prompts , You should be very familiar with your data and data distribution , Because you are resetting the optimizer . Please use !Be always aware of this fact!
Reproduced in ——http://www.cnblogs.com/woodytu/p/4552325.html
边栏推荐
- Pytest learning - dayone
- Please ask me a question. I started a synchronization task with SQL client. From Mysql to ADB, the historical data has been synchronized normally
- 一大波开源小抄来袭
- 反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
- Use 3 in data modeling σ Eliminate outliers for data cleaning
- 请教个问题,我用sql-client起了个同步任务,从MySQL同步到ADB,历史数据有正常同步过去
- 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
- 内存==c语言1
- Can't connect to MySQL server on '(10060) solution summary
- 大佬们,有没有遇到过flink cdc读MySQLbinlog丢数据的情况,每次任务重启就有概率丢数
猜你喜欢
随机推荐
ORM--数据库增删改查操作逻辑
The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
Switching value signal anti shake FB of PLC signal processing series
Huffman encoded compressed file
基础篇:带你从头到尾玩转注解
Can't connect to MySQL server on '(10060) solution summary
ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作
HCIP 第一天 笔记整理
【ORM框架】
Why does the starting service report an error when installing MySQL? (operating system Windows)
First issue of JS reverse tutorial
thinkphp数据库的增删改查
conda离线创建虚拟环境
Using keras in tensorflow to build convolutional neural network
Sword finger offer II 107 Distance in matrix
“十二星座女神降临”全新活动推出
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
Arthas simple instructions
flink. CDC sqlserver. You can write the DEM without connector in sqlserver again
Win10 installation vs2015