I have to debug a long running SQL Server stored procedure which dynamically builds and executes queries at runtime. I want to run a trace so I can see all of the individual pieces of SQL that are executed.
Which events will provide that level of detail?
I've been advised to use Stored Proc: RPC Completed; Batch Completed and Batch Started. But that's only bringing back the initial execution statement. ie. literally "exec MyStoredProcName".
Any ideas?
Thanks In Advance.
Related
I have a stored procedure that takes 1 parameter. When I run the stored procedure from SQL Server Management Studio, it runs in 2-4 seconds. When I call it with a console application, it takes 30+ seconds. The SQL Server is remote and both SSMS and my application are being run from my local machine so I don't think it's a networking issue.
I've ran the SQL Server Profiler to try to track down the issue and one thing I'm seeing is that when it's run from SSMS it starts the statement, recompiles it, then starts it over again, then completes it, like this:
SP:StmtStarting
SP:Recompile
SQL:StmtRecompile
SP:StmtStarting
SP:StmtCompleted
The 2 recompile entries have an EventSubClass of "2 - Statistics changed"
From the app I only see entries for SP:StmtStarting & SP:StmtCompleted, no recompile entries.
I'm calling exactly the same stored procedure with the same parameter value. Why does SSMS recompile based on statistics but my console app does not?
After researching and troubleshooting it appears to be entirely due to SET_ARITHABORT_ON. SSMS defaults this to 'ON' while the .net sql client defaults it to 'OFF' so it was going with 2 different execution plans, although I'm not entirely sure why the two plans are so drastically different.
I overrode the OpenConnection() method to open the connection set it to ON and my application then had the same performance as SSMS. I hope this helps anyone else who stumbles upon this.
In profiler it is easy to know when the Stored Procedure Execution Completed using RPC Completed.
Can we know when the Stored Procedure Execution began using Profiler ?
In SQL SERVER Profiler we have a template called T-SQL sps.
We can choose RPC Started Option.
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.
With respect to performance tuning, I would like to find out the number of DB calls each page is making. Also, the stored procedures and queries that is executed. I'm using Asp.net and SQL Server 2008. In some places we have directly written the query in C# insead of calling SP.
I tried using SQL Profiler. In that, under Event if I select SP, I'm able to trace the SP calls. But what about the queries that are direcly called in C#. How can I trace that.
Kindly let me know how this can be done or if there is a better tool to find this out.
Thanks in advance.
Use SQL Profiler to create a trace that logs these events:
RPC:Completed
SP:Completed
SP:StmtCompleted
SQL:BatchCompleted
SQL:StmtCompleted
See How Can I Log and Find the Most Expensive Queries?
When you open a new trace you can select the template TSQL Which will then include the event SQL:BatchStarting that will show you the queries as well
I want to do something like:
exec sproc1 and sproc2 at the same time
when they are both finished exec sproc3
I can do this in dts.
Is there a way to do it in transact sql?
Or is there a way to do it with a batch script (eg vbs or powershell)?
You could create a CLR Stored Procedure that (using C#) would call the first two on their own threads, and then block until both are complete... then run the third one.
Are you able to use CLR sprocs in your situation? If so, I'll edit this answer to have more detail.
sp _ start _ job
I'm doing a similar thing at the moment, and the only way I've found to avoid using SSIS or some external shell is to split my load routine into 'threads' manually, and then fire a single master sqlagent job which in turn executes as many sp _ start _ job's as I have threads. From that point, they all run autonomously.
It's not exactly what we're looking for, but the result is the same. If you test the job status for the sub jobs, you can implement your conditional start of sproc 3 as well.
What's the point in 8 cores if we can't use them all at once?
Do you absolutely need both SPs to be executed in parallel?
With simple CRUD statements within a single SP, I've found SQL S. does a very good job of determining which of them can be run in parallel and do so. I've never seen SQL S. run 2 SPs in parallel if both are called sequentially from a T-SQL statement, don't even know if it's even possible.
Now then, do the DTS really execute them in parallel? It could be it simply executes them sequentially, then calls the 3rd SP after the last finishes successfully.
If it really runs them in parallel, probably you should stick with DTS, but then I'd like to know what it does if I have a DTS package call, say, 10 heavy duty SPs in parallel... I may have to do some testings to learn that myself :D
You can use SSIS. The benefits of this are that the package can be stored in the SQL Server and easily scheduled there.
From PowerShell or just about any external scripting language, you can use the SQL command line osql or sqlcmd. This technique can also be used to schedule it on the SQL Server by shelling out using xp_cmdshell also.