Monthly Archives: August 2011

How to Prove It’s a SAN Problem Webcast Video

Years ago, when I was frustrated with my SQL Server’s performance, the SAN admin kept saying it was a SQL Server problem. When the SAN admin quit, I took his job – and discovered the ugly truth. In the years since, I’ve done a lot of projects with SQL Server and VMware on SAN storage, and I know how to find out right away if it’s really a SAN problem. Learn how to use disk testing tools in this session:

Along the way, we discuss:

  • My Blitz: SQL Server Takeover Script – this T-SQL script helps you rapidly assess the health of your servers.  I’ve added new backup throughput sections to show your backup throughput over time.
  • Glenn Berry’s DMV Queries – uses SQL Server wait stats to prove if the server’s waiting on storage.  Also check out Glenn’s book on SQL Server hardware.
  • Microsoft SQLCAT disk latency recommendations – Top OLTP Issues and OLTP Best Practices.  Yes, these these posts are old, and if your SAN guy complains about that, respond with, “Oh, are our disks not supposed to be as fast as they were in 2006?”
  • CrystalDiskMark – the easy button of storage testing.  It doesn’t mimic SQL Server at all, but that’s okay, because remember that we’re proving it’s a SAN problem.
  • SQLIO – nowhere near as easy as CrystalDiskMark, but gives you much more fine-grained control.  I still don’t really try to mimic SQL Server IO patterns exactly, but just use this to get a sanity check after the quick results from CrystalDiskMark.
  • Tom’s Hardware Disk Performance Charts – to get quick ballpark numbers to compare your storage throughput.  Yes, these are only single hard drives, but disturbingly, you’ll often find that poorly configured storage isn’t all that much faster than a single enterprise hard drive.
  • Wes Brown’s Fundamentals of SQL Server IO – great educational series that goes into detail with lots of links.
  • My SAN Best Practices – a collection of my posts about SQL Server on shared storage.

If you liked this video, I’ve got several upcoming training sessions:

Brent Ozar

Brent specializes in performance tuning for SQL Server, VMware, and storage. He's one of the very few Microsoft Certified Masters of SQL Server, a published author, and a Microsoft MVP. He likes travel, Jeeps, Apple gear, jokes, and writing about himself in the third person. Read more and contact Brent.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

Secret New SQL Server Denali Feature: Data Director

You heard it here first, folks – I’ve got the scoop on what might be the most exciting new feature in Microsoft SQL Server Denali.  I hope you’re sitting down.

SQL Server 2008 R2 introduced the data-tier applications (DACs) – packaged databases that could be deployed on Azure or full-blown SQL Server.  The initial idea was that databases could be moved around from place to place and upgraded from afar.  At the time, I wrote that while the initial version wasn’t worth exploring, future versions could bring us virtualization for databases.

SQL Server Denali’s new contained databases seemed interesting at first, but like the DAC packs, were more of a down payment than an actual deliverable.  For example, they don’t really separate out TempDB per contained database – sure, they create objects in the right collation to avoid join problems, but if you’ve got one poorly-behaving app that abuses the buffer pool or TempDB, you’re still screwed.  These databases are contained in much the same way that velociraptors were contained in Jurassic Park.

Fun for the whole family. Especially your ex.

But hold on to your butts.

Enter the Data Director

What if you had a console that let you create or deploy contained databases that were really contained – not by deploying them on an existing server, but by creating a new virtual machine for each individual database?

That day is here.

With Windows Core, we’ve finally got lightweight virtual machines that can be completely locked down and managed.  With Hyper-V, we’ve got the ability to light up VMs quickly and easily via an API, which means we can do it inside SQL Server Management Studio.  Now, when you deploy a database, you get to pick how many CPUs it gets, how much memory it gets, and what tier of storage it gets.

It’s hard to guess the number of CPUs and amount of memory, though.  Project managers lie about schedules and user counts.  Developers lie about their code being optimized.  New hardware comes in and we have to move things around.  Fortunately, we can change these numbers on the fly: SQL Server’s hot-add CPU and memory capabilities haven’t been fully utilized in the wide market yet, but virtualization makes it a no-brainer.  Change the dropdown for the number of CPUs and memory, and the virtual hardware is instantly added through the hypervisor, recognized by the OS, and added to SQL Server as well.

Denali’s new AlwaysOn Availability Groups add the ability to scale out to multiple replicas for more read performance and easier disaster recovery.  It’s scriptable, so you know what that means – yep, just pick the number of additional replicas you want, and the console takes care of the rest, spinning up additional VMs for you and configuring the scale-out.

