When a query that used to run fast suddenly starts running slowly in the middle of the night and nothing else is affected except for this one query, how do I troubleshoot it...?
You can start by checking if the execution plan is still in the cache. Check sys.dm_exec_query_stats
, sys.dm_exec_procedure_stats
and sys.dm_exec_cached_plans
. If the bad execution plan is still cached you can analyze it, and you can also check the execution stats. The execution stats will contain information as logical reads, CPU time and execution time. These can give strong indications what the problem is (eg. large scan vs. blocking). See Identifying problem queries for an explanation how to interpret the data.
Also, this is not a problem with parameter sniffing. I've seen that before, and this is not it, since even when I hard-code the varaibles in SSMS, I still get slow performance.
I'm not convinced. Hard-coding variables in SSMS does not prove that the past bad execution plan was not compiled against a skewed input. Please read Parameter Sniffing, Embedding, and the RECOMPILE Options for a very good article on the topic. Slow in the Application, Fast in SSMS? Understanding Performance Mysteries
is another excellent reference.
I've concluded (perhaps incorrectly) from these little experiments that the reason for the slow-down is due to how SQL's cached execution plan is set up -- when the query is a little different, it has to create a new execution plan.
This can be easily tested. SET STATISTICS TIME ON
will show you the compile vs. execution time. SQL Server:Statistics performance counters will also reveal whether compilation is an issue (frankly, I find it unlikely).
However, there is something similar that you may hit: the query grant gate. Read Understanding SQL server memory grant for details. If your query requests a large grant at a moment no memory is available, it will have to wait, and it will all look as 'slow execution' to the application. Analyzing wait info stats will reveal if this is the case.
For a more general discussion about what to measure and what to look for, see How to analyse SQL Server performance
Question 1: Though I will do in non peek hours of business but are there any chances that making these changes can corrupt my database or log file?
No this cannot damage your Log file.
Question 2: Does database compression mode can effect IO operation? If yes, how can I resolve it?
Yes database compression affects I/O and as far as my experience goes it decreases the I/O and is actually benefitial if you look at I/O consumption. I will tell you how.
- Compressed data will take fewer pages when stored on disk hence when data is read fewer I/O is required to fetch data in memory.
- Again since fewer pages are bought into memory this increases buffer pool availibility.
- CPU is what consumed when compression or decompression happens. But in most cases you would find that benefits gained by space and I/O would outweight the CPU consumption
- Please remember a compressed page will remain compressed on disk as well as compressed when brought in memory for reading or writing. So no additional work of decompressing in memory
This is excellent article on data compression, its big, but would help you in understanding data compression
You must remove all SQL Server related folder and files from Antivirus check specially if you have McAfee Antivirus.
•I am planning to change target recovery time to < 1 minute of data base (specifically tempdb and my database)
I can only say please leave it to default, I am not sure how recovery time has anything to do with log file and its configuration
Regarding Tempdb make sure you have Data files equal to number of physical cores. Paul has more to say about how many files you can have as per cores IMHO you can start with 4 tempdb files but keep monitoring for contention. Make sure that all tempdb data files have SAME INITIAL SIZE and SAME AUTOGROWTH setting. You can also enable TF 1118 to avoid contention
To check contention you can run below query
select
session_id,
wait_duration_ms,
resource_description
from sys.dm_os_waiting_tasks
where wait_type like 'PAGE%LATCH_%' and
resource_description like '2:%'
There are some good article for you to read about Tempdb and Contention
Troubleshooting Tempdb Contention by Paul Randal
Recommendation to reduce Allocation contention in Tempdb database
Monitoring Allocation Bottleneck In Tempdb
Edit:
SQL Server has encountered 1 occurrence(s) of I/O requests taking longer than 15 seconds to complete on file
This message means that a particular session or query requested for I/O to get data from disk but that session had to wait more than 15 sec and after that request was catered. You can guess that 15 sec is threshold value moment this time is crossed message is dumped in errorlog. This MOSTLY means that disk is not able to cope up with I/O request which is getting generated and in turn means disk might be slow. Since you said 90 % of your errorlog is filled with this information I am forced to believe that underlying hardware is slow or might require a firmware upgrade. This Article will help you in understanding the issue and fixing it
Best Answer
There are three DMVs you can use to track tempdb usage:
The first two will allow you to track allocations at a query & session level. The third tracks allocations across version store, user and internal objects.
The following example query will give you allocations per session:
If you want to track usage over a period of time, consider collecting data with sp_whoisactive, as demonstrated by Kendra Little.