SQL stored procedure different execution time - sql

Well, I have a stored procedure on SQL server 2012. When I execute it with the same parameters from SSMS it always takes different time to get results.I have observed that I need from 10sec to 10mins to wait. What could be a reason? Where is to start digging? I can not post the code here because it's too large, but I think some common recommendations might appear.

Well, the time difference between runs is rather large, so it could be that the system may be under load when you run this queries. Is this in a production environment?
To troubleshoot:
Turn on Actual Query Plan and execute the query/
Check that other queries are not blocking your query (sp_who2)
You can also run SQL Profiler when you run your query.

Related

Executing a SQL query multiple times

How to run the same query multiple times in SQL Server?
Simple example, I have a query
select * from sys.databases
I wanted to run it N times, because I wanted to return the data in a dashboard in "real time", until I stopped the execution, the select would need to continue running "example: as SQL Server Profiler does, while I don't stop, it keeps bringing the information in the screen".
What would be the best way for this type of situation?
Remembering that the query and SQL Server profiler are just examples.
create a job and insert your query to run, and under the schedule set the timings to execute your query.
Because your dashboard code must poll the dbms every so often to get the latest data, you must decide how often as part of your system design.
Once a minute? That is very often to poll the data. But only you can decide how out-of-date your dashboard users will tolerate.
Whatever you do, avoid promising "real time" if you possibly can. We programmers can't deliver on that promise with polling the database.

Entity Framework - good SQL, yet slow

My Entity Framework is set up to emit the SQL generated, followed by the time taken to run the query, to the Output pane.
When I run it locally, the EF query takes .064s (as can be seen in the Output pane), and the SQL (when run by itself in Management Studio) takes about the same. In production the EF query takes .660s, yet the SQL generated only takes .157s.
There are about 50 rows returned. All the other EF queries are running at the expected speed.
What can cause the EF to take so much longer to run than the SQL it generates?
Thank you for any ideas.
The simplest way to figure out why queries run differently when called from different locations is to check the execution plans for both of them. From SSMS it's simply a case of including the plan in the output. For a live SQL Server you can use SQL Profiler.
When you have both plans, compare them and figure out the differences.
One example when a query runs differently is the ARITHABORT setting. Your app probably connects to SQL Server with this turned off whereas in SSMS it is on. This may cause it to use a different execution plan.

Auditing execution of stored procedures in Sql Server

My boss and I have been trying to see what sort of auditing plan we could try for our stored procedures. Currently there're two external applications taking information from our database through stored procedures and we're interested in auditing when they're being executed, and what values are passed as parameters. So far what I've done is simply create a table for the stored procedures one of the apps is using, and as they use the same input parameters, have one column per parameter. Obviously this isn't the best choice, but we wanted to get quick info to see if they were running batch processes and when they were running them. I've tried SQL Server Audit, but it doesn't catch the parameters unless you're executing a SP in a query.
SQL Server Profiler will do this for you; its included for free. Setup a trace and let it run.
You can also apply quite a bit of filtering to the trace, so you don't need to track everything; you can also direct the output to a file, or sql table for later analysis. This is probably your best bet for a time limited audit.
I think I've used the SQL Server Profiler (http://msdn.microsoft.com/en-us/library/ms181091.aspx) in the past to audit SQL execution. It's not something you would run all the time, but you can get a snapshot of what's running and how it's being executed.
I haven't tried using them, but you might look at event notifications and see if they will work for you.
From BOL
Event notifications can be used to do the following:
Log and review changes or activity occurring on the database.

Sql Server Queries taking time

I am facing some issues with my stored procedures.
I am having 1 stored procedure for a Stack Bar graph, showing one months data.
Earlier on my local server it was taking more than 40 seconds, so I made some changes and now it takes 4 seconds. The same query when I run on my live server takes more than 40 seconds.
The count of the records are the same as in my local server.
Can anybody tell me what I should do to make it more fast on my live server?
A good start is to run SQL Server Management Studio (SSMS), load up the query, and switch on 'Display Actual Execution Plan', this will show you exactly what SQL is doing with your query. It will also show you a relative '%cost' in relation to the steps in the query. This helps to identify which table/join/aggregate whatever is causing the query to take so long.
I also believe that in the latest version of SSMS it advises which indexes should be added.
Hope this helps.
Rich.
It's complicated question . It's a lot of parameters
Can change time of execution
CPU speed,
Ram,
Indexes
Assuming server will be more powerful in terms of processing power and RAM, indexes is something you would like to look into.
use indexes with your mysql tables and also it may be because of your hosting server's performance. The server may be faced with down time.

SQL Server Express 2008 Stored Procedure execution time spikes periodically

I have a big stored procedure on a SQL Server 2008 Express SP2 database that gets run about every 200 ms. Normal execution time is about 50ms. What I am seeing is large inconsistencies in this run time. It will execute for while, say 50-100 times at 40-60ms which is expected, then seemingly at random the same stored procedure will take way longer, say 900ms or 1.5 seconds to run. Sometimes more than one call of the same procedure in a row will take longer too.
It appears that something is causing sql server to slow down dramatically every minute or so, but I can't figure out what. There is no timing pattern between the occurences.
I have the same setup on two different computers, one of which is a clean XP Pro load with no virus checking and nothing installed except SQL server.
Also, The recovery options for all the databases are set to "Simple".
I would suggest breaking out applicable sections into their own stored procedures; there is only one query plan cached per batch.
It looks like my problems happen simultaneously with the SQL Server Plan Cache Object Counts hitting 999 and resetting.