Backups?  Not only can we take full backups of the database, we can take snapshot backups of the VM host too.  We can use storage replication (built into the hypervisor, no matter what storage we’re using) to seamlessly replicate the entire server from our production datacenter over to a disaster recovery datacenter without the hassles of mirroring, log shipping, or replication.  Just check a box, and it’s taken care of.  All of this integrates with Policy-Based Management – set a policy for production, and all of the new production-class databases you create will inherit this policy.

That license really has teeth.

OPEN SOURCE IS COMING FOR YOU!

One of the reasons we need those backups is to restore – whether it’s to development, or to test a new version of SQL Server.  With this new feature set, you can simply restore to a new database server name in a matter of seconds thanks to virtualization snapshots.  This means when you need to test a new version of Linux, you…

Oh, wait, you caught me.

VMware Killed the DBA Star

This is going to be a hard paragraph for you to read, but here goes.  Data Director isn’t a feature of SQL Server Denali.  It’s VMware vFabric Data Director.  And, uh, it’s for Postgres, not SQL Server.  And it might be cheaper than SQL Server Standard Edition for some companies.  Here’s a demo video:

I KNOW, right?  I shook my head when Microsoft introduced the DAC Pack two years ago, I shook my head at Denali’s contained databases, but my floor shook when I saw what a virtualization vendor managed to pull off in Version 1 of their database appliance.  This looks fantastic for run-of-the-mill infrastructure databases.

I know what you’re thinking: who wants one OS per database?  Infrastructure managers, that’s who.  They want to avoid the hassles of databases stepping on each other just like you do, and they don’t mind throwing hardware at the problem.  Hardware is cheap – especially compared to salaries.  Why not throw another blade in whenever we add another dozen databases?  Let VMware manage the load by moving things around automatically.

If you’re a DBA, and you’re not learning about the cloud – whether it’s public clouds like SQL Azure or private clouds like VMware vSphere – you’re never going to see your career shift coming.  And believe me, it’s coming – not this year, maybe not next year, but if you wait until it’s a no-brainer for the CIO to deploy it, then it’s going to be a no-brainer for him to let you go and hire someone who understands these new technologies.

And the dinosaur’s gonna be you.

Brent Ozar

Brent specializes in performance tuning for SQL Server, VMware, and storage. He's one of the very few Microsoft Certified Masters of SQL Server, a published author, and a Microsoft MVP. He likes travel, Jeeps, Apple gear, jokes, and writing about himself in the third person. Read more and contact Brent.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

The Dedicated Admin Connection: Why You Want It, When You Need It, and How To Tell Who’s Using It

There’s a DAC in SQL Server that you should know and love. Sadly, you probably either haven’t met or you’ve forgotten to turn it on.

DAC? What’s That?

First, a little disambiguation. The acronym ‘DAC’ is too popular.

For the context of this article, our DAC is the ‘Dedicated Admin Connection’.

SQL Server implements a totally unrelated DAC as well: a data tier application or DAC package. We’re not talking about that here.

What Can DAC Do For You?

Have a SQL Server that’s in crisis? The DAC can help you stage an intervention.

The Dedicated Admin Connection was built to help you connect and run basic troubleshooting queries in cases of serious performance problems. This is your opening to grab a ‘Get Out of Jail’ card, but since you don’t use this on a regular basis it’s easy to forget how to use it. It’s also easy to forget to enable access to the DAC remotely.

How the Dedicated Admin Connection Works

The DAC uses a special reserved scheduler which has one thread for processing requests. This essentially means that SQL Server is keeping a backdoor open to processor resources available just for you.

Don’t be too tempted to abuse this privilege. That one thread is just one thread— there’s no parallelism for queries running on the DAC. Two percent of you will be tempted to use this for your maintenance jobs on busy systems. Seriously, just don’t go there. The DAC was not designed for high performance.

How to Enable the DAC for Remote Connections and Clusters

By default the DAC is only enabled for accounts logged onto the local machine. For production servers, that means it only works for remote desktop sessions to non-clustered SQL Server instances. If your instance is clustered or if you’re connecting over TCP/IP, you’re out of luck unless you change one setting. That setting is ‘Remote Admin Connections’.

Should you enable remote connections? I agree with Books Online, which points out:

If SQL Server is unresponsive and the DAC listener is not enabled, you might have to restart SQL Server to connect with the DAC.

For clusters, sign me up! I’m in favor of enabling it for other instances as well. In times of trouble, you want to minimize the amount of time you spend using remote desktop on a server having problems. You want to use that only to gather information you can’t get another way.

