You Can Tune Your Own SQL Code
-
Upload
mike-walsh -
Category
Technology
-
view
1.252 -
download
2
description
Transcript of You Can Tune Your Own SQL Code
YOU CAN TUNE YOUR OWN CODESQL Saturday #33 Charlotte
WHO IS MIKE?
10 years with SQL DBA roles of various sorts Blog: www.straightpathsql.com/ Syndicated at SQLServerPedia Full Time – Principal DBA at global insurance
company. “Spare” Time – Consult for SQL
Administration and Performance Issues President of new “Seacoast SQL” PASS
Chapter
Who Are You?
“Why is this so slow?!”
“It worked fine in dev!”
“The results came back fine… I’m done”
What Aren’t We Covering?
“Can’t you just make it
faster?!”
http://www.flickr.com/photos/irinaslutsky/4000761728/
www.flickr.com/photos/no3rdw/170950846/
www.flickr.com/photos/no3rdw/170950846/http://www.flickr.com/photos/dullhunk/359634390/
AGENDA
Tips For Slow Queries The Right Mindset & Some Best Practices Indexes & Statistics (Building Blocks) Query Plans (Not So Scary) SQL Profiler (Don’t tell my DBA friends..) Put it all together and CLAP DBAs don’t mean to be so mean*
*Maybe we do…, but let’s work it out
Goal 1
You will leave using the concepts we
discuss.
Goal 2
You won’t ever say, “done” until you know your code
PERFORMS
Goal 3
“Can’t we all just get
along??!”
Tips For Making Queries Slow
Right Mindset
RIGHT MINDSET
• SQL Is Declarative• Work With Sets• Watch Transaction Footprint• Join Where Necessary• Remember: Disk – Memory –
Query• Smaller Sets ASAP• Work with the columns you need
“Should I split this up?”
“Should I use -that- view?”
“How Many Reads Am I
doing?”
“What will happen in 1
year?”
“How many rows are
affected by that operation?”
HOW ARE YOU PROVING YOUR CODE?
• Working with representative volume?
• Testing various scenarios?• Before/After Testing?• Using Empirical Evidence?• Look and Feel with a Stop
Watch???
Indexes
What Are Indexes?
•Pointers To Data•B-Tree Structures•Sometimes – Hero Makers•Sometimes – Frustrations
Clustered Indexes
•Leaf Level Is The Data•Narrower Is Better•Ever-Increasing•Order By•Not Changing
Non-Clustered Indexes
•Leaf Level Has Index Data and Pointer Only•Joins•Common (and Selective) Search Criteria.
ScanSeek
“Bookmark/RID/KEY Lookups”Covering
What Does It All Mean?
Statistics
SARGable Queries
Search ARGuments
•Query That Can Use An Index
•(All Things Being Equal…)
•Generally: =, <, >, <=, =>,
BETWEEN, IN, LIKE (with exceptions).
•Some Rules but think of the
phonebook…
Fun(?) With Phonebooks
Some Tools
•IO Statistics
•Query Plans
•Profiler
Put It Together
Care
Look
Act
Prove It Out
http://www.flickr.com/photos/hikingartist/3515471358/
Goal 3
“Can’t we all just get
along??!”
Goal 1
You will leave using the concepts we
discuss.
Goal 2
You won’t ever say, “done” until you know your code
PERFORMS
You CAN Tune Your Own SQL Code
RESOURCES
mike@StraightPathSQL.comwww.SQLServerCentral.comwww.SQLServerPedia.comsqlblog.comblogs.msdn.com/craigfr/default.aspxhttp://sqlinthewild.co.za/ http://scarydba.wordpress.com/ http://www.straightpathsql.com/
blogroll/
StraightPathSQL.com/ucandoit
Confused?Angry?Lost?