35
DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: [email protected] SYSolutions, Inc. – SQLSkills.com Email: [email protected]

DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: [email protected]

Embed Size (px)

Citation preview

Page 1: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

DBA 328

Designing for Performance: Optimization with Indexes Kimberly L. Tripp

Solid Quality Learning – SolidQualityLearning.com Email: [email protected]

SYSolutions, Inc. – SQLSkills.comEmail: [email protected]

Page 2: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

IntroductionKimberly L. Tripp, SQL Server MVPPrincipal Mentor, Solid Quality Learning* In-depth, high quality training around the world! www.SolidQualityLearning.com

Content Manager for www.SQLSkills.com Writer/Editor for TSQL Solutions/SQL Magwww.tsqlsolutions.com and www.sqlmag.com

Consultant/Trainer/SpeakerCoauthor for MSPress title: SQL Server 2000 High AvailabilityPresenter/Technical Manager for SQL Server 2000 High Availability Overview DVDVery approachable. Please ask me questions!

Page 3: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Overview

Selectivity

How to Improve Queries with Varying Search Arguments (SARGs)

Indexing for AND

Indexing for OR

How to Improve Joins

How to Improve AggregationsIndexes on Base Tables

Indexed Views

Page 4: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

SelectivityNot just based on the Number of Rows Returned

Always Relative to the Number of Rows in the Table (usually expressed as a percentage)

Low Number = High SelectivityAny Index is Useful if even ONE condition is highly selective!

High Number = Low SelectivityThis is harder!

Page 5: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

How to Improve Search ARGs

Don’t use * in your queries! Limit Column List Returned to Client (views!)

Make Sure You Limit Rows – ALWAYS Use a WHERE Clause

Isolate ColumnsMonthlySalary > 600000/12 -- CAN Seek

MonthlySalary * 12 > 600000 -- MUST Scan

Minimize Wildcards – Avoid Leading Wildcards

Always supply a join condition for every table

Page 6: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Low Selectivity Queries

Limited Select List

Index On Columns Requested

SELECT LastName, FirstName, PhoneNoFROM dbo.Member

WHERE LastName LIKE ‘[S-Z]%’

-- 10000 Rows, 3072 in S-Z Range

Covering!A Mini Clustered Table of Just the Data You Need!

Page 7: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Options to Access Data

Table Scan

Nonclustered on LastNameBookmark Lookups for Every Row

Nonclustered on LastName, FirstName and PhoneNo

Nonclustered on FirstName, LastName, PhoneNo

Page 8: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Actual Costs

Table Scan = 184 Reads

NC LastName = 6354 Reads

NC Covering Seek = 19 Reads

NC Covering SCAN = 59 Reads

Page 9: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Indexing for AND

AND Progressively Limits the SET

All Conditions MUST be true

Find the SMALLEST Set and work from there.

Evaluate Columns in WHEREA Single Highly Selective Condition

Any combination of Highly Selective Criteria

If NOTHING Yields a Selective Set Consider Covering

Page 10: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Indexing for OR

OR Is Similar to UNION

ANY Condition must be true

Optimally find each set or scan for the whole thing – requires useful indexes for EACH AND EVERY condition

Consider re-writing as UNIONInclude a Row Identifier – such as the Primary Key – to get same results (OR only equals UNION if the SELECT list has a UNIQUE row identifier)

Test!

Page 11: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Indexing for Joins

Tables are Joined Two Tables at a Time

Each Table has a Join Condition

Optionally Each Table has a SARG

Usually the Join is Between the Primary and Foreign Key

Always Try to Give SQL Server as Many Options to Choose From…

Page 12: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Best Options for Joins

Table1Table1 Table2Table2

SARG1Join Col PK

SARG2Join Col FK

One Join Strategy Might use Table1 SARG1 to Table2 Join

Another could use Table2 SARG2 to Table1 Join

Another could use the Join Indexes on Both Tables

BUT if ALL 4 Indexes are there then the Join has the best Options to choose from!

Do you already have individual indexes on each and all of these columns?

Foreign Key???

Page 13: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Cover the Combination

SARG1Join Col PK

SARG2Join Col FK

Not Using Any of These Indexes?

Performance Still Awful?

Cover the Combo – Create 4 Indexes (2 Per Table) Table1 (SARG, Join) – Priority to the Search ConditionTable1 (Join, SARG) – Priority to the Join Condition

Test!

Still not working?

Table1Table1 Table2Table2

Page 14: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Cover One/Both of the Tables

SARG1Join Col PK

SARG2Join Col FK

Not Using Any of These Indexes?

Performance Still Awful?

Try Covering – Create Indexes to Cover the Individual Tables requested… Trying them in both orders – Priority for the Join and Priority for the SARG.

Still not working?Table1Table1 Table2Table2

Page 15: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Indexing for Aggregations

Two types of Aggregates Stream and Hash

Try to Achieve Stream to Minimize Overhead in temp table creation

Computation of the Aggregate Still Required

Lots of Users, Contention and/or Minimal Cache can Aggravate the problem!

Page 16: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Aggregate Query

Member has 10,000 Rows

Charge has 800,000 Rows

SELECT member_no AS MemberNo,

sum(charge_amt) AS TotalSales

FROM dbo.charge

GROUP BY member_no

Page 17: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Aggregate QueryNo Indexes

Table 'charge'. Scan count 1, logical reads 4858

Page 18: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Aggregate QueryHash Match

Table 'charge'. Scan count 1, logical reads 2275

Page 19: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Aggregate QueryStream

Table 'charge'. Scan count 1, logical reads 2275

Page 20: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Aggregate QueryIndexed View