Enabling the DAC for remote connections is easy as pie. It is controlled by the ‘Remote Admin Connections’ setting. To enable it, you simply run this bit of code:

EXEC sp_configure 'remote admin connections', 1
GO
RECONFIGURE
GO

There’s always a catch. You many need to get firewall ports opened as well, depending on your environment and where you intend to connect from. This will probably be port 1434, but that will vary depending on your configuration. (Books Online has got your back: read more in the “DAC Port” section here.)

Only One Sysadmin Can Ride This Horse At a Time

This isn’t a party line, only one sysadmin at a time can use the DAC. Also, you should only run simple, quick queries using the DAC.

In other words, only connect to the DAC when you really need to. When you’re done wipe the seat make sure to disconnect.

How to Connect to the DAC

You can connect to the DAC using the command line. Use the “-A” option with SQLCMD.exe.

I find it more convenient to connect in Management Studio itself. You do this by prefixing the instance name you are connecting to with “Admin:”.

One FYI: Object Explorer can’t connect to the DAC. If you open SSMS and have Object Explorer connecting by default, the first connection prompt you see with be for that. If you try to tell that to connect to the DAC, it’ll fail. That’s a good thing, we wouldn’t want the power to go to Object Explorer’s head.

Who’s Been Sleeping in My DAC? How to Tell Who’s using the Dedicated Admin Connection.

If you try to connect to the DAC when someone is already connected, you’ll see a connection error. It probably won’t tell you straight out that someone’s connected to the DAC already, but if you check the SQL Server Error log you should see the message:

Could not connect because the maximum number of ‘1’ dedicated administrator connections already exists. Before a new connection can be made, the existing dedicated administrator connection must be dropped, either by logging off or ending the process.

So if you can’t get the DAC, how can you tell who’s using it?

Here’s a hint— the DAC uses a Dedicated TCP Endpoint. It’s even endpoint #1. The DAC is ranked #1 by endpoints everywhere!

This makes finding someone using the DAC nice and simple, because endpoint_id is listed in sys.dm_exec_sessions. So you’ll just need a little something like this:

select
	case
		when ses.session_id= @@SPID
		then 'It''s me! '
		else '' end
		+ coalesce(ses.login_name,'???') as WhosGotTheDAC,
	ses.session_id,
	ses.login_time,
	ses.status,
	ses.original_login_name
from sys.endpoints as en
join sys.dm_exec_sessions ses on
	en.endpoint_id=ses.endpoint_id
where en.name='Dedicated Admin Connection'

Your Mission: Get Back with your DAC Today

Take a few minutes today to connect with the DAC in a test environment. Check whether you have remote admin connections enabled in your environments, and talk about what the impact of that might be.

The DAC can save your bacon— make sure you have the ability, and use the power wisely.

Kendra Little

Kendra specializes in high availability and performance tuning. She is a Microsoft Certified Master in SQL Server-- the highest technical SQL Server Certification available. Kendra loves databases and software development more than long walks on the beach. Those cartoons in her blog posts? She draws 'em all. Read more and contact Kendra.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

Upcoming SQL Server Training Webcasts

Come get your learn on.  Our Tech Triage Tuesday webcasts have taken off – we’ve got over 250 registrants for this week’s pin-the-blame-on-the-SAN-guy session – so we’re adding even more webcast options.  In addition to our regular 30-minute Tuesday 11:30AM Central webcasts, we’re adding two four-hour sessions in September.  Here’s what’s coming up:

August 30 – How to Prove It’s a SAN Problem
Brent Ozar, Technology Triage Tuesday Webcast

Years ago, when Brent was frustrated with his SQL Server’s performance, the SAN admin kept saying it was a SQL Server problem. When the SAN admin quit, Brent took his job – and discovered the ugly truth. In the years since, Brent’s done a lot of projects with SQL Server and VMware on SAN storage, and he knows how to find out right away if it’s really a SAN problem. Learn how to use disk testing tools in this 30-minute session.

September 6 – Does This Transaction Log Make My Database Look Big?
Tim Ford – Tech Triage Tuesday

Face it; your database doesn’t get much exercise. It spends its day sitting on the same old server in the same old rack eating garbage all day long. Sure once in a while it may go for a spin, but SSDs are chipping away at that. They may hop around in a VM farm, but for the most part they sit there lazily wasting away the day. What goes into the log? What settings affect the size of the log and what can you do to keep it lean? Join Tim to find out.

September 7 – SAN Basics for DBAs
Brent Ozar, 24 Hours of PASS Webcast

