You are on page 1of 38

How to Develop YourDBA

SQL Server DBA Career

Training Plan
3rd Edition, Spring 2015 -

Brent Ozar Unlimited 2015 Page 1

Welcome to Your Training Plan
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 Ozar Unlimited, a
consulting company that makes SQL Server faster and more reliable.

I learned to be a DBA the You may not be able to x everything in your

hard way. The hard, crappy environment during those six months, but at
way. Our SQL Server was in least you'll understand the work involved
trouble, and I was the kind and how to condently get started.
of person who would roll up We've collected thousands of resources
my sleeves and gure out over the years -training videos, scripts, blog
whatever was broken. Next posts about everything
thing you know, I was the fromindexingtocareers - and we're excited
one responsible for it. to share them with you. It's all about making
And boy, did that suck. your journey to Professional Database
Administrator easier than ours was.
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 nd 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 better 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 bottom
of the pyramid up to the top.

Brent Ozar Unlimited 2015 Page 2

If You Have Questions questions. They react fast, too - make sure
to go back and revisit your question every
If you have questions about what you're
10-15 minutes for the rst few hours to see
reading, start by Googling your questions. It
what's happening. Answer their clarication
sounds obvious, but you'd be amazed at how
questions, and include as much detail as you
much good stu there is out there to help.
can. For more instructions, read Getting Help
(I'm not being sarcastic. This is exactly how
with a Slow Query.
we get started whenever we have our own
questions.) If you still can't get the answers you need,
email us at This is a
If you have a really short question and you
real email address manned by the real
expect a really short answer, and youre on
people at Brent Ozar Unlimited. This isn't
Twitter, tweet the question with #SQLhelp in
one of those emails where it says, "Don't hit
your tweet. Lots of community members
respond because nobody cares." Seriously,
monitor this hash tag, so even if they dont
we care, and that's why we put these emails
follow you personally, theyll still see the
together. Just please don't use that as your
tweet and respond. You can read more about
FIRST resort - we're real people with real
#SQLhelp too.
jobs and real families, and there's only so
If you'd like to post a question, try many hours per week that we can spend or answering questions. By using the above
SQLServerCentral's forums. Yes, both of methods rst, you'll be able to leverage the
these require registration, but they're totally whole community's expertise instead of just
worth it. On both of these sites, there's a few of us.
hundreds - sometimes thousands - of people
who are itching to help answer your

ave a e I ght My other

I h
l l n am crime. And database is a ham
son, unior.
NOLOCK sandwich. UE
him J DBA. hints. EE
or E!

Brent Ozar Unlimited 2015 Page 3

Build a Server Inventory
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 x them.

by Brent Ozar That last column gets a little tricky - it

means, if this server dies in a re, what's our
At your company, walk into Plan B? Are we going to restore the
the VP of Sales's oce databases from another server? Will we fail
and ask them how many over to a log shipped copy? Or will we
salespeople they have. update our resume and head out for an early
lunch? As we go farther into the training,
NO, I mean, don't actually
we're going to get much more specic about
DO that, because he's
Plan B.
going to ask you why the sales app is so
slow. But I mean, imagine if you COULD walk There's no wrong answers here - week 1 is
into his oce and ask him that. I bet he about understanding where we're at today,
would have an instant answer. He wouldn't not where we'd like to be. We're never where
wait for a single moment. Or walk into the we'd like to be. (Me personally, I'd like to be
CEO's oce and ask how many employees at a poolside bar right now, but noooo, I'm in
he has. Or ask the CFO how much the annual a hotel room waiting for my girlfriend to
budget is. nish blow drying her hair. If you've ever
wondered why I write so much, you can thank
My point is that when you're in charge, you
her full head of hair.)
need to know exactly what you're in charge
of. If you'd like to get ambitious, add additional
columns for Core Count, CPU Count, and
Make a Spreadsheet Inventory
Memory. The core and CPU counts will get
Let's start by making a spreadsheet. Across you a head start on licensing, although I
the top, make columns for: have to confess that we're not going to cover
licensing as part of our training plan.
SQL Server Version (2012, 2008, 2005)
What We'll Do With This Spreadsheet
Edition (Standard, Enterprise, Developer)
Right now, you probably sleep well at night
Environment (Production, QA,
development, disaster recovery) thinking you know everything that's
happening in these servers. Hoooweee, have
Department (sales, HR, accounting, IT, I got bad news for you. Over the next six
mixed use) months, we're going to progressively add
Business Users Aected (list of people to more and more columns to this spreadsheet
email when the server dies) as we learn more about our environment,
uncover problems, and learn how to solve
Application Names (internal or external them.
product names)
For bonus points, add a column for What
Plan B Scares Me. Write a quick note about the one

Brent Ozar Unlimited 2015 Page 4

thing that scares you We don't get paid for
most about this server. Most DBAs dont plugging these products,
Maybe it's blocking actually have a list of and we're always on the
problems, maybe it's the lookout for similar
failing jobs, maybe it's their servers. inventory-building tools,
code you don't so if you know of a better
understand. Six months one, email it to us at
from now, I bet you'll be proud of how this
column has changed. PSST: Ask About This Before Getting Hired
How to Survey Your Network for Servers When you take a new job as a DBA, the very
Put a row in the spreadsheet for every server rst question you should ask the company is,
you have - whether you're in charge of it or "Do you have a list handy of all the SQL
not. We want to start with a good inventory Servers I'll be managing? I don't have to see
of what we have, and there's two good free the list - I understand if you have security
tools to do it. concerns - but I just want to know if that list
Microsoft Assessment and Planning Toolkit -
it's actually designed for licensing Most of the won't.
compliance, but it works great for building This question serves two purposes: it tells
server inventories. It scans your network YOU if the company has their act together
looking for whatever programs you pick, but when it comes to documentation, and it tells
just conne it to SQL Servers only. THEM that you're the right person to
Quest Discovery Wizard for SQL Server-it's manage their database servers. If they don't
a GUI tool that pings all the servers in your have the list, they're going to want that list
network and tries to gure out if they've got right away. Now's your chance to explain
SQL Server installed. If you're in a small how you would go about gathering that
shop where your account has admin information (armed with the info in this
privileges in the domain, you might nd a lot email.)
more servers than you expected. 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!

Brent Ozar Unlimited 2015 Page 5

Bootcamps and
The Reason We Back Up certication
programs alone wont get
All Those Databases you that rst job not when
(And yes, this means were assuming your
youre competition has
backing them up.)

by Brent Ozar individual tables, don't restore onto the

production server. I don't like touching
The only reason we do production servers more than I have to, and
backups is so we can do let's face it - you're already having a bad
restores. enough day as it is. That's why you're doing a
restore, remember? So let's do our work on a
When I rst started out
dierent server (like dev or QA) and leave
as a SQL Server DBA, I
production as it is. I've also written about
thought things were
restores in my ideal dev, test, and production
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 After we've safely restored the right data
still running, and ... that was the end of it. I onto another server, it's easy to copy that
gured if disaster ever struck, I'd just do a data across to other servers. For simplicity
restore. How hard could it be? and security, you can set up a linked server
on the production box with read-only access
In theory, you test our backup strategy
over to the restore server. Then, from
ahead of time with Kendra's 5 Simple
production, you can run INSERT statements
Questions About Your Backups, and youve
using a SELECT sourced from the linked
memorized the 9 Letters that Get DBAs
server tables.
Fired, along with your companys answers.
However, if you're restoring tables over
In practice, small disasters strike all the
10GB, you'll probably want to do the restores
time. The most common reasons to do
directly on the production server to make the
restores aren't to revive an entire server - it's
data copies faster. Just make sure you're
just to get back a few small tables or an
extremely careful with the scripting and the
individual database. Somebody ran the
database names - we don't want to restore
wrong DELETE statement or dropped a
over the top of your working production
database in production instead of
development, and next thing you know, we're
all scrambling. Let's think through a few This may require adding extra space to the
things ahead of time to make the crisis production server. In one emergency, I freed
easier. up the necessary space by shrinking all of
TempDB's data and log les down to just
Where to Do Restores
1MB. TempDB was on fast drives, perfect for
When you're restoring code (stored a one-time emergency restore, and that
procedures, views, triggers, etc) or

Brent Ozar Unlimited 2015 Page 6

