Stored procedure takes time on database server using SQL Server - sql-server

When I run the stored procedure on the local machine's SQL Server, it takes 2 seconds, but on the database server, it takes 10 seconds.
For both, local and database server, the execution plan is the same.
How do I find out the reason for taking too much time to execute the stored procedure?
Thanks.
ExecutionPlan

Related

Schedule SQL Job X minutes after SQL Server Agent starts

I have a Job in SQL server which is scheduled to 'Start automatically when SQL Server Agent starts', however, this is creating some issues in our environment. The issue is not SQL specific but because of it we need to delay the execution of this particular job. I cannot schedule this as a recurring job and assign a particular time of a day, it needs to run every time when the SQL Server starts. Is there a way in which I can schedule the job that runs after 15 mins after SQL Server Agent starts?
Sure, just make the first step:
WAITFOR DELAY '00:15:00';
(Or, probably better, you could try to resolve whatever "some issues" are.)
However, note that someone can restart the Agent service without restarting SQL Server; or, they could set SQL Server Agent to not automatically start at startup, so that the next time the SQL Server service is restarted, your procedure will not run.
If you want to tie some startup activity to SQL Server starting, you could probably look into startup procedures, but if you need it to wait 15 minutes, the first thing in the procedure would be the above WAITFOR. Also startup procedures can't have input parameters (or output parameters, but that is less likely to be an issue for a stored procedure you're calling from a job).
Finally, SQL Server 2008 R2? That was barely still in support at the beginning of the last decade.

SQL Server stored procedure long running intermittently

I have 5 stored procedures, called by an api, that run in under 100 ms normally, but intermittently, all the stored procedures suddenly take > 10 seconds in production. We did stress test before going live and it was all good during stress test.
For each of the stored procedures, I capture start time using SYSDATETIME() and log it an table at the end of the stored procedure with end time as well. So I run profiler on production and notice strange things.
For example, today, profiler RPC:completed event start time is 09:18:04.680 where as my stored procedure execution log tables says the execution started at 09:19:54.288. So there is a 110second mismatch between profiler execution start vs my stored procedure internal start time. So this happens to all stored procedures for a window of 2-3mins and everything clears by itself.
I've ran perfmon and nothing shows out of ordinary to me. The SQL Server has high capacity with the application only running few users currently so not a very high traffic application.
I also have Redgate SQL Monitor and it doesn't show any abnormal wait times.
I'm not even sure where to look for. I'm not sure its parameter sniffing because one of the stored procedure affected doesn't accept any parameters. After the 2-3 minutes all the stored procedures run as expected.

SQL Server Query Plan creation in SSMS vs Application Server

I have the following scenario:
After a database deployment we have a .Net application server that is attempting to execute a stored procedure.
The timeout on the application is 30 seconds. When the application first attempts to execute the stored proc an attempt is made to create a new query plan but this takes longer than 30 seconds and the application has many timeouts. My experience with this is that if the stored procedure is run manually(with representative data inputs) from SSMS the first time it runs it takes about 1-2 minutes, a plan gets generated and then the application then runs smoothly.
I work with a third party company and there is s DBA there who is claiming the following:
"Manually invoking this stored procedure will create a plan that is specific to the connection properties used (SSMS), the plan generated would not be used when the procedure is invoked by an application server."
Is this correct? It seems like a poor design if query plan used was linked to connection properties? Is there a difference between a query plan created if you run the stored procedure manually in SSMS vs when it is executed by an application?
If so, What is the optimal way to resolve this issue? Is increase the timeout the only option?

How to fix SQL Server stored procedure timeout - partially complete when called from VBS

I have VBS called stored procedures starting but not completing and returning query timeout messages to the application.
We are on SQL Server 2008 SP2 release 1, Windows Server 2008 R2 Standard.
This is not an issue in production, but in dev we have VBS scripts associated with the DrillThrough database producing:
VBS<105,4> Microsoft OLE DB Provider for SQL Server: Query timeout expired.
These scripts are automated on the application server side. The stored procedure queries run fine in SSMS but do take over 40 seconds.
ALL the scripts are all set not to timeout:
conn.ConnectionTimeout = 0
conn.CommandTimeout = 0
The OLEDB login is the dbo. The only scripts failing are on the DrillThrough database. The failing scripts use more than one database and functions in the DrillThrough database to gather data in temp tables to update the actual drill through table.
There is one nightly process that is completing on the DrillThrough database where the stored procedure runs in about 2 seconds. It does not use any functions but does compare data from two records pulled from two other databases before writing the difference to a table.
I have compared the scripts and they are set up the same except for the stored procedures they call.
I updated statistics on the tables just in case. The query still timed out on the application side, and actually took a little longer to run in SSMS.
The SQL Server remote connections is set to 0 for no timeout.
It definitely gets into the procedure because it truncates the tables, bulk loads the holding table and deletes from the actual drill through table based on the holding table data and creates the first temp table but it is not completing and is dropping out somehow.
Again, the stored procedure runs fine in SSMS all the way through.
Everyone refers back to the SQL Server setting and the ConnectionTimeout and CommandTimeout. No one seems to be experiencing a situation where the stored procedure starts but doesn’t complete.
Other things I’ve tried with the same results:
Adding ‘With Recompile’ to the stored procedure
Setting an actual timeout time in the VB Script
I have tuned the query as much as I can
I can't get a faster development server.

SQL Server Stored procedure performance varies one server to another server

I have a stored procedure created in two different servers, one is installed in my local machine and another one is installed another machine.
When i am trying to execute in my local server it will take 1 sec to execute but when i am trying to connect other instance and execute the same stored procedure it will take 15 seconds to execute.
Kindly let me know the reason and how to improve the performance

Resources