They keep telling you it’s a SQL problem – but how can you prove where the real problem is? Learn the basics about your storage options like RAID 5, RAID 10, solid state drives, and why your biggest bottleneck might be a $5 cable. No storage experience required. This session is for production database administrators who’ve never looked inside the black box of storage before.  Register now.

September 13 – Configuring SQL Server in Amazon EC2
Jeremiah Peschka – Tech Triage Tuesday

Sure, the cloud is a cheap and easy way to spin up more servers and spin up more servers faster, but what do you do when performance problems hit? There’s no SAN team to blame and no infrastructure team to move you to a new server. This talk will expose some common performance complaints about moving SQL Server to the cloud and provide explanations and mitigations to keep you floating along on cloud 9.

This talk is for DBAs, developers, and managers who are considering moving their infrastructure into EC2. If you’re already running in EC2 and you haven’t solved some of your performance problems, this might help you out too, but I wouldn’t count on it.  Register now.

September 13 – Storage Area Networks for DBAs
Brent Ozar, 4-Hour Webcast

If you like Brent’s 1-hour sessions on SAN basics, you’ll love the extended-length, unrated version.  Brent will cover:

  • The differences between RAID levels (5, 6, 10, DP)
  • How pools of disks are shared between servers, and why you might like it
  • Pathing: the route between your server and your data
  • How PCI Express SSDs like Fusion-IO work differently

Then he ties it into SQL Server to show you:

  • Why the SAN admin always thinks you’re not pushing the SAN hard enough
  • When you don’t need to separate your data and your log files – and when you do
  • Why pathing may determine the number of data files you need for speed
  • When you might actually need two log files in your database – despite what the “experts” say

This session is for DBAs who are frustrated with slow or unreliable SAN performance, or DBAs who are about to embark on buying a new SAN.  Register today.

September 17 - SQL Saturday #84, Kalamazoo

The whole Brent Ozar PLF team will be visiting and presenting at SQL Saturday Kalamazoo! Come on out and meet the team.

September 20 – Tuning SQL Server on VMware 4
Brent Ozar – 4-Hour Webcast

SQL Servers can run faster and more reliably under VMware vSphere 4, but they can also run craptastically.  You can’t just use the defaults and expect databases to fly, but thankfully, it’s not that hard to get high performance and uptime with just a few important tweaks.

In this four-hour session, production DBAs will learn why:

  • Virtual CPUs are different – and sometimes less is more
  • Virtual memory is different – and how to set SQL Server max memory under VMware
  • Virtual storage is different – and whether we should use VMDKs or raw LUNs
  • Virtual networking is different – and why we have to do our backups differently
  • Virtual monitoring is different – and why Task Manager is a dirty, filthy liar

This session is for DBAs who are frustrated with slow or unpredictable SQL Server performance inside VMware, or DBAs who are about to embark on virtualizing their first SQL Server.  Register today.

October 4 – PASS Summit Conference Tips and Tricks
Entire Brent Ozar PLF Team – Tech Triage Tuesday Webcast

Are you going to the PASS Summit conference in Seattle this year? We’ve been going for years, and we share our favorite inside info. Learn the best places for breakfast, which sessions you should definitely attend, and where things happen after hours. We’ll make sure you get the most out of your Summit experience.  Register now.

Brent Ozar

Brent specializes in performance tuning for SQL Server, VMware, and storage. He's one of the very few Microsoft Certified Masters of SQL Server, a published author, and a Microsoft MVP. He likes travel, Jeeps, Apple gear, jokes, and writing about himself in the third person. Read more and contact Brent.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

You’ll be Reunited with an Old Friend in 2012

I may love a challenge, but I also like things that are quick and easy.

There’s one particular quick and easy feature from SQL Server 2005 which I thought was gone for good.

But there are signs it’s going to return.

Any guesses what I’m talking about?

Nope, it’s not Notification Services. (But that would be kinda funny.)

It’s better.

DBAs Like Data.

If you work with databases, you probably like graphs and trends. You probably enjoy tools that help you analyze the state of a server quickly.

This is why DBAs love the DMVs.

And this is why DBAs will probably also love the….

SQL Server 2012 Performance Dashboard Reports

I spied an announcement for this SQL PASS Session:

I don’t know about you, but I really liked these reports from 2005. They were handy, friendly, and easy to use. And they’re great because they’re accessible, and they lead people into learning more about the DMVs and building their skills for tuning SQL Server.

What Do You Hope is in the 2012 Version?

Were you a fan of the 2005 version of this report? What do you hope is updated or added in the new version?

Kendra Little