particular server didn't have any other restores them for you automatically, in order.
activity happening due to the outage. We're We'll talk about automating restores in the
not always so lucky, but it helps to think out next training module.
of the box like that. To learn more about backups and restores,
A word of warning: if referential integrity is our favorite getting-started articles are:
involved, like if you're trying to restore tables
Grant Fritchey's SQL Server Backup and
that have relationships to other tables that Restore for the Accidental DBA
you're NOT restoring, then you can be in for
a world of hurt here. We're not going to cover Brent's DBA Nightmare: SQL Down, No
that scenario - it really is dierent in each Plans
case. Jes's 3 Things You Need to Start Doing to
Doing the Restore Your Database Server
Restore the most recent full backup using When I Did My First Restore
the WITH NORECOVERY option - this is I did my rst emergency restore when I was
really important. This leaves the database in working for a photo studio. I'd dropped out of
a restoring state so that you can continue to college, and I took a job running their
apply additional backups to it. If you forget databases. Every morning, I got in bright and
those two key words, your restore has to early to print out the list of labels for the high
start over again from scratch, so please, for school graduates, weddings, babies, and so
the love of all that's holy, double-check that on that were going to be photographed that
option before you start the restore. day. The photographers would pick up their
When I'm restoring code or cong tables that stacks of labels for their lm (FILM!
haven't changed since the last full backups, I remember that?) and head out into the eld.
don't bother restoring One morning, as part of my
any subsequent
dierential backups or
Either the backups are data cleanup process, I ran
the DELETE statement.
transaction log tested, or youre about One minor problem - I'd
backups. The goal is to forgotten to put in the
nish the restore to be tested. WHERE clause, so I
quickly. deleted all of the photo
Next, if dierential shoots, ever.
backups are involved, restore the most Thankfully, I was also the guy in charge of
recent dierential WITH NORECOVERY. backups, so after I freaked out, I put last
Dierential backups are cumulative - you night's tape backups in and started the
only have to restore the most recent one. restores. When the boss came in to pick up
Next, restore all of the transaction log his stack of labels, I was able to calmly
backups after the dierential (or, if you don't explain what had happened and what I was
have dis, all of them after the full backup) - doing to x it.
again, using WITH NORECOVERY. Orville's temper was legendary, but I dodged
Doing all of this with the GUI sucks. The a bullet and kept my job. Since then,
more backups you have, the longer this whenever I handle a database, the very rst
takes, and the more likely you are to run into question I ask is, "Where are the backups,
errors. Instead, what you need is a script and when was the last time they were
that looks at all of the backups in a folder, tested?" Either the backups are tested, or
plucks out the most recent relevant les, and you're about to be tested.

Brent Ozar Unlimited 2015 Page 7

Doing Faster Database Restores
The GUI isnt going to cut it. Were going to have to roll up our sleeves.
As we script out a restore, well learn how we need to design backups.

