SQL Server DBA Training Plan 1 PDF
SQL Server DBA Training Plan 1 PDF
com/needs
How to Develop Your DBA Career
SQL Server DBA
Training Plan
2nd Edition, Winter 2013 - BrentOzar.com/needs
Brent Ozar Unlimited 2013 Page 2 htp://BrentOzar.com/needs
Youre a developer or a sysadmin who wants to become a DBA.
We know. Weve been there too, and were here to share our lessons learned
so that your journey is easier than ours. Were Brent, Jeremiah, Jes, and
Kendra of Brent Ozar Unlimited, a SQL Server consulting company.
I learned to be a DBA the
hard way. The hard, crappy
way. Our SQL Server was in
trouble, and I was the kind
of person who would roll up
my sleeves and fgure out
whatever was broken. Next
thing you know, I was the
one responsible for it.
And boy, did that suck.
I didn't know about any free videos or blogs
or e-books. I didn't have the
budget to go to a class, and
even if I did, I didn't know where
to fnd a good one. DBA wasn't
in my job title, and it wouldn't
be for years.
We want to make your learning
experience much beter than
mine was.
This book - part of our free 6-
Month DBA Training Plan -
covers the basics of the topic,
plus link to our favorite free
training material on that topic.
Your journey will start with
Ozars Hierarchy of Database
Needs, the pyramid shown at
right. In the next six months,
we'll take you from the botom
of the pyramid up to the top.
You may not be able to fx everything in your
environment during those six months, but at
least you'll understand the work involved
and how to confdently get started.
We've collected thousands of resources
over the years -training videos, scripts, blog
posts about everything
fromindexingtocareers - and we're excited
to share them with you. It's all about making
your journey to Professional Database
Administrator easier than ours was.
Welcome to Your Training Plan
Brent Ozar Unlimited 2013 Page 3 htp://BrentOzar.com/needs
If You Have Questions
If you have questions about what you're
reading, start by Googling your questions. It
sounds obvious, but you'd be amazed at how
much good stuf there is out there to help.
(I'm not being sarcastic. This is exactly how
we get started whenever we have our own
questions.)
If you have a really short question and you
expect a really short answer, and youre on
Twiter, tweet the question with #SQLhelp in
your tweet. Lots of community members
monitor this hash tag, so even if they dont
follow you personally, theyll still see the
tweet and respond. You can read more about
#SQLhelp too.
If you'd like to post a question, try
DBA.StackExchange.com or
SQLServerCentral's forums. Yes, both of
these require registration, but they're totally
worth it. On both of these sites, there's
hundreds - sometimes thousands - of people
who are itching to help answer your
questions. They react fast, too - make sure
to go back and revisit your question every
10-15 minutes for the frst few hours to see
what's happening. Answer their clarifcation
questions, and include as much detail as you
can. For more instructions, read Geting Help
with a Slow Query.
If you still can't get the answers you need,
email us at Help@BrentOzar.com. This is a
real email address manned by the real
people at Brent Ozar Unlimited. This isn't
one of those emails where it says, "Don't hit
respond because nobody cares." Seriously,
we care, and that's why we put these emails
together. Just please don't use that as your
FIRST resort - we're real people with real
jobs and real families, and there's only so
many hours per week that we can spend
answering questions. By using the above
methods frst, you'll be able to leverage the
whole community's expertise instead of just
a few of us.
I f
g
h
t
crim
e. A
n
d
N
O
L
O
C
K
h
in
ts.
S
Q
U
E
E
E
E
!
I
f
I
h
a
v
e
a
s
o
n
,
I
l
l
n
a
m
e
h
i
m
J
u
n
i
o
r
.
J
u
n
i
o
r
D
B
A
.
My other
database is a ham
sandwich.
Brent Ozar Unlimited 2013 Page 4 htp://BrentOzar.com/needs
Lets start by making an Excel spreadsheet of the servers weve got, the
number of databases on each one, and over the coming weeks well fx them.
Build a Server Inventory
by Brent Ozar
At your company, walk into
the VP of Sales's ofce
and ask them how many
salespeople they have.
NO, I mean, don't actually
DO that, because he's
going to ask you why the sales app is so
slow. But I mean, imagine if you COULD walk
into his ofce and ask him that. I bet he
would have an instant answer. He wouldn't
wait for a single moment. Or walk into the
CEO's ofce and ask how many employees
he has. Or ask the CFO how much the annual
budget is.
My point is that when you're in charge, you
need to know exactly what you're in charge
of.
Make a Spreadsheet Inventory
Let's start by making a spreadsheet. Across
the top, make columns for:
SQL Server Version (2012, 2008, 2005)
Edition (Standard, Enterprise, Developer)
Environment (Production, QA,
development, disaster recovery)
Department (sales, HR, accounting, IT,
mixed use)
Business Users Afected (list of people to
email when the server dies)
Application Names (internal or external
product names)
Plan B
That last column gets a litle tricky - it
means, if this server dies in a fre, what's our
Plan B? Are we going to restore the
databases from another server? Will we fail
over to a log shipped copy? Or will we
update our resume and head out for an early
lunch? As we go farther into the training,
we're going to get much more specifc about
Plan B.
There's no wrong answers here - week 1 is
about understanding where we're at today,
not where we'd like to be. We're never where
we'd like to be. (Me personally, I'd like to be
at a poolside bar right now, but noooo, I'm in
a hotel room waiting for my girlfriend to
fnish blow drying her hair. If you've ever
wondered why I write so much, you can thank
her full head of hair.)
If you'd like to get ambitious, add additional
columns for Core Count, CPU Count, and
Memory. The core and CPU counts will get
you a head start on licensing, although I
have to confess that we're not going to cover
licensing as part of our training plan.
What We'll Do With This Spreadsheet
Right now, you probably sleep well at night
thinking you know everything that's
happening in these servers. Hoooweee, have
I got bad news for you. Over the next six
months, we're going to progressively add
more and more columns to this spreadsheet
as we learn more about our environment,
uncover problems, and learn how to solve
them.
For bonus points, add a column for What
Scares Me. Write a quick note about the one
Brent Ozar Unlimited 2013 Page 5 htp://BrentOzar.com/needs
thing that scares you
most about this server.
Maybe it's blocking
problems, maybe it's the
failing jobs, maybe it's
code you don't
understand. Six months
from now, I bet you'll be proud of how this
column has changed.
How to Survey Your Network for Servers
Put a row in the spreadsheet for every server
you have - whether you're in charge of it or
not. We want to start with a good inventory
of what we have, and there's two good free
tools to do it.
Microsof Assessment and Planning Toolkit -
it's actually designed for licensing
compliance, but it works great for building
server inventories. It scans your network
looking for whatever programs you pick, but
just confne it to SQL Servers only.
Quest Discovery Wizard for SQL Server-it's
a GUI tool that pings all the servers in your
network and tries to fgure out if they've got
SQL Server installed. If you're in a small
shop where your account has admin
privileges in the domain, you might fnd a lot
more servers than you expected.
We don't get paid for
plugging these products,
and we're always on the
lookout for similar
inventory-building tools,
so if you know of a beter
one, email it to us at
Help@BrentOzar.com.
PSST: Ask About This Before Geting Hired
When you take a new job as a DBA, the very
frst question you should ask the company is,
"Do you have a list handy of all the SQL
Servers I'll be managing? I don't have to see
the list - I understand if you have security
concerns - but I just want to know if that list
exists."
Most of the time...it won't.
This question serves two purposes: it tells
YOU if the company has their act together
when it comes to documentation, and it tells
THEM that you're the right person to
manage their database servers. If they don't
have the list, they're going to want that list
right away. Now's your chance to explain
how you would go about gathering that
information (armed with the info in this
email.)
Bonus points: create a SQL Server Support
Matrix, a document that explains for your
developers and end users what's allowed in
production, DR, QA, and development. This
helps set expectations going forward - if a
server's going to be production, then it has
to be stable, and that means making sure
changes don't happen accidentally.
I created that sample support matrix when I
worked as a DBA, and I've shared it so you
can do a save-as and get a fast start on your
own. Hope that helps!
Most DBAs dont
actually have a list of
their servers.
Brent Ozar Unlimited 2013 Page 6 htp://BrentOzar.com/needs
by Brent Ozar
The only reason we do
backups is so we can do
restores.
When I frst started out
as a SQL Server DBA, I
thought things were
going well as long as the backup jobs ran
successfully. I'd go into SQL Server Agent
every now and then, make sure they were
still running, and ... that was the end of it. I
fgured if disaster ever struck, I'd just do a
restore. How hard could it be?
In theory, you test our backup strategy
ahead of time with Kendra's 5 Simple
Questions About Your Backups, and youve
memorized the 9 Leters that Get DBAs
Fired, along with your companys answers.
In practice, small disasters strike all the
time. The most common reasons to do
restores aren't to revive an entire server - it's
just to get back a few small tables or an
individual database. Somebody ran the
wrong DELETE statement or dropped a
database in production instead of
development, and next thing you know, we're
all scrambling. Let's think through a few
things ahead of time to make the crisis
easier.
Where to Do Restores
When you're restoring code (stored
procedures, views, triggers, etc) or
individual tables, don't restore onto the
production server. I don't like touching
production servers more than I have to, and
let's face it - you're already having a bad
enough day as it is. That's why you're doing a
restore, remember? So let's do our work on a
diferent server (like dev or QA) and leave
production as it is. I've also writen about
restores in my ideal dev, test, and production
environments.
Afer we've safely restored the right data
onto another server, it's easy to copy that
data across to other servers. For simplicity
and security, you can set up a linked server
on the production box with read-only access
over to the restore server. Then, from
production, you can run INSERT statements
using a SELECT sourced from the linked
server tables.
However, if you're restoring tables over
10GB, you'll probably want to do the restores
directly on the production server to make the
data copies faster. Just make sure you're
extremely careful with the scripting and the
database names - we don't want to restore
over the top of your working production
database.
This may require adding extra space to the
production server. In one emergency, I freed
up the necessary space by shrinking all of
TempDB's data and log fles down to just
1MB. TempDB was on fast drives, perfect for
a one-time emergency restore, and that
Bootcamps and
certifcation
programs alone wont get
you that frst job not when
your competition has
experience.
(And yes, this means were assuming youre backing them up.)
The Reason We Back Up
All Those Databases
Brent Ozar Unlimited 2013 Page 7 htp://BrentOzar.com/needs
particular server didn't have any other
activity happening due to the outage. We're
not always so lucky, but it helps to think out
of the box like that.
A word of warning: if referential integrity is
involved, like if you're trying to restore tables
that have relationships to other tables that
you're NOT restoring, then you can be in for
a world of hurt here. We're not going to cover
that scenario - it really is diferent in each
case.
Doing the Restore
Restore the most recent full backup using
the WITH NORECOVERY option - this is
really important. This leaves the database in
a restoring state so that you can continue to
apply additional backups to it. If you forget
those two key words, your restore has to
start over again from scratch, so please, for
the love of all that's holy, double-check that
option before you start the restore.
When I'm restoring code or confg tables that
haven't changed since the last full backups, I
don't bother restoring
any subsequent
diferential backups or
transaction log
backups. The goal is to
fnish the restore
quickly.
Next, if diferential
backups are involved, restore the most
recent diferential WITH NORECOVERY.
Diferential backups are cumulative - you
only have to restore the most recent one.
Next, restore all of the transaction log
backups afer the diferential (or, if you don't
have difs, all of them afer the full backup) -
again, using WITH NORECOVERY.
Doing all of this with the GUI sucks. The
more backups you have, the longer this
takes, and the more likely you are to run into
errors. Instead, what you need is a script
that looks at all of the backups in a folder,
plucks out the most recent relevant fles, and
restores them for you automatically, in order.
We'll talk about automating restores in the
next training module.
To learn more about backups and restores,
our favorite geting-started articles are:
Address 1
Billing contact frst name
Phone number
We don't want all of the Smiths ordered by
their address, and then a jumbled mess of
frst names. That wouldn't be as fast and
easy to use. That's why the covering felds go
at the end, and the names go frst - because
we use those.
Selectivity: Why the Last Name Goes First
If you wanted to search for Brent Ozar in the
phone book, you look in the O's for Ozar frst,
and then you'll fnd Ozar, Brent. This is more
efcient than organizing the phone book by
frst name then last name because there are
more unique last names than frst names.
There are probably more Brents in Miami
than Ozars. This is called selectivity. The last
name feld is more selective than the frst
name feld because it has more unique
values.
For lookup tables - meaning, when users
need to look up a specifc record - when
you've narrowed down the list of felds that
you're going to use in an index, generally you
put the most selective feld frst.
Indexes should almost never be set up with a
non-selective feld frst, like Gender. Imagine
a phone book organized by Gender, Last
Name, First Name: it would only be useful
when you wanted a complete list of all
women in Miami. Not that that's a bad thing -
but no mater how much of a suave guy you
think you are, you don't really need ALL of
the women in Miami. This is why non-
selective indexes aren't all that useful on
lookup tables.
This rule is really important for lookup
tables, but what if you aren't trying to look up
a single specifc record? What if you're
interested in a range of records? Well, let's
look at...
I p
refer
o
rd
erin
g
fro
m
fo
o
d
tru
cks.
W
h
o
a
r
e
t
h
e
s
e
p
e
o
p
l
e
?
I
w
a
n
t
a
n
i
n
d
e
x
o
n
r
e
s
t
a
u
r
a
n
t
t
a
b
l
e
s
.
I prefer
ordering trucks
of food.
Brent Ozar Unlimited 2013 Page 27 htp://BrentOzar.com/needs
The Yellow Pages: Another Index
When we need to fnd a dog groomer, we
don't want to go shufing through the white
pages looking for anything that sounds like a
dog groomer. We want a list of organized by
business category:
Business Category
Business Name
Address 1
Phone Number
Then we'll look at the list of businesses, see
which name sounds the coolest and which
address is closest to ours, and we'll call a
few of them. We'll work with several of the
records. Here, we're searching for a range of
records, not just a single one.
Notice that we didn't put the most selective
feld frst in the index. The feld "Business
Name" is more selective than "Business
Category". But we put Business Category
frst because we need to work with a range of
records. When you're building indexes, you
not only need to know what felds are
important, but you have to know how the
user is fetching records. If they need several
records in a row next to each other, then it
may be more helpful to arrange the records
like that by carefully choosing the order of
the felds in the index.
Learning More About Indexes
Indexes are really important, so we'll be
covering these in more depth in the next two
emails. In the meantime, here's a few great
resources on geting started with indexes:
Our index resources page - where we've got
posts and videos about heaps, indexing for
deletes, partitioning, and more.
Our Indexing videos - free 30-minute videos
on indexing mistakes, DBA Darwin Awards,
and how to design smarter indexes with the
DMVs.
SQLServerCentral's Index Stairway - a 15-
part series by David Durant that goes all the
way to indexing internals.
Expert Performance Indexing by Jason
Strate and Ted Krueger (book) - covers how
indexes work and how to pick the right one.
Also available on Kindle.
And fnally, if youd like a video training
session with our very own Microsof Certifed
Master Kendra Litle, weve got a 6-hour set
of videos complete with quizzes and demos:
How to Tune
Indexes and
Speed Up SQL:
6-Hour Training
BrentOzar.com/go/indextunin
Brent Ozar Unlimited 2013 Page 28 htp://BrentOzar.com/needs
by Brent Ozar
Last time, we talked about
the two most common types
of indexes - clustered and
nonclustered. In this week's
episode, we're going to
spend just a paragraph or two covering the
other types of indexes, starting with
covering indexes. HA! Get it? We're covering
covering indexes. Oh, I kill me.
Covering Indexes
Covering indexes aren't actually a diferent
kind of index - it's a term that is used in
combination with a query and an index. If I
have this query:
!"#"$% (?<9B-8EAK #89B-8EAK PU;\A-YEDA<
()*+ =D;1PA;7@A 30")" #89B-8EA ] 5*_8<5
And if I have this index:
$)".%" ,-H"Q ,Q/#89B-8EA/,\>@Y=A9 *-
=D;1PA;7@A G#89B-8EAL ,-$#2H"
G(?<9B-8EAK PU;\A-YEDA<L
Then the index covers all of the felds I need
to run this query. SQL Server will start by
looking up all of the Ozars by last name, and
then the index includes the FirstName and
PhoneNumber felds. SQL Server doesn't
have to go back to the clustered index in
order to get the results I need. This means
faster queries, plus less contention - it
leaves the clustered index (and the other
nonclustered indexes) free for other queries
to use.
Covering indexes are most efective when
you have very frequent queries that
constantly read data, and they're causing
blocking problems or heavy IO.
Filtered Indexes
Say we're a big huge online store named
afer a river, and we constantly add records
to our dbo.Orders table as customers place
orders. We need to query orders that haven't
been processed yet, like this:
!"#"$% *<=A<-YEDA< ()*+ =D;1*<=A<9
30")" *<=A<P<;>A99A= ] O
The vast majority of the Orders records will
have OrderProcessed = 1, because we keep
all of our order history in this table. If we
create an index on the OrdersProcessed
feld, it's going to have a lot of data - but
we're never going to run queries looking for
OrderProcessed = 1. Starting with SQL
Server 2008, we can create an index with a
WHERE clause:
$)".%" ,-H"Q ,Q/*<=A<!B8BY9 *-
=D;1*<=A<9 G*<=A<-YEDA<L 30")"
*<=A<P<;>A99A= ] O
Covering, fltered, full text, XML, heaps, 3x5 index cards, you name it.
More Kinds of Indexes
Brent Ozar Unlimited 2013 Page 29 htp://BrentOzar.com/needs
Now, we have an index on just a few records -
a small subset of our table!
Full Text Indexes
Let's say we have a table called
dbo.MoviePlots, and it had a Description
feld where we put each movie's plots. We
know we liked this one movie where a guy
was afraid of snakes, but we couldn't
remember the exact table. We could write a
query that says:
!"#"$% ' ()*+ =D;1+;`?AP@;B9 30")"
HA9><?7B?;\ #,4" 569\8aA965
But that wouldn't be very
efcient. SQL Server
would have to look at
every movie's
description, and scroll
through all of the words
one character at a time looking for snakes.
Even if we index the Description feld, we're
still going to have to scan every row.
Full text indexes break up a text feld like
Description into each word, and then stores
the list of words in a separate index. They're
blazing fast if you need to look for specifc
words - but only as long as you rewrite your
query to use the full text search commands
like this:
!"#"$% ' ()*+ =D;1+;`?AP@;B9 30")"
$*-%.,-!GHA9><?7B?;\K 59\8aA95L
You can even do fun stuf like look for
synonyms or variations on a word. To learn
more about full text indexing, check out:
Books Online on Full Text Indexing -
seriously, stop laughing, the manual's
really good. This link is for SQL 2014/2012,
but keep in mind that there were changes
from 2008 to 2012.
Understanding Full Text Indexing by Robert
Sheldon - covers SQL Server 2008, plus
the diferences between 2005 and 2008
(which were huge).
XML Indexes
You can store XML data natively in SQL
Server tables using XML felds. SQL Server
is aware of the contents - in the sense that
SQL Server knows the content is valid XML -
but it's not necessarily smart about
searching the data.
When you run XML queries, SQL Server has
to roll up its sleeves and parse the XML data.
Every. Single. Time.
That's CPU-intensive,
and a recipe for slow
performance.Instead,
we can create pre-
processed versions of
the XML so we can rapidly jump to specifc
nodes or values.
Primary and Secondary XML Indexes -
Books Online.
Selective XML Indexes - instead of wasting
a ton of space indexing values we never
use, SQL Server 2012 can create the
equivalent of fltered indexes on XML.
Heaps
Heaps are tables with no clustered index
whatsoever. They're tables stored in random
order, data slapped in any old place that fts.
When you want to query a heap, SQL Server
scans the whole freakin' thing. Every. Single.
Time.
Sounds bad, right? Most of the time, it is -
except for a couple of very niche uses. If you
have a log-only table, meaning theres inserts
but never any updates, deletes, or selects,
then a heap can be faster. If you have a
Why did it have
to be snakes?
Brent Ozar Unlimited 2013 Page 30 htp://BrentOzar.com/needs
staging table in a data warehouse, where you
shove a lot of data in quickly and then need
to get it all out at once, then delete all of it, a
heap can be faster. Just make sure you test
it to make sure it's actually faster under your
application's needs.
Picking the Right Indexes for Your Apps
SQL Server's Dynamic Management Views
(DMVs) surface a lot of useful
instrumentation like which indexes are
geting used, which ones aren't geting used,
and which ones SQL Server wishes it would
have had. Unfortunately, they don't give you
a holistic overall picture - they're just raw
data that has to be manually combined and
interpreted. We'll talk about that in coming
lessons, but you need to have this ground
knowledge of index options frst.
Dude, Who Stole My
Missing Index
Recommendation?
by Kendra Litle
Recently, Jes asked the team
an index tuning question: If a query has an
index hint in it, will the optimizer ever
suggest a missing index for that query?
I immediately loved the question because Id
never really thought about it before. I
typically think of index hints as being a very
risky game and avoid them whenever I can
afer all if someone drops the index youve
hinted, any query hinting a non-existent
index will start to fail. (Thats a really bad
day!)
Even so, some people love index hints...
Read the full story: www.brentozar.com/
archive/2013/07/dude-who-stole-my-
missing-index-recommendation/
How to Master Index
Tuning in One Step
by Kendra Litle
Im going to tell you a secret.
Index tuning is complicated, but its
something you can become great at. You just
need to practice it regularly.
Heres that one step: stop thinking index
tuning is a problem for Future You.
Thats it. Really. If you read this headline and
didnt skip the post, your job probably
involves helping an application using SQL
Server go faster. If thats the case, index
tuning is a problem for Present You. Its a
problem for you now, its a problem for you
next month, and its still a problem the month
afer that.
Index tuning isnt something you do once a
year. Its something that you need to do
iteratively that means every month. Over
time, data sizes change, user activity
changes, and the SQL Server optimizer
changes. Each of these things mean that
indexes that are best for an application will
alsochange. As you tune indexes, your query
plans will change and youre very likely to
see more opportunities to add, drop, and
combine indexes emerge. Because of this,
you want to do a few changes every month.
I hear a lot of reasons why people dont tune
their indexes...
Read the full story: www.brentozar.com/
archive/2013/08/how-to-master-sql-server-
index-tuning/
Brent Ozar Unlimited 2013 Page 31 htp://BrentOzar.com/needs
by Kendra Litle
Once up on a time, there was
a database server with
500GB of data and a heavy
read workload of dynamic
queries. Data was updated
frequently throughout the day and index
tuning was a serious challenge. At the best
of times, performance was dicey.
Things went bad
Application performance plummeted. Lots of
code changes had been released recently,
data was growing rapidly, and the hardware
wasn't the absolute freshest. There was no
single smoking gun-- there were 20 smoking
guns!
A team was formed of developers and IT
staf to tackle the performance issue. Early
in the process they reviewed maintenance on
the database server. Someone asked about
index fragmentation. The DBA Manager
said, "Of course we're handling
fragmentation!" But a few queries were run
and some large, seriously fragmented
indexes were discovered in production.
The DBA explained that fragmentation
wasn't the problem. He didn't have
automated index maintenance set up, but
heperiodically manually defragmented
indexes that were more than 75%
fragmented.
Bad, meet ugly
The whole performance team fipped out.
Trust disappeared. Managers squirmed.
More managers were called in.
The DBA tried to change the subject, but it
was just too late. More than a week was
wasted over Fragmentation-Gate. It was a
huge, embarrassing distraction, and it
solved nothing.
Here's the deal-- the DBA was actually right.
Fragmentation wasn't the root cause of the
performance problem. But he made a
miscalculation: he should have set up
occasional automated index maintenance to
align with his team's normal practices and
standards.
Why you need automated index
maintenance
When performance gets bad, one of the very
frst things people look at is whether
systems involvedare confgured according
to best practices. If you're not following a
best practice, you need a good reason why.
Regular index maintenance still has a lot of
merit: even in Shangri-La, where your data
all fts into memory and your storage system
is a rockstar with random IO, index
maintenance can help make sure that you
don't have a lot of empty space wasting
loads of memory.
It's still a good idea to automate index
maintenance. Don't go too crazy with it--
monitor the runtime and IO use and run it
only at low volume times to make sure it
helps more than it hurts.
Indexes are like cars. You have to maintain them, youre probably not doing
it, and if you take them to a mechanic, youll probably get overcharged.
The Parable of Index Maintenance
Brent Ozar Unlimited 2013 Page 32 htp://BrentOzar.com/needs
How much downtime can
you spare?
Before you implement
index maintenance, fnd
out how much time tables
can be ofine in each of
your databases.
If you've got SQL Server
Standard Edition, index
rebuilds are always
ofine.
Even with SQL Server
Enterprise Edition, you
can specify an online
rebuild unless the index
contains large object types. (This
restriction is relaxed somewhat in SQL
Server 2012).
Partitioned tables are especially tricky. You
can rebuild an entire partitioned index
online, but partition level rebuilds are ofine
until SQL Server 2014.
Maintenance plans or custom scripts?
You can go the easy way and use SQL Server
Maintenance Plans, but unfortunately
they're very simplistic: you can only say
"rebuild all the indexes" or "reorganize all the
indexes". You cannot say, "If the index is
45% or more fragmented, rebuild it--
otherwise do nothing." If you don't spend
much time with SQL Server and you've got
downtime available every weekend, this can
be a decent option.
If you need to minimize downtime, custom
index maintenance scripts are the way to go.
Our favorite: Ola Hallengren's maintenance
scripts. These are super fexible, well
documented, and free! The scripts have all
sorts of cool options like time boxing and
statistics maintenance.
Download and confgure them on a test
instance frst. There's a lot of options on
parameters, and you'll
need to play with them.
Get used to the 'cmdexec'
job step types. When you
install the scripts you'll
see that the SQL Server
Agent jobs run index
maintenance using a call
to sqlcmd.exe in an
MSDOS style step. That's
by design!
Use the examples on the
website. If you scroll to
the botom of the index
maintenance page you'll
fnd all sorts of examples showing
how to get the procedure to do diferent
useful things.
Find out when maintenance fails
Don't forget to make sure that your
maintenance jobs are successfully logging
their progress. Set up Database Mail and
operators so jobs let you know if they fail.
Tell your boss you did a good thing
Finally, write up a quick summary of what you
did, why you chose custom scripts or
maintenance plans, and why. Share it with
your manager and explain that you've set up
automated index maintenance as a proactive
step.
Having your manager know you're taking the
time to follow best practices certainly won't
hurt-- and one of these days, it just might
help you out.
Learning More About Fragmentation
5 Things About Fill Factor - Including what
it's for, what it's NOT for, and why you
shouldn't play with that.
Stop Worrying About Fragmentation -
defragging everything can cause more
problems than it solves.
Our Best Free
SQL Downloads
includes video
tutorial on Olas
script setup.
BrentOzar.com/go/freebies
Brent Ozar Unlimited 2013 Page 33 htp://BrentOzar.com/needs
Time to fnd out if youve been reading, or just scanning.
Backup & Recovery Questions:
1. How many production SQL Servers do
you have?
2. What's the RPO and RTO of your most
important production server?
3. Did your backups take the normal
amount of time last night?
4. When was the last time DBCC
successfully fnished in production?
Security Questions:
1. How many diferent people are
sysadmins in production?
2. Do they each know that they're
sysadmins, and take care to avoid
accidents?
3. How many of your databases hold
personally identifable data like credit
card numbers, social security numbers,
and passwords?
4. If someone gets hold of one of those
database backups, can your company's
data go public?
5. Have you informed your managers of
that risk, or will they blame you?
Monitoring Questions:
1. When a database server runs out of drive
space, who gets emailed?
2. Do at least two diferent people get the
email in case one is on vacation or
unavailable?
3. What actions will you take to fx the
situation?
4. Are those actions documented so that
everyone who gets the email can take
action quickly?
Index Questions:
1. Does every table in production have a
clustered index?
2. For any exceptions (heaps), do you have
a plan to fx them?
3. Which table in production has the most
indexes, and why?
4. Which frequently queried tables in
production have the least indexes, and
why?
5. How are you managing index
fragmentation?
Pop Quiz!
Brent Ozar Unlimited 2013 Page 34 htp://BrentOzar.com/needs
The Right Answers
There's no one right answer for
any of these questions, but
some answers are more wrong
than others. Database administration is a
journey, and not everyone in the company is
going to appreciate the work you're puting
in. You can spend weeks or months trying to
improve your answers on these. No mater
how big your company is and how many
database administrators you have, you're
probably never going to be truly happy with
your answers here.
You're not aiming for perfect.
You're aiming for good enough that your
managers accept the base of your Database
Hierarchy of Needs pyramid, and that you
feel confdent in tackling the higher levels
like performance and future-proofng.
Next week's email is going to start digging
into performance, and we're going to ignore
the lower levels of the pyramid - but that
doesn't mean that part of your journey is
over. Print out this email, cut out the
question list, and scribble in a few thoughts.
Pin it up on your wall, and a few months from
now, when you're feeling overconfdent that
your environment is awesome, check that list
again. Refresh your memory with the links on
the right side of this email.
When an outsider comes in, like a support
engineer or a consultant, they're going to
start at the base of your pyramid frst. Before
they start to help, they have to make sure
your data is backed up and checked for
corruption. They can't go making changes
without having a safety net.
And you shouldn't either.
Remember that as we start talking about
changing server and database setings next.
H
o
w
? Yo
u
re
th
e o
n
e w
h
o
w
ro
te it.
Y
o
u
r
e
n
o
t
t
o
u
c
h
i
n
g
m
y
a
n
n
u
a
l
e
v
a
l
.
I
f
a
i
l
e
d
t
h
e
q
u
i
z
.
Yeah, but I
scored it for
him.
Dont make changes
without a tested safety net.
Brent Ozar Unlimited 2013 Page 35 htp://BrentOzar.com/needs
As SQL Server runs queries, it constantly tracks what it waits on.
Ask SQL Server for these wait statistics, and tuning is easy. Easier, anyway.
What is SQL Server Waiting On?
by Brent Ozar
You probably got into
database administration
by way of development or
systems administration.
You're used to monitoring
stuf from the OUTSIDE
using things like Performance Monitor
counters.
SQL Server has a way, way, way beter tool.
When SQL Server starts running your query,
your query consumes CPU. It sits on a CPU
scheduler, using as much CPU as it can, all
to its greedy self. SQL Server doesn't carve
up a core and say you can run for 3 seconds
until someone else gets to run - oh no. Your
query runs until it's done, burning CPU the
whole time.
Until it has to wait for something.
The instant your query has to wait - like if
SQL Server needs to read data from hard
drives, or wait for someone else to let go of a
lock - then your query steps of the CPU and
goes into a waiting queue. SQL Server
tracks how many milliseconds your query
spends waiting, and what resource it's
waiting on.
The Crappy Way to Check Waits
Run this simple query:
!"#"$% ' ()*+ 9F91=E/;9/:8?B/9B8B9
And you'll get back a list of wait types, plus
how many milliseconds the server has spent
waiting on this wait type. The time is
cumulative, measured over time since the
SQL Server instance was started - or since
someone manually cleared the wait stats.
(Don't do that.)
There's a few problems here: the wait list is
really cryptic, it's flled with irrelevant system
wait types, and it's measured over time.
What you really want is a quick snapshot of
waits over a 30-second period, with the
system wait types fltered out.
The Beter Way to Check Waits
Hit BrentOzar.com/go/waitsnow and get our
free script. This returns 3 result sets - the
frst showing how long the server has been
up, the second showing the waits since they
were last cleared, and the third is a fun one.
The third shows a running sample of waits
over the last 30 seconds.
If your server is busy, you'll see MORE than
30 seconds of waits on the biggest
botlenecks. That's totally normal because
your SQL Server has multiple cores, each of
which may have multiple queries lined up
waiting for something.
If your server isn't busy, your waits will
probably add up to 30 seconds - or much
You might be
surprised at
how litle memory is
used for caching data.
Brent Ozar Unlimited 2013 Page 36 htp://BrentOzar.com/needs
less. This just means the server's mostly
siting around idle. Don't make big
performance tuning decisions based on
small samples like that - aim for sampling
when the server's really busy.
To learn more about wait types and what
they mean, check out our wait types
resources page.
Once you get started with wait stats, you'll
want to capture this data all the time and
trend it. Don't reinvent that wheel: every
modern performance monitoring tool tracks
wait stats already.
Correlating Waits Stats and Perfmon
Once we think we've got a botleneck, we
need to double-check those numbers by
gathering server-level metrics about that
particular botleneck.
In our Performance Monitor tutorial, we
explain how to set up Perfmon, gather the
right metrics, and export them to a
spreadsheet. Depending on the wait stats
you're seeing as a botleneck, here's the
Perfmon counters to collect:
To double-check CXPACKET and
SOS_SCHEDULER_YIELD waits, collect:
System: Processor Queue Length
(for each individual core, not the total)
This wait type indicates challenges with
parallelism. Parallelism isn't a bad thing - it
means SQL Server is breaking out your large
queries into multiple tasks, and spreading
that load across multiple processors. Learn
more about CXPACKET waits.
For PAGEIOLATCH* and WRITELOG waits:
Physical Disk: Avg Sec/Read
Physical Disk: Avg Sec/Write
Physical Disk: Avg Reads/Sec
Physical Disk: Avg Writes/Sec
The top two counters are about response
time - how fast the storage is returning
results. The botom two counters are about
how much work we're giving to the storage.
Much like you, the more work you're asked to
do, the slower you get. The top two are the
SAN person's fault, the botom two are your
fault. You want to make sure the botom two
numbers trend down over time by doing
beter indexing.
You need to fgure
out how to help an
ailing SQL Server.
Our SQL Critical
Care helps.
brentozar.com/go/help
Brent Ozar Unlimited 2013 Page 37 htp://BrentOzar.com/needs
For LCK_* waits, collect:
SQLServer: Locks - Lock Waits/sec
SQL Server: Locks - Avg Wait Time
SQL Server: Access Methods - Table Lock
Escalations/sec
SQL Server: Transactions - Longest Running
Transaction Time
SQL Server: Access Methods - Full Scans/
sec
These counters help you determine how
much locking is going on, and where the
source is. For example, when you're having
locking problems, and the Full Scans/sec
reports a high number, maybe you've got a
lot of table scans going on, and those are
grabbing locks across tables while they
work. Or maybe Longest Running
Transaction Time is reporting a few minutes -
meaning someone is running a really long
transaction, and it's starting to block lots of
other users.
Got Other Waits?
Wait types can be so cryptic - theres so
many of them, and theyre ofen not
documented well. To learn more about wait
types and what they mean, check out our
wait types resources page.
Learning More About Perfmon Counters
No mater what your biggest wait type is, the
idea here is to correlate that wait type with
underlying Perfmon counter measurements
to drill down deeper and fnd the root cause
of the problem. To learn more, here's our
favorite resources:
Perfmon Counters of Interest Poster - from
Quest Sofware, listing the best counters by
type.
Our Perfmon tutorial - explaining how to
collect the data and analyze it.
Jimmy May's Perfmon Workbook - an Excel
spreadsheet with Jimmy's favorite counters
and thresholds.
Watch Brent explain wait stats while hes
dressed up as Richard Simmons. Click here.
Brent Ozar Unlimited 2013 Page 38 htp://BrentOzar.com/needs
by Brent Ozar Unlimited
We love - no, we LOVE - helping
people get relief for data pains,
and weve got lots of options to
help.
FIRST AID: TOTALLY FREE STUFF
We build cool troubleshooting tools and give
them away for free:
sp_Blitz - fast SQL Server health check.
sp_BlitzIndex - identifes indexing
madness dragging down your SQL Server.
Our blog - thousands of articles on
performance tuning, availability, and
career development.
Much more - like our posters, YouTube
videos, and weekly webcasts.
IN-PERSON TRAINING CLASSES:
CHICAGO, PHILADELPHIA, SAN DIEGO
How to Be a Senior DBA - 2 days of
practical classes for beter database
management.
SQL Server Performance Troubleshooting
Learn to make SQL Server apps go faster
in this 3-day course
Our classes are taught by real experts with
hands-on knowledge - specifcally, us. We
share the latest cuting-edge tips and tricks
that weve learned in real-life deployments.
Were available for questions and answers -
its your chance to talk face-to-face and get
personal advice on your tough challenges.
Join us in-person in 2014!
VIDEO COURSES:
$29-$299 FOR IN-DEPTH KNOW-HOW
Introduction to Hadoop, $29 - Get started
in the world of Big Data.