Professional Documents
Culture Documents
Query Tuning
Query Tuning
Speaker: Dean Richards
Senior DBA, Confio Software
Silicon Valley SQL Server User Group
November 2010
Mark Ginnebaugh, User Group Leader,
mark@designmind.com
Query Tuning
Get it Right the First Time
Dean Richards
Senior DBA, Confio Software
2
Who Am I?
Introduction
Which Query Should I Tune
Query Plans
SQL Diagramming
• Who registered
g yesterday
y y for Tuning
g Class
• Check order status
4
Why Focus on Queries
Most Applications
• 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
Whyy Tune Q
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?
p
• Developing applications is very difficult
• Typically focused on functionality
• Not much time left to tune SQL
• Do not get enough practice
• SQL runs differently
diff tl in
i Production
P d ti than th Dev/Test
D /T t
DBA?
• Do
D nott kknow th
the code
d lik
like developers
d l do
d
• Focus on “Keep the Lights On”
• Very complex environment
6
Need a team approach
Which SQL
User / Batch Job Complaints
Queries Performing Most I/O (LIO
(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
statement_end_offset, plan_handle,
handle, execution
execution_count,
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
http://msdn.microsoft.com/en-us/library/ms188754.aspx
dm_exec_requests dm_exec_query_stats
start_time execution_count
status total_logical_writes
sql_handle total_physical_reads
plan handle
plan_handle total logical reads
total_logical_reads
start/stop offset total_elapsed_time
database_id
user_id
blocking_session dm_exec_query_plan
wait_type
yp query plan
wait_time
dm_exec_text_query_plan
query plan
dm_exec_sessions
login_time
login_name
host_name
dm_exec_sql_text
program name
program_name
t t
text
session_id
9
Base Monitoring Query
10
RTA - Proactive
11
RTA - Firefighting
12
RTA - Correlation
13
Sample Wait Types
WRITELOG
• Waiting for a log flush to complete
LCK_M_S, LCK_M_U, LCK_M_X…
• Waiting to acquire locks
NETWORKIO, ASYNC_NETWORK_IO
• Waiting on the network
PAGEIOLATCH_SH, PAGEIOLATCH_EX…
• Physical disk reads
WAITFOR (idle event)
• Waiting
W i i during
d i a WAITFOR command
d
14
Tracing
15
Summary of Response Time
16
Why is SQL Slow - Plans
17
DM_EXEC_QUERY_PLAN
18
Case Studies
SQL
Q Diagramming
g g
• Who registered yesterday for Tuning Class
• Check order status
19
SQL Statement 1
20
Database Diagram
21
Execution Plan
Recommendation
R d ti from
f SSMS
CREATE NONCLUSTERED INDEX [<Name of Missing Index>]
ON [dbo].[registration] ([cancelled],[signup_date])
INCLUDE ([student_id],[class_id])
([student id] [class id])
22
SQL Diagramming
Great Book “SQL Tuning” by Dan Tow
• Great book that teaches SQL Diagramming
• http://www.singingsql.com
registration
g .03
37 1293
1 1
2 / 1,267 = .001
23
New Execution Plan
CREATE INDEX cl_name ON class(name)
24
Database Diagram
25
New Execution Plan
26
Better Execution Plan
29
Database Diagram
30
SQL Diagramming
wage_pmt .02
90
1000
loc
select count(1) from wage_pmt
where pay_date >= DATEADD(day,-31,CURRENT_TIMESTAMP)
32
New Execution Plan
CREATE INDEX ix2_fname ON emp(first_name)
33
Which Index?
SSMS Recommendation
CREATE INDEX wp
wp_pay_date
pay date ON wage
wage_pmt(pay_date)
pmt(pay date)
INCLUDE (emp_id)
50 000 Logical
50,000 L i l Reads
R d
or…
Better Option
CREATE INDEX wp
wp_emp_pd
emp pd ON wage
wage_pmt(emp_id,
pmt(emp id, pay_date)
pay date)
46 Logical Reads
34
New Execution Plan
CREATE INDEX wp_emp_pd ON wage_pmt(emp_id, pay_date)
35
SQL Statement 2
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
It i ON i.OrderID
i O d ID = o.OrderID
O d ID
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 ISNULL(@LastName,'') + '%'
--AND c.FirstName LIKE ISNULL(@FirstName,'') + '%'
AND o.OrderDate >= DATEADD(day, -30, CURRENT_TIMESTAMP)
AND sd.ShipStatus <> 'C'
37
Execution Plan
38
SQL Diagramming
o .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
SELECT COUNT(1)*1.0/(SELECT COUNT(1) FROM [Order]) FROM [Order]
WHERE OrderStatus <> 'C'
.005
-- Combined
.005
39
Data Skew Problems
43
To learn more or inquire about speaking opportunities, please contact:
Mark Ginnebaugh, User Group Leader
mark@designmind.com
http://www.meetup.com/The‐SiliconValley‐SQL‐Server‐User‐Group/