Post on 22-May-2015
description
SQL DEBUGGINGOPRIS MONICA – DB & BI Developer
04/12/2023
SQL Debugging 2
Outline
• SQL Server Debugging
• SQL Server Profiler
• Database Engine Tuning Advisor
04/12/2023
SQL Debugging 3
Demo: SQL Server 2012 TSQL 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.
04/12/2023
SQL Debugging 4
Keyboard shortcuts
Action StandardStart or continue debugging ALT+F5Stop debugging SHIFT+F5Step into F11Step over F10Step out SHIFT+F11Implement the Run To Cursor command
CTRL+F10
Display the QuickWatch dialog box CTRL+ALT+QToggle breakpoint F9Delete all breakpoints CTRL+SHIFT+F9Display the Breakpoints window CTRL+ALT+BBreak all CTRL+ALT+BREAKDisplay the Watch 1 window CTRL+ALT+W, 1Display the Watch 2 window CTRL+ALT+W, 2Display the Watch 3 window CTRL+ALT+W, 3Display the Watch 4 window CTRL+ALT+W, 4Display the Autos window CTRL+ALT+V, ADisplay the Locals window CTRL+ALT+V, LDisplay the Immediate window CTRL+ALT+IDisplay the Call Stack window CTRL+ALT+CDisplay the Threads window CTRL+ALT+H
04/12/2023
SQL Debugging 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
04/12/2023
SQL Debugging 6
SQL Server Profiler
• Monitor the performance of an instance of SQL Server
• Debug Transact-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
04/12/2023
SQL Debugging 7
SQL Server Profiler terminology
• Events
• Data columns
• Filters
• Trace
04/12/2023
SQL Debugging 8
RPC:Completed,SP:StmtCompleted SQL:BatchStartingSQL:BatchCompletedShowplan XML
Duration ObjectNameTextData CPU Reads Writes IntegerData DatabaseName ApplicationName StartTime EndTime SPID LoginName EventSequence BinaryData
SQL Server Profiler
04/12/2023
SQL Debugging 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?
SQL Server Profiler
04/12/2023
SQL Debugging 10
DTA using Profiler trace
1) RPC:Completed
2) SP:StmtCompleted
3) SQL:BatchCompleted
1) TextData2) Duration3) SPID4)DatabaseName5)LoginName:
04/12/2023
SQL Debugging 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
04/12/2023
SQL Debugging 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
04/12/2023
SQL Debugging 13
Thank you