by Brent Ozar it's the simplest way to get the point across
in a tiny email. Hey, you try teaching tough
If you're doing transaction concepts in a page or two, buddy.)
log backups, forget using the If disaster strikes at 8:15AM and I lose the
GUI. Even if you only have server, I need to restore the full, the most
one transaction log backup recent dierential, and the transaction logs
per hour, it'll take you way that follow the dierential, like this:
too long to click through all the les.
Think about what your backup folder might
look like if we named our backup les by MyDatabase_20130718_0600.dif
database, date, time, and a dierent MyDatabase_20130718_0700.trn
extension per type of backup (BAK for fulls,
DIF for dierentials, and TRN for transaction MyDatabase_20130718_0800.trn
logs): That's a really easy script to write - and
thankfully, MSSQLTips has already done it.
Just change the variables for your database
MyDatabase_20130718_0100.trn name and the path where your restore les
MyDatabase_20130718_0200.trn live, and presto, the script will restore all of
the les for you automatically.
This Script Has Implications For You
You need to back up your databases
MyDatabase_20130718_0500.trn intelligently.You want to put each database
MyDatabase_20130718_0600.dif in its own folder, and you want the le names
to have the database name and the time in
MyDatabase_20130718_0700.trn them. I'm a big fan of that anyway - it makes
MyDatabase_20130718_0800.trn life easier when I want to quickly scan and
In that scenario, I took my full backup at see what backups have been done.
midnight, then hourly It assumes the same database
transaction logs, path when you restore. If you're
with dierential Knowing how youll the kind of DBA who likes to
backups every 6 change drive letters and folders
hours. (This is never
restore aects how all the time, or you've got les
a scenario I'd use in you back up. scattered all over the place, and
the real world, but your production and

Brent Ozar Unlimited 2015 Page 8

development database servers have in a GUI - you'll just open a proven script and
dierent drive letters, then life is going to be hit Execute. Bam!
harder for you. There are other restore Getting Even Fancier
scripts that can adjust data and log le
names at restore time, but they're not quite You can use this technique to build smaller
as elegant when it comes to restoring fulls, copies of your production databases. For
dis, and t-logs. example, at StackOverow, I built an ultra-
compressed backup with even more tricks.
You can do transaction log backups as often On my nightly restore server, after restoring
as you want. If the restore process is fully the production backups, I dropped the non-
automated, why not do transaction log clustered indexes, rebuilt all objects with
backups every minute? It doesn't cost you 100% ll factor, and shrank the data and log
any extra. Rather than incurring extra les down.
overhead, it can actually incur LESS
overhead. If you're only backing up logs once The end result was a database that was over
an hour, your users probably feel that hourly 50% smaller!
load big time when it kicks o. If you do I then backed it up with compression, and
smaller backups more frequently, they'll be left those backups in a folder that the
less likely to notice the impact. developers could access. That made it easier
You can automate your own re drill testing. for developers to quickly grab a copy of
Since this is done with plain old T-SQL, you production data as of last night, copy it to
can set up a SQL Agent job to restore last their workstation as quickly as possible, and
night's backups onto your staging server. I restore it faster with less drive space
like setting up a DBA utility server chock full required.
of really cheap (but really large) SATA Free 30-Minute Videos to Learn More
drives, like 2-4TB drives in a RAID 5. I have a
series of Agent jobs to restore a dierent Log Shipping Part 1: Jesexplainshow log
server's backups every day, and then run shipping is really just doing your restores
DBCC jobs on them. This way I know I'm ahead of time.
getting really good backups. Log Shipping Part 2: when disaster strikes,
Your Homework for This Chapter what youll need to do to fail over to the
secondary server and manage your jobs.
You don't have to go build this whole
infrastructure out - but start laying the Backup Planning Video:Jeremiah explains 3
groundwork by making sure your backup company scenarios and asks you to pick the
les are named in a restore-friendly way. right backup strategy for each.

Then, try out theMSSQLTips restore Our Backup Video Archive: no, its not a
scriptto make sure you can quickly restore a backup of our videos - its our videos about
database from scratch into a development or backups. We talk database mirroring,
staging environment. If you get errors, leave clustering, AlwaysOn Availability Groups,
comments on that blog post describing the and more.
error, and ideally, contribute your script
improvements back in. Now, when disaster
strikes, you won't be clicking around blindly

Brent Ozar Unlimited 2015 Page 9

Security: Knowing Who Has
Access to the Servers & Data
Before we try to lock things down, nd out who has copies of keys.

by Brent Ozar backlash. In one shop, the head developer's

SA rights were removed, and he stormed
Run our free sp_Blitz into the DBA's oce the next morning
stored procedure on one of screaming. Turns out one of his apps
your production servers and automatically created a processing
pay particular attention to database every night, did a bunch of work in
the Security section of the it, and then dropped the database. Nobody
results. It lists the logins
who have been granted the
sysadmin or security admin roles. Think of sysadmin
Don't think of them as logins. logins as people
Think of them as people who can get you who can get you red.
These people can drop databases, drop knew because it was only around for 30-45
tables, change stored procedures, edit data, minutes. The problem could have been
or even change SQL Server conguration avoided by communicating the security
settings like max server memory or maxdop. changes ahead of time, and that's where we
You're probably not getting alerted when any need to start.
of these things change - we just can't aord
to monitor every single thing in SQL Server Take the sp_Blitz output to your manager -
and send alerts on it. At some point, we have just the security portions - and say
to be able to trust certain logins, and that's something like this:
where the sysadmin and security admin roles "Here's the list of people who can do
come in. anything on the production server - delete
Except when we rst get started learning data, drop databases, or change
database administration, it's usually performance settings. If they do, here's the
because we're the only DBA in the shop, and list of applications that will be aected,
the server is a mess. The front door is including accounting and payroll. I don't
unlocked, the key is under the oor mat, and want to take away all of their permissions - I
everybody knows we've got a big screen TV just want to start by giving them full
in the living room. permissions over their database, but not in
any other databases, and not at the server
How to Get Started Locking Things Down level. Can I talk to them about doing that?"
Before you start removing people's SA
rights, be aware that there can be political

Brent Ozar Unlimited 2015 Page 10

Note that we're only going to TALK to them, Database administration isn't all bacon and
not actually do it, because we need to roses. Sometimes it's boring politics and
communicate with them rst. Then, go to the paperwork, and this is one of those weeks.
end users or developers involved and say: In the very rst chapter, we built a
"We're starting to lock down the production spreadsheet inventory of our servers, and
server, but I want to make sure you have all now it's time to ll in a little more details.
the permissions you need. I'm going to make Since we're analyzing security, we need to
you a complete database owner inside your know which applications live on each server,
database so you can do anything you want in and who's in charge of each of those
there, but I'm going to take away your rights applications. You don't have to ll in the
to the other databases (like accounting and specics of who has read or write
payroll), and I'm going to remove your permissions in each database, but we want
permissions to change server-level settings
like how much memory the server can use.
I'm planning on doing it next weekend, and I
want you to have my email and phone
number so that if anything breaks on that Manage lots of
date, you can call me and I can audit what's
happening on the server to see if it's related servers? Learn
to the permissions change." How to Be a
When You Get Resistance
Senior DBA:
When - not if - you get pushback from
developers or users, go to the project
managers or business people who have a
large stake in the database. For example, if
the accounting database is on the server, go
to the CFO and say:
"Here's the list of people who can take down
the accounting system. They have the
permissions to drop the database at any
given time, and there's nothing I can do to
stop it. I'd like to get that changed - can I
schedule a short meeting with you and the
development manager to get everyone on
the same page?"
You want to turn it into a business problem,
not a technology problem, and the CFO will
very much be on your side. She can't aord
to have her entire department go down just
because some developer didn't put a
WHERE clause on a T-SQL statement.
I Know, This Chapter Isn't Fun

Brent Ozar Unlimited 2015 Page 11

to at least know the business purpose and It's made more challenging because we often
the business contact. don't have any real authority. Sometimes the
DBAs report to completely dierent
The business contact is the one who really
managers than the developers - and
helps us get the database locked down
sometimes, it's even dierent companies!
because their job is on the line if this data is
We might be administering a database
lost or unavailable. (Come to think of it,
server that houses a third-party application,
you're someone who can get THEM red!) In
and the vendor's support team demands to
the coming weeks, you'll be working more
have SA access.
with them on reliability and performance,
too, so now is a good time to start eshing Consulting Lines:
out that part of the spreadsheet. After consulting for a while, I wrote a series
Database Administration is Politics of posts with lines you can use as a DBA:
Sure, we like to think we're the police, here to "Sounds like you've got it all under control."
protect and to serve, but most of what we do "What happens if that doesn't work?"
involves sitting in meetings, convincing
people to do what we want, how we want. "Would you mind driving?"
"SQL Server needs a dog."

High denition
video training on
your desktop,
laptop, or even
your iPad.

Brent Ozar Unlimited 2015 Page 12

Risk isnt just a board game.
Top tip: when you play Risk, go for South America. Its only 4 countries
with 2 access points, so its easy to defend. See, security matters.

by Brent Ozar SELECT * FROM

Download the SQL Server WHERE COLUMN_NAME LIKE '%password%'
Compliance Guide. It's an
old 92-page whitepaper OR COLUMN_NAME LIKE '%social%'
circa 2008, but it might just OR COLUMN_NAME LIKE '%credit%'
be the best (and most
timeless) technical document that Microsoft Feel free to change out those keywords for
has ever produced. JC Cannon and Denny other terms that are relevant to your
Lee deserve a big thank-you for a great job. business - elds that are sensitive, and that
would hurt if they got out into the wild. Then
I only need you to read pages 7-13. In those look at the contents of those tables - is the
six pages, you'll understand the dierences data being stored unencrypted? Who has
between risk management, governance, and access to it?
compliance. Risk management means
knowing what risks the company is taking If we're storing unencrypted passwords in
with the data, governance means the actions the database, for example, then every
taken to address the risks, and compliance database backup we've ever done is
means someone is double-checking that dangerous. If anybody gets access to any
we're actually doing the governance stu we backup le, like our osite tape backups,
say we're doing. then we could be on the front page of the
news tomorrow.
Your job as a DBA involves all three, but
when you're just getting started with When you nd it, email the developers and
compliance, focus on risk management. We the business contact for that database.
need to get a quick idea of the dierent ways Explain that the data is not encrypted, and
we could lose data, or that supposedly use examples from the SQL Server
secure data could get into the wrong hands. Compliance Guide to show how this is a risk
(Yes, your developers are probably the for the business.
wrong hands, but that's a dierent story.) Compliance: Knowing Who Accesses What
Homework: Look for Risky Business It's unusual for me to assign homework rst
Odds are, nobody in the company has an in the email, and then go on to talk about
inventory of the data we're storing in other things, but I want you to be aware of
ridiculously unsafe ways. Here's a quick way other things that companies usually do
to check your own database looking for around their secure data.
dangerous elds:

Brent Ozar Unlimited 2015 Page 13

Often management will say, "We need to cheap, but seriously good security is
audit everyone who changes or accesses seriously expensive.
secure data." Technically, SQL Server has Big Companies Love Security
features that can accomplish this goal -
things like SQL Server Auditing. In practice, The bigger the company, the more interested
though, this is a problem for hard-core they get in security.
security teams In small shops
because the very without a dedicated
same DBA who Know whether your security
manages the company just wants department,
servers also has
to check a box on an auditors everybody is often a
permissions to domain
change the SQL
Server audits. A
form, or really be secure. administrator, and
everybody knows
greedy DBA could where all the
easily disable passwords are stored.
auditing, get the necessary data, and then
enable it again. In big shops with a dedicated security team,
the security team usually makes requests to
With seriously ambitious work, you can lock disable the DBA's sysadmin access. They
auditing down so that can't happen, but ... want the DBA to only have enough
it's really, really hard. permissions to do their job, but not enough
As a result, most seriously secure permissions to see the database contents.
companies end up with a completely (After all, the DBA shouldn't see credit card
dierent auditing solution that lives outside data, health data, etc.)
of the DBA's realm. The security team buys This isn't a realistic request in SQL Server
third party hardware appliances like IBM 2012 and prior, but SQL Server 2014 brings
Guardium or Imperva that act like a network new server roles that will make this request
rewall between everyone and the SQL easier. For more info, check out the security
Server. The appliance logs everything that enhancements section of What's New in
happens in the SQL Server, and then the 2014.
security team can review those logs without
you (the DBA) knowing about it. Learning More About Security

These solutions are typically six gures and Check out Denny Cherry's book Securing
up. They're expensive because they have to SQL Server (paperback - Kindle). Denny
be absolutely bulletproof - if they fail, you manages to bring a very dry topic to life.
can't have the access to the database Every shop with PCI, HIPAA, SOX, or other
suddenly stop. Plus, you'll need them in compliance needs should denitely own a
place for not just your production copy of this book. It's a tougher sell for small
environment, but also your disaster recovery shops with only one full time DBA, though,
environment. because a lot of the security features
If you just want to check a box and make the discussed take time to digest and
auditors think you're secure, that's easy and implement.

Brent Ozar Unlimited 2015 Page 14

Its Time to Level Up.
In Ozars Hierarchy of Database Needs, weve touched on the bottom two
layers, backups and security. Before we move up a level, lets pause.

by Brent Ozar
Weve covered
backups and security,
Well, we havent.
We could go on for
hundreds of pages about those two
levels alone. We could ll entire ebooks,
perhaps even libraries, with cool
concepts like how the dierential
bitmap works, how to secure stored
procedures with certicates, how to
congure SQL Server when using
VMware-based backups for storage
replication, how to recover from a
corrupted nonclustered index, yadda
yadda yadda. Trivial Pursuit category youre hot on this
Our point of this ebook is a fast start. week. We just cant cover it all in one ebook.

Your journey in SQL Server database When you nd an area that you want to dig
administration is never really done. Youre deeper into, check out:
going to be learning for the rest of your life, Our video training courses - from our $29
and even when you think youre done, youre 90-minute courses all the way up to our 5-6
only mistaken. Heck, Im a Microsoft hour monsters at $299, we can go much
Certied Master, and I still learn things in deeper. Some even include quizzes!
almost every presentation I attend. (Except
Our in-person classes - we have 2-3 day
Jeremiahs, because Im not smart enough
courses for developers and DBAs who need
to digest that stu.)
to make SQL Server faster and more
So were going to move up a level to reliable.
capacity, but please dont email us with cries
Our SQL Critical Care - we work together
of complaints about how you wish we would
with you over WebEx or in person to
have gone deeper into clustering setups,
diagnose your SQL Servers pain points and
replication internals, or whatever other
train you on the xes.

Brent Ozar Unlimited 2015 Page 15

How to Monitor Drive Space
Just when you thought it was safe to let your guard down,
we throw a trick chapter at you.

by Brent Ozar Why You Shouldn't Build a Monitoring Tool

If you want to be a professional developer,
Dont. you should build things. You should learn
Its not your job. what exceptions could happen, how to trap
them, and how to fail elegantly. It's hard
No, seriously, hang on a
work, but if you get good at it - really good -
second. I'm not saying that
you can build amazing things and make a
you should ever say things like "It's not my
killer living for yourself.
job," but I do want you to understand when
you should avoid reinventing the wheel. Your But if you want to be a professional DBA,
Windows administrators should be you should leave the developing to the
monitoring every Windows server you have, developers.
making sure it doesn't run out of drive space. I'm not saying you shouldn't learn the
This isn't some wild, unique need that only dynamic management views (DMVs), how to
SQL Server has - it's everywhere in your data dig through them to nd the evidence you
center. need, and how to turn that data into actions.
(You might actually even BE a Windows Hopefully, I'm going to teach you how to do a
admin, just here because you need to learn lot of that over the course of the next six
about working with SQL Server. It might months.Take another look at the Hierarchy
actually BE your job to monitor this stu. of Database Needs again, and think for a
That's cool - that's how I got started too.) second about all the things we're going to be
learning over the next six months. Just in the
In our journey from the base of our Hierarchy
last ve weeks, I've had you build an
of Database Needsto the top, we do indeed
inventory of your servers, automate your
need to talk about capacity - but I don't want
database restores, start locking down
to monitor drive space from inside SQL
security, and enumerate your database risks.
Server, and I don't want to buy SQL-specic
The next few months are chock full of things
tools in order to pull it o. Yes, you can
that will help you become a hero in the eyes
technically use commands like
of your users and your developers.
xp_xeddrives to list all of the SQL Server's
drive letters and how much free space they Building a crappy monitoring tool in your
have, but that doesn't work everywhere. For spare time will not give you that same level
example, you might have mount points or of respect. (And yes, if you've only got your
databases on UNC paths, neither of which spare time at work, your monitoring tool is
will show up in xp_xeddrives. So leave the going to be crappy. If you've got so much
drive space monitoring to the pros. time that you can build a great monitoring

Brent Ozar Unlimited 2015 Page 16

tool, you need to focus on providing results the up/down alerting for services, drive
to the business fast, because you're about to capacity monitoring, etc. If so, get them to
be laid o as pure overhead.) start monitoring your servers. I'm being
selsh here - my real reason is that I want to
How to Pick a Monitoring Tool
save my monitoring budget for tools in the
There's basically three kinds of monitoring Performance Diagnostics category.
tools out there for SQL Server. Surprisingly, all of these tools are around the
Hardware Alerting - when you buy a server same price - around $1,000-$2,000 USD
from big companies like Dell, HP, or IBM, per monitored instance.
they come with built-in management tools. Next, its time to evaluate them. Dont just
These tools are surprisingly powerful - for install a bunch of random tools in production
example, check out our post on How to Use and see what they nd. Instead, no matter
HP System Management Homepage. which category of tool you're buying, make a
Up/down Alerting - these tools make sure list of the last 4-5 things that have caused
the SQL Server service is running, and that it your phone to ring after hours. Here's some
has all the basic needs taken care of. If the of my personal favorites:
server runs out of drive space, if the service Deadlocks
stops, if the event log starts lling up with
errors, or zombies attack, these tools will let Out-of-control query running for hours
you know. The most popular software in this Long-running job, like slow backup
category is Quest Spotlight, Idera SQL
Queries that desperately need an index
Diagnostic Manager, and Red Gate SQL
Monitor. SQL Server not accepting connections
Performance Diagnostics - these tools try to Figure out how to recreate those same
derive insight from SQL Server's dynamic symptoms in your development
management views (DMVs) to help environment, and then get a free trial of a
performance tuners wring the most couple of the tools I mentioned. (All of them
throughput out of their hardware. The most provide free 10-14 day trials.) Reproduce the
popular problem, and watch how
software here the monitoring software
is SQL Sentry
If youre building a reacts. Does it lead you
Performance monitoring tool in your to the root cause
Advisor, quickly, or does it just
Quest spare time at work, its either have a bunch of ashing
Foglight going to be crappy, or youre red lights on the
Performance console? The best ones
Analysis, and about to be laid o because you will save you time by
to some getting you right to the
have too much spare time. solution.
extent, the
tools in the After you've done your
above evaluation and picked a
category too. favorite, get a quote from them - and get a
If I was you, I'd start by asking the Windows quote from the other vendors as well. Share
team if they've got any software that handles the quotes with the competing salespeople.

Brent Ozar Unlimited 2015 Page 17

You'd be surprised how willing they are to Bobcats per 100 Orders and
negotiate, and you can use the cheapest Other Spurious Metrics
quote to get the tool you really want. by Jeremiah Peschka
Your Homework Did you know that you can
I don't expect you to magically get budget ship a bobcat 1/30th of the
approval for a tool this week, but I want you time and still maintain 97%
to start making a list of day to day problems positive feedback on ebay?
as they strike. If you waste four hours What other statistical lies are lurking out
troubleshooting a deadlock problem, make a there for you to nd in Perfmon? Cache Hit
note of the date, the time required, and a few Ratio, Disk Queue Length, Page Life
words about the root cause. In a month or Expectancy, Page Splits, and User
two, you'll be able to present this list to your Connections can be bogus. Learn why.
boss as proof of why the right monitoring
tool could save you money. Down the road,
I'll also link you to a video showing you how Monitoring SSD
to get the budget approval for that tool. Performance
I'm not selling you vendor tools, by the way. I by Jeremiah Peschka
often link to vendor tools, but I don't receive
any compensation. I'm just a huge believer What if we could watch SSD
in using the right tool to look like a hero fast wear in real time? Many
- instead of building your own hammer every vendors oer SMART status
time you change jobs. codes to return detailed
information about the status of the drive.
How This Philosophy Cost Me a Job Rotational drives can tell you how hot the
I actually failed a DBA job interview once for drive is, provide bad sector counts, and a
giving the same answer in my email here. The host of other information about drive health.
lead DBA asked me, "If you were going to Read more in this post.
monitor your servers, how would you do it?" I
said I'd buy a tool o the shelf, and he was
furious. He wanted me to use an open source
monitoring framework.
Before you build
Today, several years down the road, now that
I'm a Microsoft Certied Master, my answer or buy, check out
is still the same. I don't reinvent the wheel,
and neither should you. Focus on things that the best free
provide the business tremendous value, and SQL Server
your career will take care of itself.
If your business would nd tremendous downloads:
value in a killer monitoring system, then
become a developer and give it to them, or
consider contributing to the huge number of
open source monitoring products. Don't
build a new one from scratch, and denitely
know which metrics to monitor.

Brent Ozar Unlimited 2015 Page 18

Manage Space Inside Databases
While I dont want you reinventing the Is the C Drive Full? wheel,
I very much want you to know whats going on inside each database.

by Brent Ozar use some parts of the page to store

metadata, and we'll talk about that later in
Inside each of your the training.)
database data les Brent Ozar Unlimited is a small company, so
(MDFs), SQL Server stores we can keep all of our employees on a single
your data in 8KB pages. 8KB page. As we insert, update, and delete
That's kilobytes - not employees, SQL Server fetches that 8KB
megabytes, not gigabytes, page o disk, brings it into memory, makes
but just mere kilobytes. the necessary changes, and then writes that
Say that we create a table: data page back to disk. The 8KB page itself
is the smallest unit that SQL Server will
CREATE TABLE dbo.Employees
cache - it doesn't cache individual rows/
(EmployeeID INT IDENTITY(1,1), records - and each page belongs exclusively
EmployeeName VARCHAR(200)) to just one object.

First o, yes, I understand, I shouldn't do A Word About Objects

EmployeeName in one eld - I should have You'll notice that I avoid using the word
FirstName, LastName, MiddleName, Sux, "table". Tables are cool, but as we start to dig
Prex, yadda yadda yadda, but I'm trying to into what SQL Server's doing under the
keep this email short. Now see what you did? hood, we want to start thinking about these
It's long again. Doggone it, it's hard teaching three object types:
this stu in an email.
Heap - a table with no clustered index. In my
Anyhoo, in this table, each record takes up dbo.Employees table, I didn't specify in what
just a little bit of space. EmployeeID is an order SQL Server should store my data, so
INTeger, which takes 4 bytes. It's the same 4 it's just going to slap the data down on my
bytes whether that number is 1 or 8KB page in any old order.
1,000,000.EmployeeName is a
Clustered Index - what we normally think of
VARCHAR(200), which means we can store
as a table. If I'd have created my table like
up to 200 characters in here, and each
character takes a byte. If we insert 'BRENT
OZAR', that's 10 characters (and boy, am I a CREATE TABLE dbo.Employees
character), so we need 10 bytes to store it. (EmployeeID INT IDENTITY(1,1) PRIMARY
If all of our employees average about 10 KEY CLUSTERED,
characters in their name, that means we EmployeeName VARCHAR(200))
could t about 500-600 records per 8KB
database page. (In reality, there's some
overhead because SQL Server also needs to

Brent Ozar Unlimited 2015 Page 19

Then SQL Server would store my data in of your database objects. Scroll down to the
order of EmployeeID. That way, when I examples link in that Books Online page,
search for EmployeeID #42, it can go and you'll get queries to examine a single
directly to that number without scanning table - I'd strongly recommend starting with
through all of my employees. The clustered a small table, because some of the
index is sorted in the order of the parameters for this DMF will cause SQL
EmployeeID eld, but it's actually the full Server to actually look at every page in the
copy of our table, including all of our elds - object. That means if all of the pages for that
in this case, just EmployeeName. object aren't cached in memory, SQL Server
will go pull those pages o disk, and that can
Nonclustered index - If I tell SQL Server to:
slow down your running SQL Server.
This DMF also includes average record size
ON dbo.Employees(EmployeeName) and max record size. This makes for fun
Then SQL Server will create a second copy spelunking: how big is each record, really?
of my table sorted by EmployeeName. This Just because we make everything a
copy of my table will VARCHAR(8000)
only include the doesn't mean we're
elds specied in my
Armed with this actually storing 8,000
index knowledge of characters in each eld.
(EmployeeName), Now, don't go changing
plus whatever elds pages, youll make better your database structure
just yet - you can easily
it needs to get back data modeling decisions, break applications when
to the clustered index
(in this case, my too. (MAX) aint free. datatypes change. Let's
clustering key, leave that for later.
EmployeeID). You can get similar metadata much faster by
All three of these objects - heaps, clustered using sp_spaceused, but it doesn't get the
indexes, and nonclustered indexes - will be cool info about average record size, and I
stored on separate sets of pages. We won't wanted to encourage you to go spelunking
have the clustered index and nonclustered here.
index for the same table on the same page - Learning More About Pages
they're split. That's why when we're doing
In my 90-minute session How to Think Like
space analysis, we have to think in terms of
the Engine, I explain pages, indexes, joins,
indexes, not tables. To learn more index
SARGability, and more. I use real database
basics, read Jes Borland's SQL Server Index
pages from the
database for demos, and you actually get
Pages & Rows on Disk PDFs to print out and follow along as we go.
The sys.dm_db_index_physical stats Your Homework
Dynamic Management Function (DMF)
Let's start thinking about what objects are
returns the number of rows and pages stored
taking up space in our databases. Check out
in each database object. It takes parameters
our free sp_BlitzIndex stored procedure
for database ID and object ID, or you can
that analyzes the indexes in your database
pass in NULLs to get information across all

Brent Ozar Unlimited 2015 Page 20

from a psychologist's point of view. Is your
database a hoarder, clutching on to a bunch
of nonclustered indexes that aren't getting
used, and are just taking up space? The
details columns in the results will show how
big each index is, and whether it's getting
used or not.
This is important because the more indexes
you have:
The longer your backups take
The longer index rebuilds take
Weve got tons of
The more memory you need to cache
everything blog posts and
And most importantly, the slower your videos about our
inserts/updates/deletes go, because SQL
Server has to maintain more copies of your favorite index
tips, tricks, and
As we start to move up the Hierarchy of
Needs from capacity into performance, you'll free tools:
start to see how these foundational items
are so important.
Want to See What a Page Looks Like?
Check out the DBCC PAGE command. You
pass in a database name, le number, and
page number, and SQL Server will return the
nearly-raw contents of that page along with
diagnostic information. It's a fun way to get a
peek under the hood.
This topic is a good example of how knowing
the basics of database internals can come in
handy when you step back and think about
database server performance. You don't
have to use DBCC PAGE as part of your job
as a DBA, but just knowing what an 8KB
page is helps you understand the output of
various Dynamic Management Views
(DMVs) when they report back units in

Brent Ozar Unlimited 2015 Page 21

What Pages are In Memory?
If everythings stored on an 8KB page, and each page has only one object on
it, then we can gure out what objects (tables/indexes) are getting cached.

by Brent Ozar SELECT CAST(COUNT(*) * 8 / 1024.0 AS

NUMERIC(10, 2)) AS CachedDataMB ,
We like to think SQL CASE database_id WHEN 32767 THEN
Server is using all of our 'ResourceDb' ELSE DB_NAME(database_id)
memory to cache data, but END AS DatabaseName
that's just part of it. SQL
Server uses memory for FROM sys.dm_os_buffer_descriptors
lots of things: GROUP BY DB_NAME(database_id) ,
Caching database objects database_id

Sorting data for your query results ORDER BY 1 DESC

Caching execution plans

Performing system tasks Compare the size of each database versus
how much is being cached. Often in the eld,
Often, we're surprised by how little data is I'll see 100GB databases that just have
being cached for each database. 8-12GB of data cached in memory. That
Last section, we looked at the 8KB pages in might be completely okay - if you only
our database. Those pages are the same regularly query just that amount of data - but
whether they're on disk or in memory - they what if we constantly need all 100GB, and
include the database ID and the object ID, so we're constantly pulling it from disk?
if we looked at all of the pages in memory, we This Leads to Cool Questions
could gure out which tables are being
cached in memory right now. The below This DMV query leads to so many cool
query gives us the magic answers, but be performance tuning questions! I get so
aware that the more memory you have, the excited by these concepts.
longer this will take. It won't block other How fast are the cached pages changing?
users, but it could take a minute or two if From the moment we read an 8KB page o
you've got >64GB memory, several minutes disk, how long does it stay in memory before
if you've got a terabyte or more: we have to ush it out of the cache to make
room for something else we're reading o
disk? This concept is Page Life Expectancy,
You might be a Perfmon counter that measures in seconds
how long things stay in RAM. The longer, the
surprised at better, as I explain in my Perfmon tutorial.
how little memory is Do the results change based on time of day?
used for caching data. This is a one-time snapshot of what's in
memory at the moment, but it can change in

Brent Ozar Unlimited 2015 Page 22

a heartbeat. If you have automated querying most of those elds, then we're
processes that run a bunch of reports in a caching a whole bunch of data we don't
single database at 2AM, then the memory need. Remember, SQL Server is caching at
picture will look completely dierent then. the page level, not at the eld level. A
nonclustered index is a narrower copy of the
Are we caching low-value data? If you mix
table with just the elds/columns we want.
vendor apps and in-house-written apps on
The less elds, the more data we can pack in
the server, you'll often nd that the worst-
per page. The more we can pack in, the more
written application will use the most memory.
data we're caching, and the less we need to
Thing is, that might not be the most
hit disk.
important application. Unfortunately, we
don't have a way of capping how much When I tune indexes on a server I've never
memory gets used by each database. This is seen before, sys.dm_os_buer_descriptors
why most shops prefer to run vendor is one of the rst places I look. The database
applications on separate virtual machines or with the most stu cached here is likely to be
servers - this way, they don't hog all the the one that needs the most index help.
memory on a SQL Server that needs to serve Its Probably Not a SAN Problem.
other applications.
When I was a junior DBA, I focused a lot on
Do we have enough memory? If you're the storage. I kept complaining to my SAN
running SQL Server 2008/R2/12 Standard administrators because my storage didn't
Edition, you're limited to just 64GB of respond fast enough - my drives were taking
physical RAM. If you're running SQL Server 50ms, 100ms, or even 200ms in order to
on bare metal (not a VM) and you've got any deliver data for my queries.
less than 64GB, go buy enough to get to
64GB. It's the safest, easiest performance The SAN admin kept saying, "It's okay. The
tuning change you can make. If you're in a SAN has a cache." Thing is, the size of the
VM or running Enterprise Edition, the SAN's cache is typically 32GB-128GB -
memory question gets a lot tougher. To learn which at rst sounds like a lot - but divide it
more, read A Sysadmin's Guide to SQL between all of the servers connected to the
Server Memory. SAN. Often, we nd that an individual SQL
Server might get only a couple of gigabytes
Are we using memory for anything other of SAN cache. That's way less than what the
than SQL Server? If we've got Integration SQL Server has in memory. What are the
Services, Analysis Services, Reporting odds that, when we need data for a query, it's
Services, or any other applications installed not going to be in SQL Server's 64GB of
on our server, these are robbing us of memory, but it IS going to be in the SAN's
precious memory that we might need to miserly 2GB of cache? Not gonna happen.
cache data. Don't remote desktop into your
SQL Server and run SSMS, either - it's a SAN caching is still great for writes,
memory pig. Put your management tools on especially for the transaction log, but don't
a virtual machine in the data center, and count on it helping for SELECT speeds.
remote desktop into that instead. To learn more, check out our training course
Can we reduce memory needs with indexes? on Storage, Virtualization, and Hardware for
If we've got a really wide table (lots of elds) DBAs.
or a really wide index, and we're not actively

Brent Ozar Unlimited 2015 Page 23

Indexes: What Goes First?
In which we pretend the phone company, but instead of giving everybody
unlimited calling, we just organize the data.

by Brent Ozar That means every time a record is inserted or

updated in this table, SQL Server has to
For our evil training check to make sure nobody exists with that
purposes, let's say we work same phone number. As of the year 2000,
for the phone company, and there were about 360,000 people in Miami.
we need a database table Throw in businesses, and let's say our table
with phone numbers. We has 500,000 records in it.
need to track: That means by default, every time we insert
Phone number (required) one eensy little record, SQL Server has to
read half a million records just to make sure
Billing contact last name (required) nobody else has the same phone number!
Billing contact rst name (required) Every 1 write = 500,000 reads. Well, that
won't work, will it? So let's organize our table
Business name (optional)
in the order of phone number. That way,
Business category (restaurant, dog when SQL Server inserts or updates
groomer, auto dealer, etc) records, it can quickly jump to the exact area
Address 1 of that phone number and
determine whether or not there's
Address 2 No, Brents any existing duplicates. This is
City home number called setting up a primary
CLUSTERED key. It's called
State is not in this chapter. clustered because - well, I have no
Zip idea why it's called clustered, but
the bottom line is that if you could look at the
Service start date
actual hard drive the data was stored on, it
(Sometimes a person or a business will have would be stored in order of phone number.
multiple phone numbers, but for the sake of
this training, let's keep it a simple at table.) Now we have the table organized by phone
We will never have two records in here with number, and if we want to nd people by
the same phone number. We have to tell our phone number, it'll be very fast. While our
database about that by making the phone computer systems will usually need to grab
number our primary key. people's data by phone number, our
customers and end users often need to get
When we make the phone number the numbers by other ways. That's where non-
primary key, we're telling SQL Server that clustered indexes come in.
there can be no duplicate phone numbers.

Brent Ozar Unlimited 2015 Page 24

The White Pages: A Non-Clustered Index Whenever you do a table scan and you don't
specify how many records you need, it
Our customers constantly need to nd
absolutely, positively has to scan all
people's phone numbers by their name. They
500,000 records no matter what.
don't know the phone number, but they know
the last name and rst name. We would If the database has an index by last name
create an index called the White Pages: and rst name, though, the database server
can quickly jump to Smith, John and start
Billing contact last name
reading. The instant it hits Smith,
Billing contact rst name Johnathan, it knows it can stop, because
Phone number there's no more John Smiths.

That index would save people a ton of time. Covering Fields:

Think about how you use the white pages: Helping Indexes Help You

1. You scan through pages looking at just But that's not always enough. Sometimes we
the letters at the top until you get close have more than one John Smith, and the
customer needs to know which John Smith
2. When you get close, you open up the full to call. After all, if your name was John
book and jump to the right letters Smith, and the phone book didn't include
3. You can quickly nd the right single one your address, you'd get pretty tired of
record answering the phone and saying, "No, you
want the John Smith on Red Road. He's
Now think about how you would do it without 305-838-3333." So we would add the
the White Pages. Think if you only had a Address 1 eld in there too.
book with 500,000 records in it, organized
by phone number. You would have to scan Billing contact last name
through all 500,000 records and check the Billing contact rst name
last name and rst name elds. The
database works the same way, except it's Address 1
even worse! If a developer wrote a SQL Phone number
query looking for the phone number, it would
Do we absolutely need the address in our
look like this:
index for every query? No, but we include it
for convenience because when we DO need
SELECT PhoneNumber FROM Directory WHERE it, we need it bad. And if we DON'T need it, it
LastName = 'Smith' AND FirstName = doesn't really hurt us much.
'John' This is called a covering index because it
covers other elds that are useful. Adding
the address eld to our index does make it
That doesn't say select the top one - it says larger. A phone book without addresses
select ALL of them. When you, as a human would be a little thinner, and we could pack
being, go through that list of 500,000 phone more on a page. We probably don't want to
numbers, you would stop when you thought include the Address 2 eld, because the
you found the right John Smith. The Address 1 eld is enough to get what we
database server can't do that - if it nds need. The database administrator has to
John Smith at row #15, it doesn't matter, make judgement calls as to which elds to
because there might be a few John Smiths. use on a covering index, and which ones to

Brent Ozar Unlimited 2015 Page 25

skip. When building covering indexes, the name eld is more selective than the rst
covering elds go at the end of the index. name eld because it has more unique
Obviously, this index would suck:
For lookup tables - meaning, when users
Billing contact last name
need to look up a specic record - when
Address 1 you've narrowed down the list of elds that
Billing contact rst name you're going to use in an index, generally you
put the most selective eld rst.
Phone number
Indexes should almost never be set up with a
We don't want all of the Smiths ordered by non-selective eld rst, like Gender. Imagine
their address, and then a jumbled mess of a phone book organized by Gender, Last
rst names. That wouldn't be as fast and Name, First Name: it would only be useful
easy to use. That's why the covering elds go when you wanted a complete list of all
at the end, and the names go rst - because women in Miami. Not that that's a bad thing -
we use those. but no matter how much of a suave guy you
Selectivity: Why the Last Name Goes First think you are, you don't really need ALL of
the women in Miami. This is why non-
If you wanted to search for Brent Ozar in the selective indexes aren't all that useful on
phone book, you look in the O's for Ozar rst, lookup tables.
and then you'll nd Ozar, Brent. This is more
ecient than organizing the phone book by This rule is really important for lookup
rst name then last name because there are tables, but what if you aren't trying to look up
more unique last names than rst names. a single specic record? What if you're
There are probably more Brents in Miami interested in a range of records? Well, let's
than Ozars. This is called selectivity. The last look at...

w an t an I prefer I prefer the o ar
x on ordering fr
om ordering trucks se e
inde rant s. pe
resta es. food truck of food. op

Brent Ozar Unlimited 2015 Page 26

The Yellow Pages: Another Index SQLServerCentral's Index Stairway - a 15-
part series by David Durant that goes all the
When we need to nd a dog groomer, we
way to indexing internals.
don't want to go shuing through the white
pages looking for anything that sounds like a Expert Performance Indexing by Jason
dog groomer. We want a list of organized by Strate and Ted Krueger (book) - covers how
business category: indexes work and how to pick the right one.
Also available on Kindle.
Business Category
And nally, if youd like a video training
Business Name
session with our very own Microsoft Certied
Address 1 Master Kendra Little, weve got a 6-hour set
Phone Number of videos complete with quizzes and demos:

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 How to Tune
records, not just a single one. Indexes and
Notice that we didn't put the most selective
eld rst in the index. The eld "Business Speed Up SQL:
Name" is more selective than "Business
Category". But we put Business Category 6-Hour Training
rst because we need to work with a range of
records. When you're building indexes, you
not only need to know what elds 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 elds 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 getting 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

Brent Ozar Unlimited 2015 Page 27

More Kinds of Indexes
Covering, ltered, full text, XML, heaps, 3x5 index cards, you name it.

by Brent Ozar order to get the results I need. This means

faster queries, plus less contention - it
Last time, we talked about leaves the clustered index (and the other
the two most common types nonclustered indexes) free for other queries
of indexes - clustered and to use.
nonclustered. In this week's Covering indexes are most eective when
episode, we're going to you have very frequent queries that
spend just a paragraph or two covering the constantly read data, and they're causing
other types of indexes, starting with blocking problems or heavy IO.
covering indexes. HA! Get it? We're covering
covering indexes. Oh, I kill me. Filtered Indexes
Covering Indexes Say we're a big huge online store named
after a river, and we constantly add records
Covering indexes aren't actually a dierent to our dbo.Orders table as customers place
kind of index - it's a term that is used in orders. We need to query orders that haven't
combination with a query and an index. If I been processed yet, like this:
have this query:

SELECT OrderNumber FROM dbo.Orders

SELECT FirstName, LastName, PhoneNumber WHERE OrderProcessed = 0
FROM dbo.People WHERE LastName = 'Ozar'

The vast majority of the Orders records will

And if I have this index: have OrderProcessed = 1, because we keep
all of our order history in this table. If we
create an index on the OrdersProcessed
CREATE INDEX IX_LastName_Includes ON
eld, it's going to have a lot of data - but
dbo.People (LastName) INCLUDE
we're never going to run queries looking for
(FirstName, PhoneNumber)
OrderProcessed = 1. Starting with SQL
Server 2008, we can create an index with a
Then the index covers all of the elds I need WHERE clause:
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
dbo.Orders (OrderNumber) WHERE
PhoneNumber elds. SQL Server doesn't
OrderProcessed = 0
have to go back to the clustered index in

Brent Ozar Unlimited 2015 Page 28

really good. This link is for SQL 2014/2012,
but keep in mind that there were changes
Now, we have an index on just a few records -
from 2008 to 2012.
a small subset of our table!
Full Text Indexes Understanding Full Text Indexing by Robert
Sheldon - covers SQL Server 2008, plus
Let's say we have a table called the dierences between 2005 and 2008
dbo.MoviePlots, and it had a Description (which were huge).
eld where we put each movie's plots. We
XML Indexes
know we liked this one movie where a guy
was afraid of snakes, but we couldn't You can store XML data natively in SQL
remember the exact table. We could write a Server tables using XML elds. SQL Server
query that says: is aware of the contents - in the sense that
SQL Server knows the content is valid XML -
but it's not necessarily smart about
SELECT * FROM dbo.MoviePlots WHERE searching the data.
Description LIKE '%snakes%'
When you run XML queries, SQL Server has
to roll up its sleeves and parse the XML data.
But that wouldn't be very Every. Single. Time.
That's CPU-intensive,
ecient. SQL Server Why did it have and a recipe for slow
would have to look at
every movie's to be snakes? performance.Instead,
description, and scroll we can create pre-
through all of the words processed versions of
one character at a time looking for snakes. the XML so we can rapidly jump to specic
Even if we index the Description eld, we're nodes or values.
still going to have to scan every row. Primary and Secondary XML Indexes -
Full text indexes break up a text eld like Books Online.
Description into each word, and then stores Selective XML Indexes - instead of wasting
the list of words in a separate index. They're a ton of space indexing values we never
blazing fast if you need to look for specic use, SQL Server 2012 can create the
words - but only as long as you rewrite your equivalent of ltered indexes on XML.
query to use the full text search commands
like this:
Heaps are tables with no clustered index
whatsoever. They're tables stored in random
SELECT * FROM dbo.MoviePlots WHERE order, data slapped in any old place that ts.
CONTAINS(Description, 'snakes') When you want to query a heap, SQL Server
scans the whole freakin' thing. Every. Single.
You can even do fun stu like look for
synonyms or variations on a word. To learn Sounds bad, right? Most of the time, it is -
more about full text indexing, check out: except for a couple of very niche uses. If you
have a log-only table, meaning theres inserts
Books Online on Full Text Indexing - but never any updates, deletes, or selects,
seriously, stop laughing, the manual's then a heap can be faster. If you have a

Brent Ozar Unlimited 2015 Page 29

staging table in a data warehouse, where you How to Master Index
shove a lot of data in quickly and then need Tuning in One Step
to get it all out at once, then delete all of it, a
heap can be faster. Just make sure you test by Kendra Little
it to make sure it's actually faster under your
application's needs. Im going to tell you a secret.
Index tuning is complicated, but its
Picking the Right Indexes for Your Apps
something you can become great at. You just
SQL Server's Dynamic Management Views need to practice it regularly.
(DMVs) surface a lot of useful
Heres that one step: stop thinking index
instrumentation like which indexes are
tuning is a problem for Future You.
getting used, which ones aren't getting used,
and which ones SQL Server wishes it would Thats it. Really. If you read this headline and
have had. Unfortunately, they don't give you didnt skip the post, your job probably
a holistic overall picture - they're just raw involves helping an application using SQL
data that has to be manually combined and Server go faster. If thats the case, index
interpreted. We'll talk about that in coming tuning is a problem for Present You. Its a
lessons, but you need to have this ground problem for you now, its a problem for you
knowledge of index options rst. next month, and its still a problem the month
after that.
Index tuning isnt something you do once a
Dude, Who Stole My year. Its something that you need to do
Missing Index iteratively that means every month. Over
Recommendation? time, data sizes change, user activity
changes, and the SQL Server optimizer
by Kendra Little changes. Each of these things mean that
indexes that are best for an application will
Recently, Jes asked the team alsochange. As you tune indexes, your query
an index tuning question: If a query has an plans will change and youre very likely to
index hint in it, will the optimizer ever see more opportunities to add, drop, and
suggest a missing index for that query? combine indexes emerge. Because of this,
I immediately loved the question because Id you want to do a few changes every month.
never really thought about it before. I I hear a lot of reasons why people dont tune
typically think of index hints as being a very their indexes...
risky game and avoid them whenever I can
after all if someone drops the index youve Read the full story:
hinted, any query hinting a non-existent archive/2013/08/how-to-master-sql-server-
index will start to fail. (Thats a really bad index-tuning/
Even so, some people love index hints...
Read the full story:

Brent Ozar Unlimited 2015 Page 30

The Parable of Index Maintenance
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.

by Kendra Little Trust disappeared. Managers squirmed.

More managers were called in.
Once up on a time, there was The DBA tried to change the subject, but it
a database server with was just too late. More than a week was
500GB of data and a heavy wasted over Fragmentation-Gate. It was a
read workload of dynamic huge, embarrassing distraction, and it
queries. Data was updated solved nothing.
frequently throughout the day and index
tuning was a serious challenge. At the best Here's the deal-- the DBA was actually right.
of times, performance was dicey. Fragmentation wasn't the root cause of the
performance problem. But he made a
Things went bad miscalculation: he should have set up
Application performance plummeted. Lots of occasional automated index maintenance to
code changes had been released recently, align with his team's normal practices and
data was growing rapidly, and the hardware standards.
wasn't the absolute freshest. There was no Why you need automated index
single smoking gun-- there were 20 smoking maintenance
When performance gets bad, one of the very
A team was formed of developers and IT rst things people look at is whether
sta to tackle the performance issue. Early systems involvedare congured according
in the process they reviewed maintenance on to best practices. If you're not following a
the database server. Someone asked about best practice, you need a good reason why.
index fragmentation. The DBA Manager
said, "Of course we're handling Regular index maintenance still has a lot of
fragmentation!" But a few queries were run merit: even in Shangri-La, where your data
and some large, seriously fragmented all ts into memory and your storage system
indexes were discovered in production. is a rockstar with random IO, index
maintenance can help make sure that you
The DBA explained that fragmentation don't have a lot of empty space wasting
wasn't the problem. He didn't have loads of memory.
automated index maintenance set up, but
heperiodically manually defragmented It's still a good idea to automate index
indexes that were more than 75% maintenance. Don't go too crazy with it--
fragmented. monitor the runtime and IO use and run it
only at low volume times to make sure it
Bad, meet ugly helps more than it hurts.
The whole performance team ipped out.

Brent Ozar Unlimited 2015 Page 31

How much downtime can parameters, and you'll
you spare? need to play with them.
Before you implement Our Best Free Get used to the 'cmdexec'
index maintenance, nd job step types. When you
out how much time tables
SQL Downloads install the scripts you'll
can be oine in each of includes video see that the SQL Server
your databases. Agent jobs run index
If you've got SQL Server
tutorial on Olas maintenance using a call
to sqlcmd.exe in an
Standard Edition, index script setup. MSDOS style step. That's
rebuilds are always
by design!
Use the examples on the
Even with SQL Server
website. If you scroll to
Enterprise Edition, you
the bottom of the index
can specify an online
maintenance page you'll
rebuild unless the index
nd all sorts of examples showing
contains large object types. (This
how to get the procedure to do dierent
restriction is relaxed somewhat in SQL
useful things.
Server 2012).
Find out when maintenance fails
Partitioned tables are especially tricky. You
can rebuild an entire partitioned index Don't forget to make sure that your
online, but partition level rebuilds are oine maintenance jobs are successfully logging
until SQL Server 2014. their progress. Set up Database Mail and
operators so jobs let you know if they fail.
Maintenance plans or custom scripts?
Tell your boss you did a good thing
You can go the easy way and use SQL Server
Maintenance Plans, but unfortunately Finally, write up a quick summary of what you
they're very simplistic: you can only say did, why you chose custom scripts or
"rebuild all the indexes" or "reorganize all the maintenance plans, and why. Share it with
indexes". You cannot say, "If the index is your manager and explain that you've set up
45% or more fragmented, rebuild it-- automated index maintenance as a proactive
otherwise do nothing." If you don't spend step.
much time with SQL Server and you've got Having your manager know you're taking the
downtime available every weekend, this can time to follow best practices certainly won't
be a decent option. hurt-- and one of these days, it just might
If you need to minimize downtime, custom help you out.
index maintenance scripts are the way to go. Learning More About Fragmentation
Our favorite: Ola Hallengren's maintenance
scripts. These are super exible, well 5 Things About Fill Factor - Including what
documented, and free! The scripts have all it's for, what it's NOT for, and why you
sorts of cool options like time boxing and shouldn't play with that.
statistics maintenance. Stop Worrying About Fragmentation -
Download and congure them on a test defragging everything can cause more
instance rst. There's a lot of options on problems than it solves.

Brent Ozar Unlimited 2015 Page 32

Pop Quiz!
Time to nd out if youve been reading, or just scanning.

Backup & Recovery Questions: 4. Are those actions documented so that

everyone who gets the email can take
1. How many production SQL Servers do
action quickly?
you have?
Index Questions:
2. What's the RPO and RTO of your most
important production server? 1. Does every table in production have a
clustered index?
3. Did your backups take the normal
amount of time last night? 2. For any exceptions (heaps), do you have
a plan to x them?
4. When was the last time DBCC
successfully nished in production? 3. Which table in production has the most
indexes, and why?
Security Questions:
4. Which frequently queried tables in
1. How many dierent people are
production have the least indexes, and
sysadmins in production?
2. Do they each know that they're
5. How are you managing index
sysadmins, and take care to avoid
3. How many of your databases hold
personally identiable 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 dierent people get the
email in case one is on vacation or
3. What actions will you take to x the

Brent Ozar Unlimited 2015 Page 33

The Right Answers Dont make changes
There's no one right answer for
any of these questions, but without a tested safety net.
some answers are more wrong
than others. Database administration is a over. Print out this email, cut out the
journey, and not everyone in the company is question list, and scribble in a few thoughts.
going to appreciate the work you're putting Pin it up on your wall, and a few months from
in. You can spend weeks or months trying to now, when you're feeling overcondent that
improve your answers on these. No matter your environment is awesome, check that list
how big your company is and how many again. Refresh your memory with the links on
database administrators you have, you're the right side of this email.
probably never going to be truly happy with When an outsider comes in, like a support
your answers here. engineer or a consultant, they're going to
You're not aiming for perfect. start at the base of your pyramid rst. Before
they start to help, they have to make sure
You're aiming for good enough that your
your data is backed up and checked for
managers accept the base of your Database
corruption. They can't go making changes
Hierarchy of Needs pyramid, and that you
without having a safety net.
feel condent in tackling the higher levels
like performance and future-proong. And you shouldn't either.
Next week's email is going to start digging Remember that as we start talking about
into performance, and we're going to ignore changing server and database settings next.
the lower levels of the pyramid - but that
doesn't mean that part of your journey is

e Yeah, but I You

How? Your
o scored it for tou re n
i l e d the the one wh chi ot
If a an ng m
. wrote it. him.
al e y

Brent Ozar Unlimited 2015 Page 34

What is SQL Server Waiting On?
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.

by Brent Ozar The Crappy Way to Check Waits

Run this simple query:
You probably got into
database administration
by way of development or SELECT * FROM sys.dm_os_wait_stats
systems administration.
You're used to monitoring
stu from the OUTSIDE And you'll get back a list of wait types, plus
using things like Performance Monitor how many milliseconds the server has spent
counters. waiting on this wait type. The time is
SQL Server has a way, way, way better tool. cumulative, measured over time since the
SQL Server instance was started - or since
When SQL Server starts running your query, someone manually cleared the wait stats.
your query consumes CPU. It sits on a CPU (Don't do that.)
scheduler, using as much CPU as it can, all
to its greedy self. SQL Server doesn't carve There's a few problems here: the wait list is
up a core and say you can run for 3 seconds really cryptic, it's lled with irrelevant system
until someone else gets to run - oh no. Your wait types, and it's measured over time.
query runs until it's done, burning CPU the What you really want is a quick snapshot of
whole time. waits over a 30-second period, with the
system wait types ltered out.
Until it has to wait for something.
The Better Way to Check Waits
The instant your query has to wait - like if
SQL Server needs to read data from hard Hit and get our
drives, or wait for someone else to let go of a free script. This returns 3 result sets - the
lock - then your query steps o the CPU and rst showing how long the server has been
goes into a waiting queue. SQL Server up, the second showing the waits since they
tracks how many milliseconds your query were last cleared, and the third is a fun one.
spends waiting, and what resource it's The third shows a running sample of waits
waiting on. over the last 30 seconds.
If your server is busy, you'll see MORE than
30 seconds of waits on the biggest
You might be bottlenecks. That's totally normal because
surprised at your SQL Server has multiple cores, each of
which may have multiple queries lined up
how little memory is waiting for something.
used for caching data. If your server isn't busy, your waits will
probably add up to 30 seconds - or much

Brent Ozar Unlimited 2015 Page 35

less. This just means the server's mostly The top two counters are about response
sitting around idle. Don't make big time - how fast the storage is returning
performance tuning decisions based on results. The bottom two counters are about
small samples like that - aim for sampling how much work we're giving to the storage.
when the server's really busy. Much like you, the more work you're asked to
do, the slower you get. The top two are the
To learn more about wait types and what
SAN person's fault, the bottom two are your
they mean, check out our wait types
fault. You want to make sure the bottom two
resources page.
numbers trend down over time by doing
Once you get started with wait stats, you'll better indexing.
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. You need to gure
Correlating Waits Stats and Perfmon out how to help an
Once we think we've got a bottleneck, we ailing SQL Server.
need to double-check those numbers by
gathering server-level metrics about that
particular bottleneck. Our SQL Critical
In our Performance Monitor tutorial, we Care helps.
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 bottleneck, 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.
Physical Disk: Avg Sec/Read
Physical Disk: Avg Sec/Write
Physical Disk: Avg Reads/Sec
Physical Disk: Avg Writes/Sec

Brent Ozar Unlimited 2015 Page 36

For LCK_* waits, collect: Got Other Waits?
SQLServer: Locks - Lock Waits/sec Wait types can be so cryptic - theres so
many of them, and theyre often not
SQL Server: Locks - Avg Wait Time
documented well. To learn more about wait
SQL Server: Access Methods - Table Lock types and what they mean, check out our
Escalations/sec wait types resources page.
SQL Server: Transactions - Longest Running Learning More About Perfmon Counters
Transaction Time
No matter what your biggest wait type is, the
SQL Server: Access Methods - Full Scans/ idea here is to correlate that wait type with
sec underlying Perfmon counter measurements
These counters help you determine how to drill down deeper and nd the root cause
much locking is going on, and where the of the problem. To learn more, here's our
source is. For example, when you're having favorite resources:
locking problems, and the Full Scans/sec Perfmon Counters of Interest Poster - from
reports a high number, maybe you've got a Quest Software, listing the best counters by
lot of table scans going on, and those are type.
grabbing locks across tables while they
Our Perfmon tutorial - explaining how to
work. Or maybe Longest Running
collect the data and analyze it.
Transaction Time is reporting a few minutes -
meaning someone is running a really long Jimmy May's Perfmon Workbook - an Excel
transaction, and it's starting to block lots of spreadsheet with Jimmy's favorite counters
other users. and thresholds.

Watch Brent explain wait stats while hes

dressed up as Richard Simmons. Click here.

Brent Ozar Unlimited 2015 Page 37

Get More Help from Us
Weve got more tricks than a pony

by Brent Ozar Unlimited VIDEO COURSES:

We love - no, we LOVE - helping How to Think Like the SQL Server Engine,
people get relief for data pains, $29 - Kick start your performance tuning
and weve got lots of options to with insight into SQL Servers brain.
DBA Job Interview Question and Answer
Kit, $29 - Practice questions and more.
FIRST AID: TOTALLY FREE STUFF Virtualization, SANs, and Hardware for
SQL Server, $299 - 5 hours of subsystem
We build cool troubleshooting tools and give
them away for free:
How to Tune Indexes and Speed Up SQL
sp_Blitz - fast SQL Server health check. Server, $299. 6.5 hours of quizzes,
sp_BlitzIndex - identies indexing scripts, and more!
madness dragging down your SQL Server. You can watch our high-denition training
Our blog - thousands of articles on from your desktop, laptop, or even your iPad
performance tuning, availability, and for 18 months.
career development.
Thats just a sample - check out the full list.
Much more - like our posters, YouTube
videos, and weekly webcasts. SQL CRITICAL CARE:
Tired of struggling with a slow, unreliable
SQL Server?
ACROSS THE UNITED STATES In just 4 days, well work together with you to
get to the root cause, explain your options,
and give you a simple, prioritized action plan
Our classes are taught by real experts with to make the pain stop.
hands-on knowledge - specically, us. We
share the latest cutting-edge tips and tricks We dont keep secrets: you get to keep our
that weve learned in real-life deployments. scripts, and you watch us work. Its like the
best conference training, but in your own
Were available for questions and answers - environment.
its your chance to talk face-to-face and get
personal advice on your tough challenges. Schedule a free 30-minute consultation with
us and learn more about our SQL Critical
Join us in-person at our classes! Care: email

Brent Ozar Unlimited 2015 Page 38