Presentation is loading. Please wait.

Presentation is loading. Please wait.

Query Tuning Get it Right the First Time Dean Richards Senior DBA, Confio Software 1.

Similar presentations


Presentation on theme: "Query Tuning Get it Right the First Time Dean Richards Senior DBA, Confio Software 1."— Presentation transcript:

1 Query Tuning Get it Right the First Time Dean Richards Senior DBA, Confio Software
1

2 Who Am I? 20+ Years in SQL Server & Oracle
DBA and Developer Senior DBA for Confio Software Makers of Ignite8 Response Time Analysis Tools – only free RTA Tool Specialize in Performance Tuning Presented at 24 Hours of PASS 13 SQL Saturdays and counting…

3 Agenda Introduction Which Query Should I Tune? Query Plans
SQL Diagramming Who registered yesterday for Tuning Class Lookup paycheck information Check order status

4 Why Focus on Queries Most Applications Most Queries Why Tune Queries?
Read and Write data to/from database Simple manipulation and smaller amounts of data Inefficiencies would not be noticed Most Queries Examine larger amounts of data, return a little Inefficiencies quickly become bottleneck Why Tune Queries? “Gives the most bang for your buck” Changes to SQL are usually safer ~85% of performance issues are SQL related

5 Who Should Tune Developers? DBA? Need a team approach
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

6 Which SQL User / Batch Job Complaints
Queries Performing Most I/O (LIO, PIO) Queries Consuming CPU Queries Doing Table or Index Scans Known Poorly Performing SQL Server Side Tracing Highest Response Times (Ignite8) SELECT sql_handle, statement_start_offset, statement_end_offset, plan_handle, execution_count, total_logical_reads, total_physical_reads, total_elapsed_time, st.text FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st ORDER BY total_elapsed_time DESC

7 Why is SQL Slow – Wait States
Focus on Response Time Understand the total time a Query spends in Database Measure time while Query executes SQL Server helps by providing Wait Types 7

8 Wait Time Tables (SQL 2005/8)
dm_exec_requests start_time status sql_handle plan_handle start/stop offset database_id user_id blocking_session wait_type wait_time dm_exec_query_stats execution_count total_logical_writes total_physical_reads total_logical_reads total_elapsed_time dm_exec_query_plan query_plan dm_exec_text_query_plan query_plan dm_exec_sessions login_time login_name host_name program_name session_id dm_exec_sql_text text

9 Base Monitoring Query INSERT INTO SessionWaitInfo SELECT 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 cdt FROM sys.dm_exec_requests r INNER JOIN sys.dm_exec_sessions s ON s.session_id = r.session_id WHERE r.status <> 'background' AND r.command <> 'AWAITING COMMAND' AND s.session_id > 50 AND s.session_id <>

10 Why is SQL Slow - Plans SQL Server Management Studio
Estimated Execution Plan - can be wrong Actual Execution Plan – must execute query, can be dangerous 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, Real execution plan of executed query

11 DM_EXEC_QUERY_PLAN

12 Case Studies SQL Diagramming Who registered yesterday for Tuning Class
Lookup paycheck information Check order status

13 SQL Statement 1 Who registered yesterday for SQL Tuning
SELECT s.fname, s.lname, r.signup_date FROM student s INNER JOIN registration r ON s.student_id = r.student_id INNER JOIN class c ON r.class_id = c.class_id WHERE c.name = 'SQL TUNING' AND r.signup_date AND r.cancelled = 'N' Execution Stats – 9,634 Logical Reads

14 Database Diagram

15 Execution Plan Recommendation from SSMS
CREATE NONCLUSTERED INDEX [<Name of Missing Index>] ON [dbo].[registration] ([cancelled],[signup_date]) INCLUDE ([student_id],[class_id])

16 SQL Diagramming Great Book “SQL Tuning” by Dan Tow registration
Great book that teaches SQL Diagramming registration .03 37 1293 1 1 student class .001 select count(1) from registration where cancelled = 'N' and signup_date between ' :00' and ' :00' 54,554 / 1,639,186 = 0.03 select count(1) from class where name = 'SQL TUNING' 2 / 1,267 = .001

17 New Execution Plan Execution Stats – 9,139 Logical Reads
CREATE INDEX cl_name ON class(name) Execution Stats – 9,139 Logical Reads Why would an Index Scan still occur on REGISTRATION?

18 Database Diagram

19 New Execution Plan Execution Stats – 621 Logical Reads
CREATE INDEX reg_alt ON registration(class_id) Execution Stats – 621 Logical Reads

