SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Events
Classes
About Us
Login / Register
Subscribe RSS Feed 

Takes Query Plan Analysis and Performance Monitoring to a New Level

Performance Tuning Puzzle - Part 1

Tuning takes practice, right? Well, here's your chance. We've got a fairly simple query that just returns a count, and with our default setup this query is not cheap - can you fix it? Can you get it from 700+ reads to 200? To less than 10? The references link for this video contains the database (SQL 2008) and the query, just attach the database and see what you can do. In the next view we'll look at some of the steps you might take to get warp speed from this query.

Duration:
1 mins 51 secs
Skill Level:
100
Rating:
4.06 out of 5
Publish Date:
April 28, 2010
Performance Tuning Puzzle - Part 1 You must be logged in to view this video.  
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Andy Warren
Andy Warren is a software trainer focusing on SQL Server, a member of the PASS Board of Directors, and a principal in this site - SQLShare.com.

References



Comments
Robert McCormick on 4/29/2010
I love the idea of interactive puzzles like this. What a great way to learn.

Robert McCormick on 4/29/2010
1) I added a nonclustered index on registrations.eventID (covering registrationsStatus) 2) I rewrote the table using a join instead of using "in." SELECT COUNT(*) FROM dbo.Registrations r JOIN dbo.RegistrationStatus rs ON r.RegistrationStatus = rs.RegistrationStatusID AND rs.CountAsAttending = 1 AND EventID = 49 This reduced the # of reads on Registrations to 3 and the # of reads on RegistrationStatus to 2.

andre porter on 4/29/2010
I also used the nonclustered index added in your step1 but instead of rewriting the query I... 1) added a clustered index on the RegistrationStatus tbl that included (RegistrationStatusID & CountAsAttending) 2) I added column statistics that DTA recommended on the Registrations tbl for (RegistrationStatus and EventID). Profiler shows the reads droping from 600+ to 300 and then finally to 7.

Jason Willis on 4/30/2010
It's good to have something like this. If people actually use this they can actually learn the lessons that are presented.

Feodor Georgiev on 4/30/2010
Awesome idea!

Kristeen Sellers on 4/30/2010
Love the challenge!

Royston Neal on 4/30/2010
Great idea

Prince on 4/30/2010
gud

Lokesh on 4/30/2010
k

Don Weigend on 4/30/2010
Great idea with throwing out a puzzle to solve

Kasey Wheeler on 4/30/2010
Cool stuff...

Kevin on 4/30/2010
Quiz! Yay!

5124CFD2E1 on 4/30/2010
/* step 1: Create a Clustered Primary Key on the Registration..RegistrationID column and on the RegistrationStatus..RegistrationStatusID column step 2: Create Foreign Key Relationship [Registrations].[dbo].[RegistrationStatus])REFERENCES [RegistrationStatus].[dbo].[RegistrationStatusID]) step 2: On Registration Table: Determine selectivity on the eventID column and registrationStatus column which will be used as a nonclustered index (most selective = first column). */

E000099845 on 4/30/2010
got it down to two logical reads on each table with clustered index on each of the id columns. Filtered index on registrationstatus.CountsAsAttending, and non-clustered on Registrations.eventid with Registrationid and registrationstatus included

Mark Hions on 4/30/2010
Output is hard to read - consider a larger font

Chris Howarth on 4/30/2010
Down to 2 reads in *total* by creating an indexed view on a reworked version of the query: CREATE VIEW dbo.Temp WITH SCHEMABINDING AS SELECT COUNT(*) AS CountRows FROM dbo.Registrations r INNER JOIN dbo.RegistrationStatus rs ON rs.RegistrationStatusID = r.RegistrationStatus WHERE rs.CountAsAttending = 1 AND r.EventID = 49 GO CREATE UNIQUE CLUSTERED INDEX [IX_TMP] ON dbo.Temp(CountRows) GO SELECT * FROM dbo.Temp WITH (NOEXPAND) GO Chris

Chris Howarth on 4/30/2010
Previous comment - first line of SELECT statement in the View should be: SELECT COUNT_BIG(*) AS CountRows

Henrique on 4/30/2010
Good but is necessary more information.

Orlando on 5/1/2010
can't wait to see if I can solve the puzzle, I like the format

Richard Willemain on 5/1/2010
excellent

murali on 5/2/2010
good but i ibetter if explanation is more.

Ed Svastits on 5/3/2010
great format of the presentation -- a problem to solve in the first video, then the proposed solution(s) in the second video.

SQLWayne on 5/3/2010
Love this idea. (although it would have helped some of us to tell us where to find the # of reads - I got from profiler) I got the reads down to 4 by adding primary keys to the two tables and adding indexes to RegistrationStatus and CountAsAttending. Did not change to a join as someone else did above. That was a good idea - duh :-)

Christian Bahnsen on 5/3/2010
Great idea. Is the database compatible with SQL Server 2008 Express Edition?

Abi Bellamkonda on 5/3/2010
I noticed something funny. I optimized the query and tables. Restarted the database, as suspected i got 300 reads on 1st attempt, but subsequent runs were good. Anyone else have this?

William Willyerd on 5/5/2010
Where are the links?

colin on 5/5/2010
Brilliant - more of these please!

colin on 5/5/2010
Is there a 2005 equivalent of the mdf please?

Andy on 5/5/2010
Good narration but wheres the puzzle or am I missing a trick? ;-)

Andy on 5/5/2010
Duh... Got the references (Puzzle, .mdf etc)

Olu Dijo on 5/5/2010
Great idea. I would love to follow but I'm having difficulties attaching the mdf file to SQL 2008 express edition. Is it 'cos ldf file is missing?

Rick Olson on 5/25/2010
It's really nice to have these scenarios posted - please keep up the good work!

Jeremy Carroll on 11/5/2010
among other things I added an index to the dbo.RegistrationStatus table using the ID column together with the CountAsAttending column. I also made the ID the PK. I added a filter of CountAsAttending = 1 to the index. Got an index scan on the new IX and an index seek on the PK. Reads = 0.

douglas williams on 5/25/2011
I get it. The puzzle is to try and figure out how to attach the database without the log file. Still working on it...



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions