I want to get transaction execute time in SQL Server 2008 R2
i want to get this time programmatically and save it in a table
. How can do it?
Save the time before and after the query and to calculate
the difference, something like this:
DECLARE #start_time DATETIME, #end_time DATETIME
SET #start_time = CURRENT_TIMESTAMP
-- query goes here
SET #end_time = CURRENT_TIMESTAMP
SELECT DATEDIFF(ms, #start_time, #end_time)
Try executing "SET STATISTICS TIME ON;" on your connection before executing your transaction.
JohnC is right and that is probably what you want.
But depending on your exact needs, there are other options. For one, SSMS tells you the amount of time, at least to the second, passed in the lower right cornor when you execute a query.
And of course you use getdate() to get the time from SQL Server immediately before and immediately after the execution and find the difference.
For repeated testing of large numbers of queries, you could also build a test harness in some other language like python with a library like timeit.
Related
I have some procedures which are not optimized . I want to analyse that whether these procedures get optimised or not , as there are only not bulk data to check.
I want to put a sql query into all the procedure which will insert in to one table that what is exact time taken by the above query to execute. I will create a table in which my procedure will insert the query execution time.
Is there any way that I can do this.
If you want to integrate it in your procedure, set a variable which stores the execution start time at the beginning, and a subtract it with the current time at the end of the procedure.
/* Start of your procedure */
Declare #startTime datetime = GETDATE()
Declare #duration varchar
/* Your procedure */
...
/* End of your procedure */
Set #duration = CONVERT(VARCHAR(8),GETDATE() - #startTime,108)
Insert into statisticTable
Values ('procedureName', #duration)
Did you try to use the SQL Profiler?
In Trace Properties you can can define Start Time and End time of Stored Procedures. It is a very powerful tool.
You can check above mentioned stuffs using Dynamic Management Views....
If you are using old version of SQL SERVER, use SQL Server Profiler....
Why would you want to do that?There are plenty of tools like DMV's ,trace ,exevents etc. You will add to overhead to your proc if you put that kind of code. For procedure best is dm_procedure_stats DMV but is availble in 2008 R2 and above only.Use the dm_exec_query_stats for older versions. These will give you much better stats about the time,logical io,cputime, and physical IO's etc.
I've been searching for an answer to this and I can't find it. I want to set up an SSIS package using Visual Studio 2005 and SQL Server 2005 that will send today's date (GETDATE())as the parameter to a stored procedure. I can find how to send a parameter, but not how to declare that parameter to be GETDATE(). Is this even possible?
If you need a constantly evaluating time, like GETDATE() then, create a Variable in SSIS called GetDate with a Data Type of DateTime. Right click and on the properties window, check the EvaluateAsExpression = True and for the Expression, use GETDATE()
Now wire that variable up to the Execute SQL Task.
If you don't need this very moment, look at using one of the system scoped variables. The ContainerStartTime of the Execute SQL Task would probably suffice. My go to value is the StartTime as that's when the package started execution but you'll know best which one is right for you.
One possible workaround to consider. You could make GETDATE() the default value for the parameter in the stored procedure and then call it without that parameter.
CREATE PROCEDURE YourProc
#InputDate DATETIME = GETDATE()
AS
...
I'm running a set of sql queries and they are not reporting the row affect until all the queries have ran. Is there anyway i can get incremental feedback.
Example:
DECLARE #HowManyLastTime int
SET #HowManyLastTime = 1
WHILE #HowManyLastTime <> 2400000
BEGIN
SET #HowManyLastTime = #HowManyLastTime +1
print(#HowManyLastTime)
END
This doesn't show the count till the loop has finished. How do i make it show the count as it runs?
To flush recordcounts and other data to the client, you'll want to use RaisError with NOWAIT. Related questions and links:
PRINT statement in T-SQL
http://weblogs.sqlteam.com/mladenp/archive/2007/10/01/SQL-Server-Notify-client-of-progress-in-a-long-running.aspx
In SSMS this will work as expected. With other clients, you might not get a response from the client until the query execution is complete.
SQL tends to be 'set-based', and you are thinking procedurally and trying to make it act systematically. It really doesn't make sense to do this in SQL.
I would be asking you motivation for doing this, and is there anything better that can be tried.
I have a stored procedure that takes a lot of time to execute. We want it to execute during the night, but it has to check for the current time every now and then, and at a given time it has to stop executing. How do I do that? Please provide me with the code I can use in my stored procedure. We are using Microsoft SQL Server 2005.
You can get the current date:
SELECT GETDATE()
Stop executing:
If #date > GETDATE()
RETURN --Exits procedure
Where #date is the date/time when you want to stop executing
Create Maintenance plan and sprecify start time for it. Create "Execute T-SQL statement task" in this plan and specify execution timeout for it (in seconds).
Why woudl you want to exist a proc that is not finished? Wouldn't you be leaving things in a bad state as far as data integrity or rolling back all the work you just did??
Wouldn't it be a better solution to try to improve the performance of the proc?
I have a stored procedure that accepts a date input that is later set to the current date if no value is passed in:
CREATE PROCEDURE MyProc
#MyDate DATETIME = NULL
AS
IF #MyDate IS NULL SET #MyDate = CURRENT_TIMESTAMP
-- Do Something using #MyDate
I'm having problems whereby if #MyDate is passed in as NULL when the stored procedure is first compiled, the performance is always terrible for all input values (NULL or otherwise), wheras if a date / the current date is passed in when the stored procedure is compiled performance is fine for all input values (NULL or otherwise).
What is also confusing is that the poor execution plan that is generated in is terrible even when the value of #MyDate used is actually NULL (and not set to CURRENT_TIMESTAMP by the IF statement)
I've discovered that disabling parameter sniffing (by spoofing the parameter) fixes my issue:
CREATE PROCEDURE MyProc
#MyDate DATETIME = NULL
AS
DECLARE #MyDate_Copy DATETIME
SET #MyDate_Copy = #MyDate
IF #MyDate_Copy IS NULL SET #MyDate_Copy = CURRENT_TIMESTAMP
-- Do Something using #MyDate_Copy
I know this is something to do with parameter sniffing, but all of the examples I've seen of "parameter sniffing gone bad" have involved the stored procedure being compiled with a non-representative parameter passed in, however here I'm seeing that the execution plan is terrible for all conceivable values that SQL server might think the parameter might take at the point where the statement is executed - NULL, CURRENT_TIMESTAMP or otherwise.
Has anyone got any insight into why this is happening?
Basically yes - parameter sniffing (in some patch levels of) SQL Server 2005 is badly broken. I have seen plans that effectively never complete (within hours on a small data set) even for small (few thousand rows) sets of data which complete in seconds once the parameters are masked. And this is in cases where the parameter has always been the same number. I would add that at the same time I was dealing with this, I found a lot of problems with LEFT JOIN/NULLs not completing and I replaced them with NOT IN or NOT EXISTS and this resolved the plan to something which would complete. Again, a (very poor) execution plan issue. At the time I was dealing with this, the DBAs would not give me SHOWPLAN access, and since I started masking every SP parameter, I've not had any further execution plan issues where I would have to dig in to this for non-completion.
In SQL Server 2008 you can use OPTIMIZE FOR UNKNOWN.
One way I was able to get around this problem in (SQL Server 2005) instead of just masking the parameters by redeclaring local parameters was to add query optimizer hints.
Here is a good blog post that talks more about it:
Parameter Sniffing in SqlServer 2005
I used: OPTION (optimize for (#p = '-1'))
Declare the procedure parameter inside the procedure and pass the external parameter to the internal .. compile ..