Requirements:Enterprise Edition Only (or Developer – for testing)

View Must Be SCHEMABOUND to the Base Table

View Must include COUNT_BIG if Aggregate in View

Concern:Test Performance of INSERT/DELETE/UPDATE to Base Table

Benefits:Data is Pre-aggregated, no hash or stream

Minimized Contention, Minimized Cache Utilization

Check out the Resources Listed at the End of this Session for more info and reading about Indexed Views!

Page 21: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Aggregate QueryIndexed View

Table 'charge'. Scan count 1, logical reads 34

Page 22: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

What’s the Diff?

Page 23: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Index Strategies

Determine Primary Usage of Table – OLTP vs. OLAP vs. Combo? This determines Clustered Index

Create Constraints – Primary Key and Alternate/Candidate Keys

Manually Add Indexes to Foreign Key Constraints

Capture a Workload and Run through Index Tuning Wizard

Continue to test, tune and troubleshoot and Add Additional Indexes using these strategies

Page 24: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Summary

Limit the Search

Limit Columns Requested

Prioritize OLTP/OLAP choose Clustered Index Strategy

Add Nonclustered for PK/UK/FK

Add Nonclustered for SARGs

Consider Covering for Low Selectivity

Test, Test, Test!

Page 25: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Review

Selectivity

How to Improve Queries with Varying Search Arguments (SARGs)

Indexing for AND

Indexing for OR

How to Improve Joins

How to Improve AggregationsIndexes on Base Tables

Indexed Views

Page 26: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

DAT 335 – SQL Server Tips and Tricks for DBAs and DevelopersTuesday, 1 July 2003, 15:15-16:30

DBA 324 – Designing for Performance: Structures, Partitioning, Views and ConstraintsWednesday, 2 July 2003, 08:30-09:45

DBA 328 – Designing for Performance: Optimization with IndexesWednesday, 2 July 2003, 16:45-18:00

DBA 322 – Optimizing Stored Procedure Performance in SQL Server 2000Thursday, 3 July 2003, 08:30-09:45

Other Sessions…Other Sessions…

Page 27: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Articles in TSQLSolutions at www.tsqlsolutions.com (FREE, just register)

All About Raiserror, InstantDoc ID#22980

Saving Production Data from Production DBAs, InstantDoc ID#22073

Articles in SQL Server Magazine, Sept 2002:Before Disaster Strikes, InstantDoc ID#25915

Log Backups Paused for Good Reason, InstantDoc ID#26032

Restoring After Isolated Disk Failure, InstantDoc #26067

Filegroup Usage for VLDBs, InstantDoc ID#26031

Search www.sqlmag.com and www.tsqlsolutions.com for additional articles

Articles…Articles…

Page 28: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Check out www.SQLSkills.com for scripts, demos, links and new resources!

Inside Microsoft SQL Server 2000, Kalen Delaney, MS Press, ISBN: 0735609985 http://www.insidesqlserver.com/

Whitepaper: Database Architecture: The Storage Engine, http://msdn.microsoft.com/library/en-us/dnsql2k/html/thestorageengine.asp?

Register on www.tsqlsolutions.com to get free access to technical TSQL articles from SQL Server Magazine. Check out Instant Doc ID#23733 for the “n-Table Joins” article for more information on adding redundant keys.

Resources…Resources…

Page 29: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Whitepaper: Statistics Used by the Query Optimizer in Microsoft SQL Server 2000, http://msdn.microsoft.com/library/techart/statquery.htmWhitepaper: Microsoft SQL Server 2000 Index Defragmentation Best Practices http://www.microsoft.com/technet/treeview/default.asp?url=/technet/prodtechnol/sql/maintain/Optimize/SS2KIDBP.asp Support WebCast: SQL Server 2000 Profiler: What's New and How to Effectively Use It http://support.microsoft.com/default.aspx?scid=%2Fservicedesks%2Fwebcasts%2Fwc111400%2Fwcblurb111400%2Easp

Resources…Resources…

Page 30: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Community Resources

Community Resourceshttp://www.microsoft.com/communities/default.mspx

Most Valuable Professional (MVP)http://www.mvp.support.microsoft.com/

NewsgroupsConverse online with Microsoft Newsgroups, including Worldwidehttp://www.microsoft.com/communities/newsgroups/default.mspx

User GroupsMeet and learn with your peershttp://www.microsoft.com/communities/usergroups/default.mspx

Page 31: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Ask The ExpertsGet Your Questions Answered

I will be available in the ATE area after most of my sessions!

Page 32: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Kimberly L. Tripp

Principal Mentor, Solid Quality Learning Website: www.SolidQualityLearning.com

Email: [email protected]

President, SYSolutions, Inc.Website: www.SQLSkills.com

Email: [email protected]

Thank You!Thank You!

Page 33: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

Suggested Reading And Resources

The tools you need to put technology to work!The tools you need to put technology to work!

TITLETITLE AvailableAvailable

Microsoft® SQL Server™ 2000 Microsoft® SQL Server™ 2000 High Availability: 0-7356-1920-4High Availability: 0-7356-1920-4

7/9/037/9/03

TodayTodayMicrosoft® SQL Server™ 2000 Microsoft® SQL Server™ 2000 Administrator's Companion:0-Administrator's Companion:0-7356-1051-77356-1051-7

Microsoft Press books are 20% off at the TechEd Bookstore

Also buy any TWO Microsoft Press books and get a FREE T-Shirt

Page 34: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

evaluationsevaluations

Page 35: DBA 328 Designing for Performance: Optimization with Indexes Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com Email: Kimberly@SolidQualityLearning.com

© 2003 Microsoft Corporation. All rights reserved.© 2003 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.