What is Parameter Sniffing?

What is Parameter Sniffing?

 Parameter Sniffing

SQL Server creates an optimal plan for a stored procedure by using the parameters that are passed the first time to the stored procedure is executed is called Parameter Sniffing.

SQL Server does this exercise to build the execution plan once and use it multiple times without recreating the execution plan again and again. The compilation process of the stored procedure can take some precious time for the execution of the procedure is very large. This entire exercise is done to save those repeated moments recompiling the stored procedures.

While this works out great in most of the cases, there is often a negative impact of the parameter sniffing issue for the stored procedure.

Let us see a very simple example. To begin with, let us create the following stored procedure.


2
3
4
5
6
CREATE OR ALTER PROC GetCustomerOrders (@CustomerID INT)
AS
SELECT *
FROM WideWorldImporters.Sales.Orders
WHERE CustomerID = @CustomerID
GO

Now let us enable the actual execution plan for your query so we can, later on, inspect the execution plans.

First, run the stored procedure with the first value.

1
EXEC GetCustomerOrders 1060

Let us observe the execution plan. You will notice in the execution plan we see operators reading 4 rows out of potential 4 rows.

SQL SERVER - Parameter Sniffing Simplest Example parametersniffing1

Next, run the stored procedure with a different value.

1
EXEC GetCustomerOrders 90

Let us observe the execution plan. You will notice in the execution plan that the operator is reading 150 rows out of potential 4 rows.

SQL SERVER - Parameter Sniffing Simplest Example parametersniffing2



Now let us look at the property of the execution plan by right-clicking on the left-most SELECT operator in the SSMS.

Pay attention to the parameter list. You will see here there is mention of the parameter @CustomerID. This will also demonstrate that the parameter is compiled with the value 1060 but currently it is running this with a value of 90.

SQL SERVER - Parameter Sniffing Simplest Example parametersniffing3

This process of building the execution plan for the stored procedure with the initial run parameter is called Parameter Sniffing.

Performance Issue

In the real world, there is no performance issue of the execution plan of the new parameter is the same as the original execution plan. However, if the newer parameter is better suited for the different execution plan there can be huge potential performance degradation.

Let us understand this further by clearing the execution plan for our stored procedure by recompiling it.

1
EXEC sp_recompile 'GetCustomerOrders'

 

Now let us run the stored procedure again with the newer value of the 90 and you will see that the execution plan changes from the original execution plan. Earlier the execution plan displayed the count of the rows as 150 out of 4 which is not corrected as 150 of 150.

1
EXEC GetCustomerOrders 90

SQL SERVER - Parameter Sniffing Simplest Example parametersniffing4

If you now run the stored procedure with the earlier parameter where we got 4 rows, you will notice that that query now uses this newer plan and display 4 out of 150. This may be a potential performance issue as the query may get more resources allocated.

1
EXEC GetCustomerOrders 1060

SQL SERVER - Parameter Sniffing Simplest Example parametersniffing5

The simplest solution to overcome the parameter sniffing is to recompilation, however, it has also many different issues.


What do you understand by database tuning advisor?

 Database Engine Tuning Advisor examines how queries are processed in the databases you specify, and then recommends how you can improve query processing performance by modifying database structures such as indexes, indexed views, and partitioning.

What is SQL Profiler?

 1). What is SQL Profiler?

Ans: Microsoft SQL Server Profiler is a rich graphical user interface to SQL Trace for monitoring T-SQL Statements of Database Engine. The events are saved in a trace file that can later be analysed or used to replay a specific series of steps when trying to diagnose a problem.

2). What is the use of Profiler?
Ans: The functions SQL Server Profiler tool can perform have been listed below:
         Creating trace
         Watching trace
         Storing trace
         Replaying trace

What is mean by Statistics and When to Update statistics?

 Statistics determine the selectivity of the indexes. If an indexed column has unique values then the selectivity of that index is more, as opposed to an index with non-unique values. The query optimizer uses these indexes in determining whether to choose an index or not while executing a query.

Some situations under which you should update statistics:

  1. If there is a significant change in the key values in the index
  2. If a large amount of data in an indexed column has been added, changed, or removed (that is if the distribution of key values has changed), or the table has been truncated using the TRUNCATE TABLE statement and then repopulated
  3. The database is upgraded from a previous version

Look up SQL Server books online for the following commands:

UPDATE STATISTICS,
STATS_DATE,
DBCC SHOW_STATISTICS,
CREATE STATISTICS,
DROP STATISTICS,
sp_autostats,
sp_createstats,
sp_updatestats

What is Index Rebuild and Index Reorganize and What is the difference between index Rebuild and Index Reorganize operations?

 Index fragmentation can be resolved by rebuilding and reorganizing SQL Server indexes regularly. The Index Rebuild operation removes fragmentation by dropping the index and creating it again, defragmenting all index levels, compacting the index pages using the Fill Factor values specified in rebuild command, or using the existing value if not specified and updating the index statistics using FULLSCAN of all the data. 

The Index Reorganize operation physically reorders leaf level pages of the index to match the logical order of the leaf nodes. The index reorganizes operation will be always performed online. Microsoft recommends fixing index fragmentation issues by rebuilding the index if the fragmentation percentage of the index exceeds 30%, where it recommends fixing the index fragmentation issue by reorganizing the index if the index fragmentation percentage exceeds 5% and less than 30%. 


How can we get the fragmentation percentage of a database index?

 

  • Using SSMS, from the Fragmentation tab of the index Properties window. Checking the fragmentation percentage of all indexes in a specific database, using the UI method requires a big effort, as you need to check one index at a time.
  • The sys.dm_db_index_physical_stats dynamic management function, that was first Introduced in SQL Server 2005. The sys.dm_db_index_physical_stats DMF can be joined with the sys.indexes DMV to return the fragmentation percentage of all indexes under the specified database.

What is Fragmentation in SQL Server

Explain Index Depth, Density and Selectivity factors and how these factors affect index performance?

 

  • Index depth is the number of levels from the index root node to the leaf nodes. An index that is quite deep will suffer from performance degradation problem. In contrast, an index with a large number of nodes in each level can produce a very flat index structure. An index with only 3 to 4 levels is very common.
  • Index density is a measure of the lack of uniqueness of the data in a table. A dense column is one that has a high number of duplicates.
  • Index selectivity is a measure of how many rows scanned compared to the total number of rows. An index with high selectivity means a small number of rows scanned when related to the total number of rows.

How can you find the missing indexes that are needed to potentially improve the performance of your queries?

 

  • The Missing Index Details option in the query execution plan, if available.
  • The sys.dm_db_missing_index_details dynamic management view, that returns detailed information about missing indexes, excluding spatial indexes,
  • A combination of the SQL Server Profiler and the Database Engine Tuning Advisor tools.

What is the “Forwarding Pointers issue” and how can we fix it?