Kendra specializes in high availability and performance tuning. She is a Microsoft Certified Master in SQL Server-- the highest technical SQL Server Certification available. Kendra loves databases and software development more than long walks on the beach. Those cartoons in her blog posts? She draws 'em all. Read more and contact Kendra.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

Even Simple Changes Can Cause Trouble: A Tale of TempDb

I recently presented to the Salem Oregon SQL Server user group. During my visit I heard an interesting story: once upon a time in a land not so far way, a high severity production incident was caused by adding an additional data file to the tempdb database.

Multiple Tempdb Data Files: Commonly Recommended

Each quadrant of the bento is a TempDB data file

Using multiple database files in tempdb is often recommended as a best practice in SQL Server. A single page can become a bottleneck in tempdb when many operations want to allocate space. Adding multiple tempdb files can help alleviate the problem. Adam Machanic gives a great summary of how to reproduce and identify this issue.

Creating multiple files in tempdb is recommended over hill and dale in Books Online, on Technet, and in many blogs.

Many people suggest you should only add extra files to tempdb when you see specific metadata contention on that instance. However, most SQL Server DBAs who’ve been bit by this problem– and many who’ve read about it– configure multiple files when they’re building out a SQL Server instance as a preventive measure. This is because DBAs love uniform configuration and managers hate finding out a major incident could have been prevented. Once a team gets burned by a particular issue, if the fix does not have a negative impact they prefer to apply that fix everywhere. It keeps the egg off their faces.

What Could Possibly Go Wrong?

I usually classify adding extra tempdb data files as an easy, low risk change. As long as you don’t have a very large existing single file that you need to shrink, adding additional files and bringing the files to a uniform size is typically a simple operation. It’s important to use change control and the appropriate change windows, but this isn’t the type of change I worry much about.

But, as with all things, there’s a way this could cause surprising results.

We Added a Tempdb Data File, and Data Was Wrong

The story begins shortly after a new file was added to tempdb on an existing SQL Server instance. Suddenly, some new data was identified as incorrect. Jobs were running successfully. Some newly processed data was correct. But some was not.

Nobody likes incorrect data.

This is how bad data feels.

My first question was just to make sure I’d understood—- there were no code changes?

Nope, no code changes. Just an additional tempdb physical file.

The Plot Thickens

This type of story gets me really curious. I guess I’ve gotten myself into enough trouble to really love a good story of a change gone wrong. I had to know more.

Were temp tables being used? Yes, data was being bulk loaded into temp tables.

Were the rows in the temporary table itself wrong? Were values from different rows somehow being mixed together? (I didn’t know of a way for this to happen, but it seemed the first thing to clarify.)

The rows still held the expected data, I learned. But the rows were in the wrong order.

Aha! I yelped like a dog. That makes sense.

A Quick Example

It’s fun to reproduce this issue and see how tempdb behaves.

First, start with a test (not production) SQL Server instance with a single tempdb data file. To count the amount of data files in tempdb, you can run:

select file_id,name, physical_name
from tempdb.sys.database_files
where type=0 -- data files

If you need to remove some of the data files in tempdb for your test, you can do so with code like this. Just substitute in the actual file name.

dbcc shrinkfile(tempdev2,emptyfile)
go
alter database tempdb remove file tempdev2
go

Now we want to create a flat file with a bunch of rows in it. We want something that will be easy to tell if it’s in order or not, so for our test we’re going to create a file with rows of numbers from 1 to 50K.

First create a global temporary table holding the sorted list of numbers. In this example we’re using a clever method to create a numbers table using CTEs attributed to Itzik Ben-Gan.

if object_id('tempdb..##imReallyOrdered') is not null
	drop table ##imReallyOrdered
go

create table ##imReallyOrdered (N int not null);
go

WITH N1 AS ( SELECT 1 as N UNION SELECT 0 ),
	N2 AS ( SELECT 1 as N FROM N1 CROSS JOIN N1 as N1B ),
    N3 AS ( SELECT 1 as N FROM N2 CROSS JOIN N2 as N2B ),
    N4 AS ( SELECT 1 as N FROM N3 CROSS JOIN N3 as N3B ),
    N5 AS ( SELECT 1 as N FROM N4 CROSS JOIN N4 as N4B ),
    Numbers as (SELECT ROW_NUMBER() OVER (ORDER BY N) as N FROM N5) --65536
insert ##imReallyOrdered(N)
SELECT TOP 50000 N
FROM Numbers
order by N --I'm really ordered! Really!
GO

Now we can use this table to create a flat file.

You can do this from SQL Server Management Studio if you enable SQLCMD mode and then run the following (using a location of your choice).

