In this book, the world’s leading practitioners of SQL Server present a collection of articles on techniques and best practices for SQL Server development and administration based on many years of combined experience. The 53 MVPs who contributed to the book each picked an area of special interest to them and shared their insights and practical knowhow with you. The topics covered will appeal to a broad range of readers with varied levels of SQL Server experience, from beginner to advanced.
Trang 1Chris Shaw ● Gail Shaw ● Linchi Shea ● Jason Strate ● Paul Turley ● William Vaughn ● Peter Ward ● Joe Webb ● John Welch ● Allen White ● Thiago Zavaschi
Trang 2SQL Server MVP Deep Dives
Volume 2
Trang 5For online information and ordering of this and other Manning books, please visit
www.manning.com The publisher offers discounts on this book when ordered in quantity For more information, please contact
Special Sales Department
Manning Publications Co
20 Baldwin Road
PO Box 261
Shelter Island, NY 11964
Email: orders@manning.com
©2012 by Manning Publications Co All rights reserved
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps
Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine
Development editor: Cynthia Kane
ISBN 9781617290473
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 16 15 14 13 12 11
Trang 6To all the children of Operation Smile
Trang 8Mladen Prajdic´ 28
Abolfazl Radgoudarzi 48 Denis Reznik 29
Rafael Salas 52 Edwin Sarmiento 44 Chris Shaw 3 Gail Shaw 8 Linchi Shea 35 Jason Strate 33 Paul Turley 43 William Vaughn 42 Peter Ward 13 Joe Webb 10 John Welch 49 Allen White 30 Thiago Zavaschi 56
Trang 10brief contents
P ART 1 A RCHITECTURE 1
Trang 11P ART 5 B USINESS INTELLIGENCE 395
Trang 12xi
Trang 14contentspreface xxxv
acknowledgments xxxix about Operation Smile xli about this book xliii about the editors xlv about SQL Server MVPs xlvi
P ART 1 A RCHITECTURE 1
Trang 152 “Yes, we are all individuals”
A look at uniqueness in the world of SQL 16
R OB F ARLEY
Primary keys 17 ■ Unique constraints 18 ■ Unique indexes 19
Advantages of the unique index 19 ■ Advantages of the unique constraint 20
The good and the bad of DISTINCT 20 ■ DISTINCT or GROUP
BY 21 ■ Are they needed at all? 22 ■ Unnecessary grouping 23 Being guided by “that” error 24
C HRIS S HAW
IDENTITY case in point 27 ■ Database design and scalability 28
Overnormalized 30 ■ Undernormalized 30
GUIDs as primary keys 31 ■ System-generated integers as primary keys 32 ■ Generating your own ID values 32
Trang 16RAID 0 49 ■ RAID 1 50 ■ RAID 5 51 ■ RAID 6 51
RAID 10 52 ■ RAID 50 52 ■ When to use RAID 5 52
When to use RAID 6 53 ■ When to use RAID 10 53
Index files 54 ■ Transaction log files 54 ■ tempdb database 54
Correcting disk alignment on Windows 2003 and earlier 55
Correcting disk alignment in Windows 2008 and later 56
Correcting after the partition has been created 56 ■ Aligning
on the array 56
Snapshots with a VSS-enabled storage array 57 ■ Snapshots
with a non-VSS-enabled storage array 57 ■ Snapshots as a backup
process 58 ■ Using snapshots to present storage to downstream
Trang 17P ART 2 D ATABASE ADMINISTRATION 65
7 Increasing availability through testing 67
A LLAN H IRT
G AIL S HAW
Recovery model and availability of log backups 80 ■ SQL Server Edition 80 ■ Page type of the damaged page 80
G REG L ARSEN
J OE W EBB
Trang 18J OHN M AGNABOSCO
Our superpowers 106 ■ Tools of the trade 107
P AWEL P OTASINSKI
P ETER W ARD
Trang 19Surface area configuration 132 ■ Sysadmin membership 134
Policy-Based Management with Central Management
R ODNEY L ANDRUM
Utility Explorer—best tools for the DBA 146
S ATYA S HYAM K J AYANTY
Trang 20T OBIASZ J ANUSZ K OPROWSKI
Service Level Agreements: the context for high
P ART 3 D ATABASE DEVELOPMENT 175
A ARON B ERTRAND
Trang 21Inline table valued function solution 190 ■ Set-based solution 191
J OHN P AUL C OOK
S ERGIO G OVONI
SQL Server 2005 and 2008 solution 201 ■ SQL Server Denali solution 201 ■ Comparing execution plan results 202
Tips for using SEQUENCE 203 ■ Restrictions 203
H UGO K ORNELIS
…But do you need it at all? 212 ■ Representations and conversions 213 ■ How about NULL? 214
Starting the project 214 ■ Adding the fields for the native representation 215 ■ Editing the signature 215 ■ Converting between
Trang 22Background 224 ■ An incredibly brief introduction to regular expressions, matches, and groups 225 ■ Regular expressions and SQL Server 226 ■ Regular expressions and the NET Framework 226
B EN M ILLER
Operating system configuration 247 ■ SQL Server configuration 248
Trang 23Unit test for databases 256 ■ T-SQL unit test walkthrough 257
M LADEN P RAJDIC ´
D ENIS R EZNIK
Using the HierarchyId data type 279 ■ Physical HierarchyId data organization 280
Depth-first indexes 283 ■ Breadth-first indexes 284
A LLEN W HITE
Security 288 ■ Message types 289 ■ Contracts 289 Queues 289 ■ Services 289 ■ Conversations 290 Endpoints 290 ■ Routes 290 ■ Remote service binding 291
Trang 24P ART 4 P ERFORMANCE TUNING AND OPTIMIZATION 297
G LENN B ERRY 299
G RANT F RITCHEY
OPTIMIZE FOR 313 ■ WITH RECOMPILE 315 ■ Local variables 316 ■ Plan guides 316 ■ Turn off parameter sniffing 318
Trang 25An introduction to waits and queues 331
R OBERT P EARL
Why use wait stats? 332 ■ Wait type categories 332
General I/O issues 339 ■ Buffer I/O latch issues 339 ■ Blocking and locking 340 ■ CPU pressure 340 ■ Parallelism 340 ■ Memory pressure 341
L INCHI S HEA
Trang 26B RAD M M C G EHEE
Start with standard performance-tuning techniques 357 ■ Take advantage of minimally logged operations if appropriate 358 Select a fast I/O subsystem 358 ■ Align disk partitions 358 Remove physical file fragmentation 358 ■ Preallocate transaction log file size 359 ■ Separating data and log files 359 ■ Managing virtual log files 360 ■ Perform transaction log backups often 360 ■ Schedule database maintenance during slow times 361
SELECT columns 364 ■ Data 364 ■ Sketch 365 Pseudocode 366
Too much data 367 ■ Functions 367 ■ Non-SARGable WHERE Clauses 368
T IM C HAPMAN
Trang 27Data collector sets using PAL template files 375 ■ Logman 376 SQLDiag 376
Counter Log 377 ■ Threshold File 378 ■ Questions 380 Output Options 380 ■ File Output 381 ■ Queue 381 Execute 381
J UNGSUN K IM
P ART 5 B USINESS INTELLIGENCE 395
report part library 397
J ESSICA M M OSS
Level 1 400 ■ Level 2 400 ■ Level 3 401 ■ Level 4 401
Creating the initial report part 402 ■ Using report parts and change requests 403 ■ Existing report part changes and publication 403
Trang 28G REG L OW
W ILLIAM (B ILL ) V AUGHN
Adding a report element property expression 412 ■ Coding a report Code property expression 414
Trang 29Create a DNS entry for the NLB cluster application 437 ■ Configure the server network cards 437
R OBERT C AIN
Creating the main dataset 450 ■ Creating the parameter list shared dataset 451
Trang 30A NDY L EONARD
File it 496 ■ Ignore it 496 ■ Update the fact later 497
Trang 31T IM M ITCHELL
Before we get started…a word of caution 516 ■ Sources, destinations, and transformations…oh my! 516 ■ Synchronous and asynchronous behavior 518 ■ Script component inputs and outputs 519 ■ Into the code 520
Surrogate key generation 531 ■ Data update strategy 532
Alerting 533 ■ Recovery and restart ability 533 ■ Audit, balance, and control support 535 ■ Runtime event logging 535 ■ ETL metadata and reporting 536
J OHAN Å HLÉN
Sample usage scenarios 539 ■ Technology overview 539
Creating the Visual Studio project 539 ■ Creating the relational database emitter 540 ■ Creating the Analysis Services emitter 540
Trang 32Creating the SSAS database 540 ■ Creating the data sources 541 Creating the Data Source View 542 ■ Creating the dimensions 543 Creating the cubes 545
Scripting databases 554 ■ Modules and snap-ins 555
DAX data types 569 ■ Sample database 570
Golden rules 571 ■ Functions that return a single date 571 Functions that return a table of dates 572 ■ Functions that evaluate expressions over a time period 573
Trang 33G OGULA A RYALINGAM
Cards (or images) 578 ■ Slicing and dicing options 579 ■ Filtering and sorting options 579
Trang 34index 617
Trang 36preface
This is the second volume of a book that many people thought would never see thelight of day In early 2007, the editor of the first volume, Paul Nielsen, had an extraor-dinary idea I’ll let him tell you about how this idea came into being, by including asection from the preface to Volume 1:
Each year Microsoft invites all the MVPs from every technology and country to
Redmond for an MVP Summit—all top secret—“don’t tweet what you see!”
During the MVP Summit, each product team holds a series of presentations
where they explain their technologies, share their vision, and listen to some
honest feedback At the 2007 MVP Summit in Seattle, Bill Gates presented his
vision of the future of computing to the MVPs and then took questions for about
an hour I really enjoy these dialogues I get the sense that if BillG wasn’t the
founder of Microsoft, he’d make a great MVP You can tell he likes us as fellow
Geeks, and he’s rather candid in the MVP Q&A time It’s one of my favorite parts
of the MVP Summit
During the Q&A, the lines at the microphones are far too long to bother to join,
so I daydream a few questions I’d ask BillG:
■ As the world’s wealthiest Geek, what can you tell me about your PC?
■ Even with all your accomplishments, do you still find the most happiness
and satisfaction with your family?
■ Do you play Age of Empires 2, and if so, want to join me in a game?
■ Kirk or Picard?
■ Can I buy you lunch?
And then I thought of a good, deep, Charlie Rose-type of question: “Centuries
from now, would you rather be remembered as the guy who put a computer on
every desk, or as the guy who ended malaria and fought the good fight against
poverty?” As I try to guess what BillG might say, the answer is obvious I’m glad
that BillG’s intellect and resources are being directed at improving the human
condition, and as an original Windows fan I’m proud of BillG But the answer to
Trang 37my question is both—Windows has already done as much to fight poverty as will
the Bill and Melinda Gates Foundation
Toward the end of the Q&A time, which was mostly taken up with technical
questions, I was thrilled to hear one of the MVPs ask for his advice as a
philanthropist BillG said that we should all be involved in our communities and
give of ourselves in creative ways: at the library, at schools, and with charities “Do
philanthropy where you are.” This idea of giving of ourselves is central to the
MVP community
Paul then went on to describe how he decided he could make a difference “where hewas” by writing about his passion, SQL Server, and using it to create a charity book tohelp children He enticed me into the project, and after communicating with the rest
of the SQL Server MVPs to determine if there was enough interest to make a project ofthis type viable, together we started approaching publishers
It didn’t take long to find that Manning Publications was extremely interested inproducing this kind of project Michael Stephens liked both the community aspect ofthe project and the charity goals Manning also offered us a higher-than-usual authorroyalty, because we were giving it all to charity We recruited four other prominentMVPs to help with the editing, and the project was underway
A project such as this had never been done before We had 53 independentauthors from all around the world trying to collaborate Figuring out how to managethe technical editing and rewrites, and dealing with writers who were technicallyextraordinary but lacking in some writing skills, and deciding what to do with a chap-ter that came in at 40 pages when all the rest were 10–15 pages, made the projectmuch more time-consuming than we expected Many of the MVPs who had writtenchapters early in the process despaired of ever seeing their work in print
But then it all seemed to come together, just in time for the 2009 PASS Conference,which is the largest conference in the world for SQL Server professionals The book hadbeen available for preorder, and by the time the conference started, the preordersalone had earned the chosen charity more than $10,000! Manning made an all-outeffort to get 200 copies of the book available in print to sell at the conference Almostthree dozen of the MVP authors were speaking at the conference, and they told theiraudiences about this remarkable work we had done On the Thursday of the confer-ence, right after lunch, we launched Volume 1 in a special event in the vendor area andfollowed this with the most popular book-signing in PASS history Most of the attendingauthors stood behind a long table, and eager readers flowed by in front of us, gettingautographs from all the authors present All 200 copies of the book were sold, which wasanother PASS record, and many people who wanted a copy weren’t able to get one andparticipate in the signing Although my hand was numb from signing my name so manytimes, it was the most exciting event of my professional life Volume 1 was so wellreceived that there was immediate talk of another one We all needed a rest, however,and we needed to give the community time to absorb the first terrific volume But in late
2010, Paul and I decided it was time to start thinking about Volume 2
Trang 38Paul and I switched roles: he stepped down from the overall editor role to become
a section editor, and I became the overall editor Kimberly Tripp, Paul Randal, andGreg Low stayed on as section editors for the new volume, and we brought in LouisDavidson and Brad McGehee as new section editors Manning was more than happy
to continue to support us with its publishing team
Like the first volume, this one is divided into five sections aligned with the five jobroles dealing with SQL Server: database architecture and design, database develop-ment, database administration, performance tuning and optimization, and businessintelligence There was no rigid organization to the outline—MVPs were simply asked
to submit abstracts for chapters that they wanted to write, and those abstracts weredirected to the appropriate section editors As in the first volume, the contents aredriven by the MVPs’ individual passions, not by a comprehensive feature list The sec-tion editors selected the best abstracts, but we committed to the idea that every MVPwho wanted to contribute to the book could contribute We had a much tighter dead-line for this volume and also a much stricter page count limit, and we limited eachauthor to a single chapter But we didn’t restrict what the authors could write, andonly insisted that it be never-published material in a topic area that wasn’t specificallyaddressed in Volume 1 So this volume is completely new material!
To select the charity, we collected nominations from the participating MVPs withthe restriction that the charity had to be a secular group that benefited childrenaround the world And we wanted to give to a smaller charity—we didn’t want ourcontribution to be added to a billion-dollar fund The vote this time was overwhelm-ing in favor of Operation Smile, which you can read about on page xli
I’d like to include another paragraph from the preface to the first volume, becausethere is no way I can say this better than Paul did:
If you are reading this book, then you are “rich.” Considering your place in
human history, you’re wealthier than most kings of centuries past—you are well
educated, your grocery store shelves are full, you have a family doctor For too
many in the world, that is not the case There are communities without clean
water, children hurting from war, and AIDS orphans who have no family or place
to sleep When one ponders the immense need and poverty in the world, it’s
easy to become overwhelmed with hopelessness How can a single drop of ink
change the color of an ocean? But we have no other option than to do what we
can My philosophy is that of Confucius: “It is better to light one small candle
than to curse the darkness.” Even BillG can’t heal the world, but we can each
make a difference
By buying this book, you’ve supported Operation Smile We want to reiterate BillG’ssuggestion that we can all find ways to do philanthropy where we are, and this book isone way to start doing that, both for the authors and for the readers
Welcome to SQL Server MVP Deep Dives, Volume 2—a collaborative work by 64
pas-sionate SQL Server MVPs
KALEN DELANEY
Trang 39xxxviii
Trang 40To Marjan Bace, our publisher, thank you for helping us refine the originalconcept, supporting the project from the very first day of Volume 1, and partneringwith us.
To Michael Stephens, Cynthia Kane, Liz Welch, Linda Recktenwald, Mary Piergies,Barbara Mirecki, Candace Gillhoolley, Ozren Harlovic, Marija Tudor, and the rest ofthe team at Manning Publications—thanks for believing in this project and stickingwith it to the end
A big thanks to Victor Isakov, Dave Dustin, Aaron Nelson, Denis Gobo, and MikeWalsh for contributing their time as technical editors Your help was invaluable! And ahuge showering of gratitude to Stacia Misner, Jen Stirrup, Rob Farley, and RobertPearl who stepped up at the last minute to help us deal with an unexpected crunch!
To the reviewers who read early versions of the manuscript and provided helpfulfeedback: Amos Bannister, Nikander Bruggeman, Margriet Bruggeman, Dave Corun,Sanchet Dighe, Richard Handloff, Peter Lee, Massimo Perga, Richard Siddaway, IanStirk, and Deepak Vohra
Many thanks to Ed Lehman and Ed Hickey, our SQL Server product team MVPLiaisons, for their support and the many good times
To our MVP leads through the years—Shawn Aebi, Paul Wehland, Stephen Dybing,Ben Miller, Alison Brooks, Suzanna Moran, Ryan Bolz, and others internation-ally—thank you for supporting our community efforts