You Can Tune Your Own SQL Code

40
YOU CAN TUNE YOUR OWN CODE SQL Saturday #33 Charlotte

description

Slide deck from a talk first delivered at SQL Saturday 34 in Waltham, MA. Mike Walsh talks about how you can tune your own SQL Server code. Walk through indexes, Query Plans, IO Statistics and Profiler. The goal was to show developers how painless it is to gauge performance and make changes as a result. Updated to v1.5 as presented in Charlotte to SQLSat33

Transcript of You Can Tune Your Own SQL Code

Page 1: You Can Tune Your Own SQL Code

YOU CAN TUNE YOUR OWN CODESQL Saturday #33 Charlotte

Page 2: You Can Tune Your Own SQL Code

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

Page 3: You Can Tune Your Own SQL Code

Who Are You?

Page 4: You Can Tune Your Own SQL Code

“Why is this so slow?!”

“It worked fine in dev!”

“The results came back fine… I’m done”

Page 5: You Can Tune Your Own SQL Code

What Aren’t We Covering?

Page 6: You Can Tune Your Own SQL Code

“Can’t you just make it

faster?!”

http://www.flickr.com/photos/irinaslutsky/4000761728/

Page 7: You Can Tune Your Own SQL Code

www.flickr.com/photos/no3rdw/170950846/

Page 8: You Can Tune Your Own SQL Code

www.flickr.com/photos/no3rdw/170950846/http://www.flickr.com/photos/dullhunk/359634390/

Page 9: You Can Tune Your Own SQL Code

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

Page 10: You Can Tune Your Own SQL Code

Goal 1

You will leave using the concepts we

discuss.

Page 11: You Can Tune Your Own SQL Code

Goal 2

You won’t ever say, “done” until you know your code

PERFORMS

Page 12: You Can Tune Your Own SQL Code

Goal 3

“Can’t we all just get

along??!”

Page 13: You Can Tune Your Own SQL Code

Tips For Making Queries Slow

Page 14: You Can Tune Your Own SQL Code
Page 15: You Can Tune Your Own SQL Code

Right Mindset

Page 16: You Can Tune Your Own SQL Code

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

Page 17: You Can Tune Your Own SQL Code

“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?”

Page 18: You Can Tune Your Own SQL Code

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???

Page 19: You Can Tune Your Own SQL Code

Indexes

Page 20: You Can Tune Your Own SQL Code

What Are Indexes?

•Pointers To Data•B-Tree Structures•Sometimes – Hero Makers•Sometimes – Frustrations

Page 21: You Can Tune Your Own SQL Code

Clustered Indexes

•Leaf Level Is The Data•Narrower Is Better•Ever-Increasing•Order By•Not Changing

Page 22: You Can Tune Your Own SQL Code

Non-Clustered Indexes

•Leaf Level Has Index Data and Pointer Only•Joins•Common (and Selective) Search Criteria.

Page 23: You Can Tune Your Own SQL Code

ScanSeek

“Bookmark/RID/KEY Lookups”Covering

What Does It All Mean?

Page 24: You Can Tune Your Own SQL Code

Statistics

Page 25: You Can Tune Your Own SQL Code

SARGable Queries

Page 26: You Can Tune Your Own SQL Code

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…

Page 27: You Can Tune Your Own SQL Code

Fun(?) With Phonebooks

Page 28: You Can Tune Your Own SQL Code

Some Tools

•IO Statistics

•Query Plans

•Profiler

Page 29: You Can Tune Your Own SQL Code

Put It Together

Care

Look

Act

Prove It Out

Page 30: You Can Tune Your Own SQL Code

http://www.flickr.com/photos/hikingartist/3515471358/

Page 31: You Can Tune Your Own SQL Code

Goal 3

“Can’t we all just get

along??!”

Page 32: You Can Tune Your Own SQL Code

Goal 1

You will leave using the concepts we

discuss.

Page 33: You Can Tune Your Own SQL Code

Goal 2

You won’t ever say, “done” until you know your code

PERFORMS

Page 34: You Can Tune Your Own SQL Code

You CAN Tune Your Own SQL Code

Page 35: 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/

Page 36: You Can Tune Your Own SQL Code
Page 37: You Can Tune Your Own SQL Code
Page 38: You Can Tune Your Own SQL Code
Page 39: You Can Tune Your Own SQL Code

StraightPathSQL.com/ucandoit

Page 40: You Can Tune Your Own SQL Code

Confused?Angry?Lost?