What is Parameter Sniffing in simple words?

I have been asked recently what is parameter sniffing in an interview. Well, we all know how Optimizer in SQL server works to retrieving the data from the tables. We do also know that Optimizer reads the statistics and internal pre-compiled plans instead generating a new plan for parameterized Queries and Store procedures. Here we are, how Optimizer knows we are executing a similar query as it searching for pre-compiled plan for us because it was reading the parameters we passed to the stored procedure or a parameterized query and it is called “sniffing”.

What it counts for Performance, Good or bad?

 “It depends “Yes “It depends” – I know we use this word many times in regards to databases


By default, Parameter sniffing is enabled and SQL Server caches the plans and reads the parameters those passed to the stored procedures or parameterized SQL Queries. We benefit from it as Optimizer will go and search for the plan it compiled earlier in cache when we execute the same query again. Optimizer will not need to re-create the plan to execute the same query multiple times.


This is all good only when there are good statics are maintained and typical parameters are used. What changes day by day is the requirement for using the new parameters with different words and optimizer will tries to use the same old plan in cache and may not be good for the new parameters as data has grown a lot and statistics have not updated yet and may be the plan for new parameter was doing table scans.

Work Around:

  1. Until we see a performance issue with this parameter sniffing it is better to leave it by default and we really benefit from it.
  2. When we see lot of performance issue, we could disable it using a trace flag 4136, this will disable the parameter sniffing at the server level.
  3. We could use it to disable only for specific query then we could use the recompile  query hint

SQL Server 2012- Sample Databases

I was not able to find the sample databases for SQL server 2012 as most of the sites I have visited have only 2000  sample databases which we will be not able to restore to 2012. Here I have uploaded the link 2012 sample .bak files. (Easy to restore to 2012)

Pubs, Adventure Works, North Wind  –Download

How to bring a database online which is in restoring mode ?

For Example, In log shipping the secondary or a stand-by database will be in restoring mode while applying the logs from primary for every 15 minutes.

In order to perform a DR test, we need to bring the production down and DR database online, this can be achieved by running the below query on secondary server database.

–Run this below query to bring the database online which is in Restoring state after successfully restored all log backups using the restore jobs in secondary server

Restore Log [DATABASE NAME] with recovery

Configure SQL Server DBMAIL using T-SQL

Configuring dBMail involves 3 main steps

Step 1: Creating Mail Profile

Step2: Creating Mail Account

Step3: Mapping Account to Profile.

Below script will let you do this 3 steps and configures your dBmail successfully.


-    ENABLE SQL DBMAIL, if diabled

EXEC sys.sp_configure N'Database Mail XPs', N'1'






-    Add Mail Profile


EXEC msdb.dbo.sysmail_add_profile_sp @profile_name=N'Profile Name'



-    Set as Default Profile


EXEC msdb.dbo.sysmail_add_principalprofile_sp 

@profile_name=N'Profile Name',

@is_default =N'1'





EXEC msdb.dbo.sysmail_add_account_sp


   @account_name    = 'Account_Name',

   @email_address   = 'Email Address,EX:DBA@yourcompany.com',

   @display_name    = 'Account Name',

   @replyto_address = 'Email Address,EX:DBA@yourcompany.com',

   @mailserver_name = 'your SMTP Server',

   @mailserver_type = N'SMTP or if you use other mail protocol',

   @port                = 25,

   @use_default_credentials = 0,

   @enable_ssl = 0



-    Mapping Account with Profile


EXEC  msdb.dbo.sysmail_add_profileaccount_sp   @profile_name=N'Profile Name', @account_name= N'Account Name',  @sequence_number=N'1'



How to run a SQL Query on list of servers and save output to excel using power shell?

You will see  how much is powershell is useful for DBA  after using this script.

Step 1: Create a text file with list of servers you want to run the script on and name it ‘serverlist’ to C:\serverlist.txt

Step 2:  I have Created a folder called scripts in my C: then Copy the below script into another text file and save it as C:\Scripts\script.ps1

# Script Runs on servers listed in serverlist.txt
# use sqlps.exe to run this script
# In cmd, sqlps.exe c:\scripts\script.ps1

$query = " Your SQL Query"

#Path to the excel file to be saved.

$csvFilePath = "c:\Scripts\queryresults.csv"
$excelFilePath = "c:\scripts\queryresults.xls"

# Run Query against multiple servers, combine results
# Replace "Server1", "Server2\INSTANCE1" with names of your SQL Server instances

$instanceNameList = get-content c:\serverlist.txt
foreach($instanceName in $instanceNameList)
        write-host "Executing query against server: " $instanceName
        $results += Invoke-Sqlcmd -Query $query -ServerInstance $instanceName

# Output to CSV

write-host "Saving Query Results in CSV format..."
$results | export-csv  $csvFilePath   -NoTypeInformation
# Convert CSV file to Excel
# Reference : <a href="http://gallery.technet.microsoft.com/scriptcenter/da4c725e-3487-42ff-862f-c022cf09c8fa">http://gallery.technet.microsoft.com/scriptcenter/da4c725e-3487-42ff-862f-c022cf09c8fa</a>

write-host "Converting CSV output to Excel..."

$excel = New-Object -ComObject excel.application
$excel.visible = $False
$workbook = $excel.Workbooks.Open($csvFilePath)
$workSheet = $workbook.worksheets.Item(1)
$resize = $workSheet.UsedRange
$resize.EntireColumn.AutoFit() | Out-Null
$xlExcel8 = 56
$excel = $null

write-host "Results are saved in Excel file: " $excelFilePath

Step3: Copy and paste the SQL Query into the above script where it says like this 

$query = ” Your SQL Query to run against multiple servers”  

Step 4: Open CMD and run the script like this

Sqlps.exe c:\scripts\script.ps1

The results from the SQL Query from each server will be combined and saved as queryresults.xls into your scripts folder.

Note :The script uses windows authentication to connect to the SQL servers.

What “Open Table “in SQL Server 2005 replaced with in SQL Server 2008? How to change this Edit top 200 to edit all?


Today, a customer asked me how to open a table in SQL Server 2008 to edit the rows in a table manually as she is able to do it in SQL 2005 using open table option in right click of table but in SQL Server 2008 it was replaced with edit top 200 rows. Initially I though there is no way to change from the default option Edit top 200 to all but after searching in BOL ,I found this


SSMS -> Tools -> Options-> SQL Server Object Explorer-> Commands-> Set value 0 for Value for Edit Top Rows Command


Note: Setting value 0 lets you retrieve all or you can customize this value to your requirement.                 


Source: BOL