IIS takes longer to run queries than SMSS - sql-server-2005

I'm completely stumped on this one.
We're running a pretty usual intranet app here, classic ASP frontend and SQL Server 2005 backend.
Occasionally, once/twice a week, a certain stored proc takes ~50x longer to run when I run it in the Management Studio. This slowness lasts the entire day and it inexcplicably goes away the next day.
So essentially what's happening is this: once/twice a week, when I fire up the intranet page that runs the stored procedure and displays the results, it takes roughly 115secs to run.
I then fire up SQL Server Management Studio and run the same query, which this time around takes only 3-4secs to run.
The next day, the intranet page is taking as long as SSMS to run the very same stored procedure, with nothing having been changed in the interim (not to my knowledge anyway).
Other details:
the IIS connects to SQL Server 2005 using Provider=sqloledb as the driver
the slowdown lasts the entire day, regardless of the number of active users
I can replicate the slowness by running the same query from Excel, using the same driver/connection string
I've ran a trace on the SQL server and it turns out that on those particularly slow days, the duration of the stored proc is actually longer when executed via IIS than direct query
As such, I was wondering whether any of you has encountered any similar behaviour before, if you've fond a solution to it and what would your advice be in tackling/solving it?
Thanks for your help,
Please find Trace results in .csv format below:
EventClass,TextData,ApplicationName,CPU,Reads,Writes,Duration (s),ClientProcessID,SPID,StartTime,EndTime
12,exec spStubbornProc,Internet Information Services,"106,016","7,059,999",1,115.80,5476,85,18/04/2013 08:17:15,18/04/2013 08:19:11
12,exec dbo.spStubbornProc,Microsoft SQL Server Management Studio - Query,"3,141","146,051",0,3.40,5876,84,18/04/2013 08:20:45,18/04/2013 08:20:48
12,exec dbo.spStubbornProc,Microsoft SQL Server Management Studio - Query,"2,563","147,387",0,2.93,5876,84,18/04/2013 08:21:04,18/04/2013 08:21:07
12,exec spStubbornProc,Internet Information Services,"103,156","7,083,365",7,118.73,5476,80,18/04/2013 09:39:41,18/04/2013 09:41:40
12,exec dbo.spStubbornProc,Microsoft SQL Server Management Studio - Query,"2,406","175,029",1,2.57,5876,84,18/04/2013 10:08:58,18/04/2013 10:09:01
12,exec spStubbornProc,Internet Information Services,"112,218","7,103,267",7,114.72,5476,75,18/04/2013 14:06:26,18/04/2013 14:08:21
12,exec spStubbornProc,Internet Information Services,"93,515","7,101,229",3,93.60,6092,60,18/04/2013 19:37:02,18/04/2013 19:38:35
12,exec spStubbornProc,Internet Information Services,"2,500","148,775",1,3.42,5160,75,19/04/2013 09:16:01,19/04/2013 09:16:04

Related

Simple SQL Server query always time out

We have a stored procedure that has been working perfectly for over 10 years. A few days ago it started timing out. I run it from the Query console in SQL Management Studio and it timesout. I restart the server and then it runs in 1 second...
You should consider the changes happened to your system over these 10 years, maybe you have added some background services that is doing some heavy sql updates/insertions while you are running your query.
I think you can have a clear vision on the performance of your query if you run it on a staging server or a local machine( which normally have a different load of sql operations ), observe the time it takes to execute your query, do your tries from time to time.
If the execution time of your query on staging server is constant, then your query is not the cause of the issue.

SQL Performance Reports - but for a date range

SQL Server comes with very handy "performance" reports: in SSMS - right-click the server - "reporting" - "Standard" - you see many useful reports, like "Top queries by Total CPU time", "Top queries by average I/O" etc.
These reports aggregate the data since last restart or since last query stats reset.
Is there any way to see these reports for a date range? Like "Top queries by total CPU time from 10pm to 11pm"? Maybe there's an SQL Server addon on or service that should be installed or something, that would allow this kind or research?
Real life use case: a DBA wakes up and sees there were some "timeout" errors during the night. He wants to see which queries were running at the moment and eating up performance.
You can download the SSMS Performance Dashboard Reports from here and use as Custom reports with SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014. Having access to the rdl file you can customize it however you want.
Also worth reading this.
Found this series of blog posts at SQLShack that advises installing "Data Collectors" that continuously record performance data, so you can analyse it later: SQL Server performance monitoring with Data Collector

Stored Procedures in SQL Database in Azure Timeout randomly once every two months

Scenario: System in a VM in Azure using MVC and a SQL Database (not in the VM) working under normal conditions for 2 or 3 months. Suddenly, stored procedures called from my MVC web app or SQL Management Studio return Time Out. Queries like Select * from Table work perfect.
EDIT: Timeouts while executing Alter or Create SP queries happened too.
No proper solutions or explanations found.
Workaround: Restore old backup in a new SQL Database and change the connection string to the new Database. While the system is running in the backup, try to backup the database with issues (first close all connections to that DB like Management Studio). It may take some time and some retries. After the backup is done, restore it in a new DB and change back the connectionString. You will lose a few minutes of data and some downtime but you will have your system working again in Azure.
Any ideas about this issue in the Stored Procedures in Azure?
At first glance, this smells like a parameter sniffing issue; it is probably not related to Azure.
Check this thread for details on what the issue is, and how to resolve it: Parameter Sniffing (or Spoofing) in SQL Server

Find which line of the SQL is running in SQL Server Management Studio?

I am executing a very long SQL script in SQL Server Management Studio, normally it takes a few second to be done.
But this time it is taking forever and never completes. Other people running this script has no problem.
So I wonder if there is anyway I could debug in SQL Server Management Studio to see which line in this long SQL Script is currently executing and taking forever?
Easiest way is to fire up SQL Server Profiler and trace your SQL with that. As a bonus, you'll get a lot more useful information than simply which line is causing the problem.
If Profiler doesn't give you what you want, have a go with the Activity Monitor. Find your session in the Processes part, and then pull up the details to see the last T-SQ command batch.

Connection Timeout Linked Server

I have created a linked server from one SQL Server 2005 to Another 2005. When I run an update query through the SQL Server Management Studio (SSMS), it runs in under a second. If I run the query through a asp webpage it times out. I ran SQL Profiler to see if I noticed anything as well as the Activity Monitor in SSMS and all I found was that a lock was being created (Wait type LOCK_M_U), but I can't find what is locking it. Any help would be appreciated.
Wade
It could be that the user account that you run under in SSMS and from the ASP page have different permissions on the linked server, which could lead to a difference in which query plans it uses when running your update. See this answer to a more generic question about linked server performance.
The issue seems to be with the ADO activeX component, since I used ASP.Net and it worked flawlessly. Oh well.