20 Better Execution Plan Execution Stats – 20 Logical Reads
CREATE INDEX reg_alt ON registration(class_id) INCLUDE (signup_date, cancelled) Execution Stats – 20 Logical Reads

21 Alternative from SSMS Execution Stats – 595 Logical Reads
CREATE INDEX reg_can ON registration(cancelled, signup_date) INCLUDE (class_id, student_id) Execution Stats – 595 Logical Reads CREATE NONCLUSTERED INDEX [<Name of Missing Index>] ON [dbo].[registration] ([class_id],[cancelled],[signup_date]) INCLUDE ([student_id])

22 SQL Statement 2 Paychecks for specific employees
SELECT e.first_name, e.last_name, l.description FROM emp e INNER JOIN loc l ON e.loc_id = l.loc_id WHERE (e.first_name OR e.last_name AND EXISTS ( SELECT 1 FROM wage_pmt w WHERE w.emp_id = e.emp_id AND w.pay_date>= DATEADD(day,-31,CURRENT_TIMESTAMP) ) Execution Stats – 64,206 Logical Reads

23 Database Diagram

24 SQL Diagramming wage_pmt emp loc select count(1) from wage_pmt
.02 90 1 emp .0005 .0009 1000 1 loc select count(1) from wage_pmt where pay_date >= DATEADD(day,-31,CURRENT_TIMESTAMP) 40,760 / 1,915,088 = .02 select top 5 first_name, count(1) from emp group by first_name order by 2 desc 12 / 23,798 = – first_name 22 / 23,789 = – last_name

25 Execution Plan

26 New Execution Plan CREATE INDEX ix2_fname ON emp(first_name)

27 Which Index? SSMS Recommendation
CREATE INDEX wp_pay_date ON wage_pmt(pay_date) INCLUDE (emp_id) 50,000 Logical Reads or… Better Option CREATE INDEX wp_emp_pd ON wage_pmt(emp_id, pay_date) 46 Logical Reads

28 New Execution Plan CREATE INDEX wp_emp_pd ON wage_pmt(emp_id, pay_date)

29 SQL Statement 3 Lookup order status for caller
SELECT o.OrderID, c.LastName, p.ProductID, p.Description, sd.ActualShipDate, sd.ShipStatus, sd.ExpectedShipDate FROM [Order] o INNER JOIN Item i ON i.OrderID = o.OrderID INNER JOIN Customer c ON c.CustomerID = o.CustomerID INNER JOIN ShipmentDetails sd ON sd.ShipmentID = i.ShipmentID LEFT OUTER JOIN Product p ON p.ProductID = i.ProductID LEFT OUTER JOIN Address a ON a.AddressID = sd.AddressID WHERE c.LastName LIKE + '%' --AND c.FirstName LIKE + '%' AND o.OrderDate >= DATEADD(day, -30, CURRENT_TIMESTAMP) AND sd.ShipStatus <> 'C' Execution Stats – 10,159 Logical Reads

30 Database Diagram

31 Execution Plan

32 SQL Diagramming o i c sd p a
.08 .005 i c .03 sd p a SELECT COUNT(1)*1.0/(SELECT COUNT(1) FROM Customer) FROM Customer WHERE LastName LIKE 'SMI%' .03 SELECT COUNT(1)*1.0/(SELECT COUNT(1) FROM [Order]) FROM [Order] WHERE OrderDate >= DATEADD(day, -30, CURRENT_TIMESTAMP) .08 WHERE OrderStatus <> 'C' .005 -- Combined

33 Data Skew Problems Only 0.5% of rows are <> ‘C’
SELECT OrderStatus, COUNT(1) FROM [Order] GROUP BY OrderStatus Only 0.5% of rows are <> ‘C’ How about changing the query? AND o.OrderStatus = 'I' Add an Index on ShipStatus

34 New Execution Plan Execution Stats – 3,052 Logical Reads
CREATE INDEX IX2_OrderStatus ON [Order] (OrderStatus) INCLUDE (OrderID,CustomerID) Execution Stats – 3,052 Logical Reads

35 Takeaway 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

36 Confio Software Wait-Based Performance Tools Igniter Suite
Ignite for SQL Server, Oracle, DB2, Sybase Helps show which SQL to tune Based in Colorado, worldwide customers Free trial at


Download ppt "Query Tuning Get it Right the First Time Dean Richards Senior DBA, Confio Software 1."

Similar presentations


Ads by Google