--Export the data
!!bcp ##imReallyOrdered out "c:\test\Numbers.dat" -T -n

--Create a format file
!!bcp ##imReallyOrdered format nul -T -n  -f "c:\test\Numbers.fmt"

Now we’re set up to test our import. We should have a single tempdb file and our files prepared to bulk load.

Run the following loop to test your bulk load. This loop creates a temporary table and then bulk inserts the data from our flat file without specifying a sort order. It then adds an identity column to the table and uses that identity column to verify the order of the data. It does this 50 times and tracks the results in a temp table. This may take a few minutes to run on a test system, depending how much power it’s got.

I’ve run this several times on SQL Server Denali CTP 3 and SQL Server 2008 R2, and each time I’ve found the data to be sorted in numeric order– as long as I’m using 50K rows with this file. Adjust the path to the file and format file you created if you placed them elsewhere.

USE tempdb ;
go

IF OBJECT_ID('tempdb..#results') IS NOT NULL
    DROP TABLE #results ;
go

CREATE TABLE #results (
    NumberOutOfOrder INT NOT NULL ) ;
go

BEGIN
    IF OBJECT_ID('tempdb..#temp') IS NOT NULL
        DROP TABLE #temp ;

    CREATE TABLE #temp ( N INT ) ;

    BULK INSERT #temp
    FROM 'C:\test\Numbers.dat' WITH (FORMATFILE='c:\test\Numbers.fmt', FIRSTROW=1, TABLOCK) ;

    ALTER TABLE #temp ADD i INT IDENTITY NOT NULL ;

    INSERT  #results( NumberOutOfOrder)
            SELECT  COUNT(*)
            FROM    #temp
            WHERE   i <> N ;

END
GO 50

SELECT  SUM( CASE WHEN NumberOutOfOrder > 0 THEN 1 ELSE 0 END) AS OutOfOrder,
	 SUM( CASE WHEN NumberOutOfOrder = 0 THEN 1 ELSE 0 END) AS InOrder
FROM #results ;
go

Now add a second file to tempdb. Use code similar to this, but set the file location for your instance. Set the size, filegrowth, and maxsize properties to match your existing file.

alter database tempdb
add file (name='tempdev2', filename='C:\MSSQL11.MSSQLSERVER\MSSQL\DATA\tempdev2.ndf', size=64MB)
go

With the second tempdb file,  re-run the loop above to drop, create, and bulk load data into the #temp table, then test the order of the rows. You should see that periodically the final query finds a significant amount of rows which are not in order.

If you don’t see the order changing, try restarting your test instance and running the loop again.

Why Aren’t the Rows Always in Order?

The rows aren’t always in order because we haven’t said they need to be in order. In SQL Server, bulk imports of all types assume that the data in a file is unordered.

Although we see see rows maintain the order in some cases when we insert them into our temporary table when SQL Server has one tempdb file, we shouldn’t rely on that behavior. It’s not guaranteed.

My test runs show that with a single tempdb data file, I will see order preserved for some sizes of data files, and that order will change when I add a tempdb file. For larger files, I see that the order is not preserved with either one or multiple tempdb files. (The level at which order changes on a single file varies by SQL Server version, which made writing the repro scripts an interesting challenge!)

But in short: since we haven’t asked for the data to be stored in a particular order, SQL Server gives the data back to us as quickly as possible— and it may or may not be in order.

What Does this All Mean? Should We Still Use Multiple Files for TempDB?

I *think* it's safe...

Sure, multiple files for tempdb is still fine. This story really just reminded me about the fragility of the custom applications we all work with every day. Every application is based on some assumptions, you see.

The story has two morals:

  • We can never assume a production change is harmless.
  • Configuration matters, and should be set identically in development, test, and production environments.

When you’re making a change to a system, unless you’ve written or recently reviewed all of the code, you never know what assumptions the code is using. It’s easy for bits of code to creep in that rely on behavior that isn’t guaranteed.

Even for changes like this one which seem like they’re production-only, the change should run through a testing cycle before making its way live. And for most accurate results, that testing cycle should use production-sized datasets– because size does matter.

Kendra Little

Kendra specializes in high availability and performance tuning. She is a Microsoft Certified Master in SQL Server-- the highest technical SQL Server Certification available. Kendra loves databases and software development more than long walks on the beach. Those cartoons in her blog posts? She draws 'em all. Read more and contact Kendra.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

Video: How to Use the Denali Windowing Functions

SQL Server Denali includes some new tricks in its T-SQL bag.  In this week’s Technology Triage Tuesday webcast, Jeremiah Peschka explains aggregations – formulas that do something to transform many numbers into one number that has meaning:

