SQL Server Query Tuning Tips - Get it Right the First Time

2,958 views
2,728 views

Published on

Whether you are a developer or DBA, this presentation will outline a method for determining the best approach for tuning a query every time by utilizing response time analysis and SQL Diagramming techniques. Regardless of the complexity of the statement or database platform being utilized (this method works on all), this quick and systematic approach will lead you down the correct
tuning path with no guessing. If you are a beginner or expert, this approach will save you countless hours tuning a query.

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,958
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
0
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

SQL Server Query Tuning Tips - Get it Right the First Time

  1. 1. SQL Server Query Tuning TipsGet it Right the First TimeDean RichardsLead DBA, Confio Software2/12/2013 1
  2. 2. 2/12/2013 Confidential - Internal Use Only 2Who Am I?• 20+ Years in Oracle & SQL Server– DBA and Developer• Lead DBA for Confio Software– DeanRichards@confio.com– Makers of Ignite8 Response Time Analysis Tools– http://www.ignitefree.com – only free RTA Tool• Specialize in Performance Tuning
  3. 3. 2/12/2013 3• Introduction• Which Query Should I Tune?• Query Plans• SQL Diagramming– Who registered yesterday for Tuning Class– Check order statusAgenda
  4. 4. 2/12/2013 Confidential - Internal Use Only 4• Most Applications– Read and Write data to/from database– Simple manipulation of data– Deal with smaller amounts of data• Most Databases– Examine larger amounts of data, return a little– Inefficiencies quickly become bottleneck• Why do SQL tuning?– Tuning SQL - “Gives the most bang for your buck”– Changes to SQL are Safer– ~85% of performance issues are SQL relatedWhy Query Focused
  5. 5. 2/12/2013 Confidential - Internal Use Only 5Who Should Tune• Developers?– Developing applications is very difficult– Typically focused on functionality– Not much time left to tune SQL– Do not get enough practice– SQL runs differently in Production than Dev/Test• DBA?– Do not know the code like developers do– Focus on “Keep the Lights On”– Very complex environment• Need a team approach - DevOps– But also need someone to lead the effort
  6. 6. 2/12/2013 Confidential - Internal Use Only 6Which SQL• User / Batch Job Complaints• Queries Performing Most I/O (LIO, PIO)• Queries Consuming CPU• Known Poorly Performing SQL• Tracing a Session / Process• Highest Response Times (Ignite)SELECT QS.query_hash, QS.total_elapsed_time, qs.execution_count,SUBSTRING(ST.text, (QS.statement_start_offset/2) + 1,((CASE statement_end_offsetWHEN -1 THEN DATALENGTH(ST.text)ELSE QS.statement_end_offset END - QS.statement_start_offset)/2) + 1) AS sql_textFROM sys.dm_exec_query_stats AS QSCROSS APPLY sys.dm_exec_sql_text(QS.sql_handle) as STORDER BY total_elapsed_time DESC
  7. 7. 2/12/2013 Confidential - Internal Use Only 7SQL Wait States Understand the total time a Query spends in Database Measure time while Query executes SQL Server helps by providing Wait TypesFocus on Response Time
  8. 8. 2/12/2013 Confidential - Internal Use Only 8Wait Interfacedm_exec_sql_texttextdm_exec_sessionslogin_timelogin_namehost_nameprogram_namesession_iddm_exec_query_statsexecution_counttotal_logical_writestotal_physical_readstotal_logical_readstotal_elapsed_timedm_exec_requestsstart_timestatussql_handleplan_handlestart/stop offsetdatabase_iduser_idblocking_sessionwait_typewait_timedm_exec_query_planquery_planhttp://msdn.microsoft.com/en-us/library/ms188754.aspx
  9. 9. 2/12/2013 Confidential - Internal Use Only 9Base Monitoring QueryINSERT INTO SessionWaitInfoSELECT r.session_id, r.sql_handle, r.statement_start_offset,r.statement_end_offset, r.plan_handle, r.database_id,r.blocking_session_id, r.wait_type, r.query_hash,s.host_name, s.program_name, s.host_process_id,s.login_name, CURRENT_TIMESTAMP cdtFROM sys.dm_exec_requests rINNER JOIN sys.dm_exec_sessions s ON s.session_id = r.session_idWHERE r.status <> backgroundAND r.command <> AWAITING COMMAND‘AND s.session_id <> @@SPID
  10. 10. 2/12/2013 Confidential - Internal Use Only 10RTA - Proactive
  11. 11. 2/12/2013 11RTA - Firefighting
  12. 12. 2/12/2013 12RTA - Correlation
  13. 13. 2/12/2013 Confidential - Internal Use Only 13Sample Wait Types• WRITELOG– Waiting for a log flush to complete• LCK_M_S, LCK_M_U, LCK_M_X…– Waiting to acquire locks• ASYNC_NETWORK_IO– Waiting on data on the network• PAGEIOLATCH_SH, PAGEIOLATCH_EX…– Physical disk reads• WAITFOR (idle event)– Waiting during a WAITFOR command
  14. 14. 2/12/2013 Confidential - Internal Use Only 14Tracing• Tracing with waits gathers very good data• Can be High Overhead – especially via Profiler• Use Server-Side Tracing– sp_trace_create – create the trace definition– sp_trace_setevent – add events to trace– sp_trace_setfilter – apply filters to trace– sp_trace_setstatus – start/stop the trace• Use Profiler to Create Initial Trace– Use File > Script Trace to Get Script• Cumbersome to review data• Set trace file sizes appropriately
  15. 15. 2/12/2013 Confidential - Internal Use Only 15Summary of RTA• Using Response Time Analysis (RTA) Ensuresyou work on the Correct Problem• Shows Exactly Why Performance is Suffering• Helps Prioritize Problems• Do Not Rely Exclusively on Health Stats (CPUUtilization, Disk IO, Cache Hit Ratio)• Data Collection– DMVs – build it yourself– Tracing – know how to process trace data– Tools – Ensure they use Wait Time and Health
  16. 16. 2/12/2013 Confidential - Internal Use Only 16Why is SQL Slow - Plans• SQL Server Management Studio– Estimated Execution Plan - can be wrong– Actual Execution Plan – must execute query, can bedangerous in production and also wrong in test• SQL Server Profiler Tracing– Event to collect: Performance : Showplan All– Works when you know a problem will occur• DM_EXEC_QUERY_PLAN(@handle,@s,@e)– Real execution plan of executed query
  17. 17. 2/12/2013 Confidential - Internal Use Only 17DM_EXEC_QUERY_PLAN
  18. 18. 2/12/2013 Confidential - Internal Use Only 18Case Studies• SQL Diagramming– Who registered yesterday for Tuning Class– Check order status
  19. 19. 2/12/2013 Confidential - Internal Use Only 19SQL Statement 1• Who registered yesterday for SQL TuningSELECT s.fname, s.lname, r.signup_dateFROM student sINNER JOIN registration r ON s.student_id = r.student_idINNER JOIN class c ON r.class_id = c.class_idWHERE c.name = SQL TUNINGAND r.signup_date BETWEEN @BeginDate AND @EndDateAND r.cancelled = N• Execution Stats – 9,634 Logical Reads
  20. 20. 2/12/2013 Confidential - Internal Use Only 20Database Diagram
  21. 21. 2/12/2013 Confidential - Internal Use Only 21Execution PlanRecommendation from SSMSCREATE NONCLUSTERED INDEX [<Name of Missing Index>]ON [dbo].[registration] ([cancelled],[signup_date])INCLUDE ([student_id],[class_id])
  22. 22. 2/12/2013 Confidential - Internal Use Only 22SQL Diagrammingregistrationstudent class37112931.03.001select count(1) from registration where cancelled = Nand signup_date between 2010-04-23 00:00 and 2010-04-24 00:0054,554 / 1,639,186 = 0.03select count(1) from class where name = SQL TUNING2 / 1,267 = .001• Great Book “SQL Tuning” by Dan Tow– Great book that teaches SQL Diagramming– http://www.singingsql.com
  23. 23. 2/12/2013 Confidential - Internal Use Only 23New PlanCREATE INDEX cl_name ON class(name) Execution Stats – 9,139 Logical Reads Why would an Index Scan still occur on REGISTRATION?
  24. 24. 2/12/2013 Confidential - Internal Use Only 24Database Diagram
  25. 25. 2/12/2013 Confidential - Internal Use Only 25New PlanCREATE INDEX reg_alt ON registration(class_id) Execution Stats – 621 Logical Reads
  26. 26. 2/12/2013 Confidential - Internal Use Only 26Better PlanCREATE INDEX reg_alt ON registration(class_id)INCLUDE (signup_date, cancelled) Execution Stats – 20 Logical Reads
  27. 27. 2/12/2013 Confidential - Internal Use Only 27SSMS PlanCREATE INDEX reg_can ON registration(cancelled, signup_date)INCLUDE (class_id, student_id) Execution Stats – 595 Logical ReadsCREATE NONCLUSTERED INDEX [<Name of Missing Index>]ON [dbo].[registration] ([class_id],[cancelled],[signup_date])INCLUDE ([student_id])
  28. 28. 2/12/2013 Confidential - Internal Use Only 28SQL Statement 2• Lookup order status for callerSELECT o.OrderID, c.LastName, p.ProductID, p.Description,sd.ActualShipDate, sd.ShipStatus, sd.ExpectedShipDateFROM [Order] oINNER JOIN Item i ON i.OrderID = o.OrderIDINNER JOIN Customer c ON c.CustomerID = o.CustomerIDINNER JOIN ShipmentDetails sd ON sd.ShipmentID = i.ShipmentIDLEFT OUTER JOIN Product p ON p.ProductID = i.ProductIDLEFT OUTER JOIN Address a ON a.AddressID = sd.AddressIDWHERE c.LastName LIKE ISNULL(@LastName,) + %--AND c.FirstName LIKE ISNULL(@FirstName,) + %AND o.OrderDate >= DATEADD(day, -30, CURRENT_TIMESTAMP)AND sd.ShipStatus <> C• Execution Stats – 10,159 Logical Reads
  29. 29. 2/12/2013 Confidential - Internal Use Only 29Database Diagram
  30. 30. 2/12/2013 Confidential - Internal Use Only 30Plan
  31. 31. 2/12/2013 Confidential - Internal Use Only 31SQL Diagrammingo .08.03SELECT COUNT(1)*1.0/(SELECT COUNT(1) FROM Customer) FROM CustomerWHERE LastName LIKE SMI%.03SELECT COUNT(1)*1.0/(SELECT COUNT(1) FROM [Order]) FROM [Order]WHERE OrderDate >= DATEADD(day, -30, CURRENT_TIMESTAMP).08SELECT COUNT(1)*1.0/(SELECT COUNT(1) FROM [Order]) FROM [Order]WHERE OrderStatus <> C.005-- Combined.005i cpsda.005
  32. 32. 2/12/2013 Confidential - Internal Use Only 32Data Skew• Only 0.5% of rows are <> ‘C’• How about changing the query?– AND o.OrderStatus = I• Add an Index on ShipStatusSELECT OrderStatus, COUNT(1)FROM [Order]GROUP BY OrderStatus
  33. 33. 2/12/2013 Confidential - Internal Use Only 33New PlanCREATE INDEX IX2_OrderStatus ON [Order] (OrderStatus)INCLUDE (OrderID,CustomerID)Execution Stats – 3,052 Logical Reads
  34. 34. 2/12/2013 Confidential - Internal Use Only 34Takeaway Points• Tuning Queries gives more “bang for the buck”• Make sure you are tuning the correct query• Use Wait Types and Response Time Analysis– Locking problems may not be a Query Tuning issue– Wait types tell you where to start• Use “Real Execution Plans”– Get plan_handle from DM_EXEC_REQUESTS– Pass plan_handle to DM_EXEC_QUERY_PLAN()• SQL Diagramming - “Get it right the First Time”– Query Tuner Tools can mislead you
  35. 35. Q&AThank you for attending.More questions?Contact Dean atDeanRichards@confio.comDownload free trial of Ignite athttp//www.confio.com2/12/2013 35

×