SQL debugging


Published on

This presentation is for anyone who works with SQL and wants to have a better understanding over SQL Debugging, Profiling and DTA.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • This presentation is for anyone who works with SQL and wants to have a better understanding over SQL Debugging, Profiling and DTA.
  • DECLARE @var INT = 0WHILE( @var < 1000 ) BEGIN IF( @var%100 = 0 ) PRINT @var SET @var += 10 END
  • As a good practice you need to run Profiler on a computer other than the SQL Server you are monitoring. This way, SQL Server doesn't have to compete for memory with Profiler.Profiler templates: Standard SP_Counts is used if you are interesting in identifying the top 10 most run sp in your application TSQL_Duration template is used to capture slow queries. TSQL – can be used by developers to debug client applications. TSQL_Replay - is often used for stress testing.TSQL_Grouped is used to investigate problem queries from a particular client or user. TSQL_SPs is used to analyze the statements within spTuning trace can be used as the row data to feed into the Database Engine Tuning Advisor for index optimization. You can create a template from scratch, you can modify an existing template or you can save your trace as a template. Notice that specific templates are associated with specific server types. The last option: you can schedule a trace’s stop time. Don’t use this option unless you are comfortable that your system won’t run out of space. Otherwise it is best to stick around during the trace watching the size of the trace file to ensure it doesn’t get too big. You can schedule a Profiler trace using the SQL Agent Job also and in the References section you can find more details on how to do this step by step. it’s important to select only those data columns you really need in order to minimize the amount of resources consumed by the trace. The problem is not in the mechanics of selecting events but in deciding which of the 170 Profiler events and 64 data columns that really want to select. You can run your trace on a production server otherwise on a test server and a stress test tool. Running Profiler on a production environment is not a recommended practice, as it could degrade the performance of the server or the connection can be lost and you miss important events.Running Server Side TraceUsing Profiler you are doing a ClientSideTracing, but you can also do ServerSideTracing which means running your trace as a script from SQL Management Studio. You can go to File and Export as a script. For more information about this please check the video posted in the references section.
  • It’s important not only to identify long running queries but also to identify those queries that execute the most often. I think you would agree that the shorter query is more important on the overall performance of the system than the longer one. Generally we would expect the same T-SQL code to run in about the same amount of time each time it runs. If there are variations in Duration and also CPU time then is just a problem with server load.
  • In order to start this tool you need to have prepared a trace that feed DTA to make optimized recommendations. You can choose to use the default tuning template or create your own template using the events and data columns from slide. In fact, if you include any other events in the trace, the DTA will just ignore them.Don’t forget to check Server Processes Trace Data checkbox to ensure that all these 3 trace events are captured. You need to pick a time of the day that is representative for your server load and run the trace for as long as possible -> say 8 hours, but if you can’t run the trace for this long due to disk space limitations or negative impact on server resources then consider taking multiple, smaller traces. On busy production servers the trace file can grow big very quickly. You will want to keep a close eye on the amount of disk space used by the trace so that you don’t accidently run out of room. If you consider your trace is not the best one taken then you need to run the analysis several times and compare the results to see if you can either accept them all or be selective. Once you implement the recommendations you need to go back and see if the indexes are really helping. One think I like to do is to compare Profiler traces taken before and after the DTA recommendations were implemented.
  • Here are the references where you can find how to schedule a profiler trace, how to make a ServerSideTrace, and the download link for the Mastering SQL Server Profiler book where you can find all the events and data columns explained.
  • SQL debugging

    1. 1. SQL DEBUGGING OPRIS MONICA – DB & BI Developer
    2. 2. Outline • SQL Server Debugging • SQL Server Profiler • Database EngineTuning Advisor 7/25/2013 2SQL Debugging
    3. 3. Demo: SQL Server 2012TSQL Debugger Enhancements: 1) Conditional Hit breakpoints 2) Hit Count 3) Breakpoint Filter 4) Breakpoint Action 5) Breakpoint Location 6) Export/Import the breakpoints to an XML file. 7/25/2013 3SQL Debugging
    4. 4. Keyboard shortcuts Action Standard Start or continue debugging ALT+F5 Stop debugging SHIFT+F5 Step into F11 Step over F10 Step out SHIFT+F11 Implement the RunTo Cursor command CTRL+F10 Display the QuickWatch dialog box CTRL+ALT+Q Toggle breakpoint F9 Delete all breakpoints CTRL+SHIFT+F9 Display the Breakpoints window CTRL+ALT+B Break all CTRL+ALT+BREAK Display the Watch 1 window CTRL+ALT+W, 1 Display the Watch 2 window CTRL+ALT+W, 2 Display the Watch 3 window CTRL+ALT+W, 3 Display the Watch 4 window CTRL+ALT+W, 4 Display the Autos window CTRL+ALT+V, A Display the Locals window CTRL+ALT+V, L Display the Immediate window CTRL+ALT+I Display the Call Stack window CTRL+ALT+C Display the Threads window CTRL+ALT+H 7/25/2013 4SQL Debugging
    5. 5. Debugging using PRINT Declare @isdebug = 1 IF @isdebug = 1 PRINT @yourvar IF @debug = 1 BEGIN PRINT 'Something' --or insert into a log table if you need the rows of a temp table --or the results of a calculation END 7/25/2013 5SQL Debugging
    6. 6. SQL Server Profiler • Monitor the performance of an instance of SQL Server • DebugTransact-SQL statements and stored procedures • Identify slow-executing queries • Troubleshoot problems in SQL Server by capturing events on a production system and replaying on a test system • Audit and review activity that occurred on an instance of SQL Server 7/25/2013 6SQL Debugging
    7. 7. SQL Server Profiler terminology • Events • Data columns • Filters • Trace 7/25/2013 7SQL Debugging
    8. 8. 7/25/2013 8 RPC:Completed, SP:StmtCompleted SQL:BatchStarting SQL:BatchCompleted Showplan XML Duration ObjectName TextData CPU Reads Writes IntegerData DatabaseName ApplicationName StartTime EndTime SPID LoginName EventSequence BinaryData SQL Server Profiler SQL Debugging
    9. 9. • Which is worse, a query that runs once and takes 30 seconds to run or a query that runs 100 times and takes 1 second to run? • Does a query run slowly every time? 7/25/2013 9 SQL Server Profiler SQL Debugging
    10. 10. DTA using Profiler trace 7/25/2013 10 1) RPC:Completed 2) SP:StmtCompleted 3) SQL:BatchCompleted 1)TextData 2) Duration 3) SPID 4)DatabaseName 5)LoginName: SQL Debugging
    11. 11. Conclusions • SQL Debugger has limitations • Profiler is not just for DBAs • Use Profiler to learn how applications work • Use Profiler to monitor live activity • Collect only relevant data • Be selective when applying DTA recommendations 7/25/2013 11SQL Debugging
    12. 12. References • http://technet.microsoft.com/en-us/library/cc293612.aspx • http://www.sqlservercentral.com/articles/SQL+Trace/99217/ • http://download.red- gate.com/ebooks/SQL/Mastering_Profiler_eBook.pdf • http://technet.microsoft.com/en-us/library/ms187929.aspx • http://technet.microsoft.com/en-us/library/ms175047.aspx • http://msdn.microsoft.com/en-us/library/ms173494(v=sql.105).aspx • http://midnightdba.itbookworm.com/Video/Watch?VideoId=88 7/25/2013 12SQL Debugging
    13. 13. 7/25/2013 13SQL Debugging Thank you