For more details, check out Jeremiah’s post on Leaving the Windows Open.

Jeremiah Peschka

Jeremiah Peschka has worked as a database and emerging technology expert at Quest Software where he researched new trends and technologies in the world of data storage. Over the course of his career he’s worked with companies across many industries as a system administrator, developer, and DBA. He’s been involved with all aspects of application development and deployment. He likes cheesecake, coffee, and ice cream.

More Posts - Website

Follow Me:
TwitterFacebook

Five Ways to Fight Blocking Video

What do *you* do when your database has a problem with blocking? Most people reach for the “NOLOCK” hint. Bad news: that may introduce other problems, and it doesn’t truly resolve the root problem in your application code. In this video Kendra Little summarizes five big-picture application changes that fight blocking long term.

This 300 level talk is designed for developers and DBAs who understand table structures, index structures, and common query patterns. We’re going to keep things general: no demos, just concept discussions.

If you like this, come join the fun in our upcoming webcasts. And here’s a secret: we start the fun half an hour before the webcast actually starts. You can join in as early as 11AM, watch us trade jokes, share our favorite links and stories from the week, and just generally horse around. It’s my favorite part of the week.

Register now to watch the webcasts.

Kendra Little

Kendra specializes in high availability and performance tuning. She is a Microsoft Certified Master in SQL Server-- the highest technical SQL Server Certification available. Kendra loves databases and software development more than long walks on the beach. Those cartoons in her blog posts? She draws 'em all. Read more and contact Kendra.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

Consulting Line: “What problem are we trying to solve?”

The problem with being a Microsoft Certified Master is that people come running up with Master-level questions.  I field a lot of very advanced questions completely out of the blue, and I can’t help but raise an eyebrow.

Deckard: “After answering forty-two questions from the Voight-Kampff test, what’s the pupil size of a Nexus-6 replicant?”

Me: “The what?  I’m sorry, I didn’t catch your name?”

Deckard: “It doesn’t matter.  After I’ve read the first forty-two emotional response questions to a version 6 skin-job, how much do their pupils dilate?”

Me: “Hmm, that’s an interesting question.  Aren’t we supposed to know after the first twenty or thirty questions if they’re a replicant?”

Deckard: “Yeah, but just give me the answer.”

Still trying to get my CLR pie piece

Me: “Well, let’s take a step back.  What problem are we trying to solve?”

What That Line Does

As much as I love Trivial Pursuit, my real job is to solve problems.  Before I give answers to obscure questions, I want to tease out the real business problem.  It’s not that I don’t trust the questioner – it’s that I don’t trust anybody, period.  When us meatbags are troubleshooting a problem, we tend to go chasing shiny objects that look like they might be related.  By the time we’ve followed the shiny object into the Bradbury Building, we’ve forgotten what we were trying to fix in the first place.

Asking this question forces the questioner to take a step back, give me the backstory, and voice their own reasons for getting to that point of troubleshooting.  Sometimes my job is just to keep saying, “Hmmm, tell me more about that,” and people will talk themselves through their own solution.  They’ll understand, just by talking, that they’ve gone down the wrong path, or that they’ve ignored vital clues on their way to a solution.

If I simply said, “You’re asking the wrong question,” I would be setting up a confrontation.  The questioner’s already frustrated, and a confrontation won’t solve their problem.  I’ll look like just another one of those guys who are telling ‘em they’re wrong, and from that point forward, they’ll disregard everything I say.

Sometimes, there really isn’t a problem at all: the questioner might just be playing Trivial Pursuit, wanting help to settle a bar bet between geeks.  The clue here is that the questioners walk up in pairs with smiles on their faces – much different than the typical questioner in this situation.

What Happens Next: The Hard Way

When you’re dealing with someone who’s really amped up and insistent that they know what they’re doing, things can get ugly, and they always use exactly the same response:

How I studied for the MCM

Deckard: “I don’t have the time to explain!  Just give me the answer.”

This is where you have to make a snap judgment.

If you give them the answer, they’re going to walk away and go right back down their (likely incorrect) path of troubleshooting.  They might thank you – although these kinds of people usually don’t – and they’ll remember you as a person with The Right Answer.  Their server won’t get any healthier, their manager won’t be happy, and even worse, they may name-drop you.  “Well, Brent Ozar said the right answer was ___” – even when that’s not what I said at all.

If you don’t give them the answer, they’re going to be pissed initially.  I try to disarm ‘em with humor.

Me: (deep breath and a smile) “Do you still beat your wife?”

Deckard: “What?!?”

Me: “Do you still beat your wife, yes or no?”

Deckard: “That’s a ridiculous question!”

Me: “I know, right?  Here’s my card.  Email me, and let’s set up a call tomorrow.  We’ll work together for one hour tomorrow, completely for free, and you can bring me up to speed about the problem.  Then I’ll be able to give you the right fix for your pain points.”

Either they get it or they don’t – and sadly, there’s a certain part of the IT population that doesn’t ever get it.  They’re the angry, fist-shaking people who kick servers and dogs, all the while believing that the system is broken.  Thankfully, they’re in the minority.

What Happens Next: The Easy Way

Deckard: “I’ve been questioning someone who just isn’t responding the way I’d think. I’m convinced she’s a replicant because she has strange memories of her childhood, but her pupils still aren’t dilating after over 40 Voight-Kampff questions.”

Me: “It’s only supposed to take 20-30 questions to get pupil dilation, so we’ve got a few possibilities here. Maybe her V-K results are skewed because you’re displaying stress when you interview her, or maybe she’s a Nexus-7, or – ”

Deckard: “There’s no such thing as a Nexus-7!”

Me: “Well, I’m bound by some NDAs, so I can’t talk about future versions, but let’s talk theoretically here for a moment. Tyrell Corporation has to keep building new versions of replicants if they’re going to stay in business, right? And they have a financial incentive to make harder-to-detect replicants. What if the next version didn’t suffer from the pupil dilation problem?”

But he still beats me every time.

Deckard: “Is that true?”

Me: “I dunno, but let’s go back to your question. Let’s reword it as, ‘The Voight-Kampff test indicates that my subject isn’t a replicant, but I need another test too. What’s an alternate test?’”

Deckard: “You’re right.”

Me: “Of course I am. I wrote this exchange in a blog post. I craft my own reality here.”

Deckard: “You’re also stunningly good-looking.”

If you liked this, check out more posts in my Consulting Lines series.

Brent Ozar

Brent specializes in performance tuning for SQL Server, VMware, and storage. He's one of the very few Microsoft Certified Masters of SQL Server, a published author, and a Microsoft MVP. He likes travel, Jeeps, Apple gear, jokes, and writing about himself in the third person. Read more and contact Brent.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

Free SQL Server Webcasts Coming Up

Our new Technology Triage Tuesdays webcast series is going really well. Every Tuesday at 11:30AM Central, we get together for a free 30-minute training session. Here’s our upcoming sessions:

Goin' to the chapel

Horsing around in Seattle

Five Ways to Fight Blocking with Kendra Little – What do *you* do when your database has a problem with blocking? Most people reach for the “NOLOCK” hint. Bad news: that may introduce other problems, and it doesn’t truly resolve the root problem in your application code. In this webcast Kendra Little summarizes five big-picture application changes that fight blocking long term. This 300 level talk is designed for developers and DBAs who understand table structures, index structures, and common query patterns. We’re going to keep things general: no demos, just concept discussions.

Leaving the Windows Open with Jeremiah Peschka on Aug 23 – Computing running totals, averages by state or group, and other complex aggregations are not easy to implement in many relational databases. SQL Server Denali includes full support for window functions. Window functions are an extension to the ANSI SQL standard. More than just another part of SQL, they enable complex ranking, ordering, and other aggregations over a subset of rows in a query. Understanding the capabilities of window functions makes it easy to extend application functionality, build rich and complex queries, and reduce overhead in the application tier. If you’re an application developer or database developer, then this talk is for you. If your job title says DBA but you tune T-SQL, there’s a good chance this talk is for you.

How to Prove It’s a SAN Problem with Brent Ozar on Aug 30 – Years ago, when Brent was frustrated with his SQL Server’s performance, the SAN admin kept saying it was a SQL Server problem. When the SAN admin quit, Brent took his job – and discovered the ugly truth. In the years since, Brent’s done a lot of projects with SQL Server and VMware on SAN storage, and he knows how to find out right away if it’s really a SAN problem. Learn how to use disk testing tools in this 30-minute session.

And here’s a secret: we start the fun half an hour before the webcast actually starts. You can join in as early as 11AM, watch us trade jokes, share our favorite links and stories from the week, and just generally horse around. It’s my favorite part of the week.

Register now to watch the webcasts.

Brent Ozar

Brent specializes in performance tuning for SQL Server, VMware, and storage. He's one of the very few Microsoft Certified Masters of SQL Server, a published author, and a Microsoft MVP. He likes travel, Jeeps, Apple gear, jokes, and writing about himself in the third person. Read more and contact Brent.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube