Rotating Header Image

If an Index is Heavily Fragmented Will SQL Server Still Use It?

Brent Ozar ( @BrentO , blog ) recently wrote a blog post about how you should Stop Worrying About SQL Server Fragmentation which prompted this twitter convo:

So I thought I would write up a quick test that does the following:

  • Create a Test DB
  • Create a test table (Customers) that has a CreateDateTime that defaults to a random date greater than (2000-01-01)
  • Create an index on CreateDateTime (idx_CreateDateTime)
  • Loop through and insert 1 million rows of data
  • Check the fragmentation of idx_CreateDateTime (should be around 95-99% fragmented)
  • Query the table using a WHERE clause that will use the idx_CreateDateTime index
USE [master]
GO
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'TestIndexUsage')
DROP DATABASE TestIndexUsage
GO
CREATE DATABASE TestIndexUsage ON PRIMARY
( NAME = N'Test', FILENAME = N'C:\Test.mdf' , SIZE = 1GB , FILEGROWTH = 100MB )
 LOG ON
( NAME = N'Test_log', FILENAME = N'C:\Test_log.ldf' , SIZE = 100MB , FILEGROWTH = 100MB)
GO
ALTER DATABASE TestIndexUsage SET RECOVERY SIMPLE
GO
SET NOCOUNT ON
GO
USE TestIndexUsage
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Customers]') AND type in (N'U'))
DROP TABLE [dbo].[Customers]
GO
CREATE TABLE [dbo].[Customers](
	[CustomerID] [int] PRIMARY KEY IDENTITY(1,1),
	[FirstName] [nvarchar](50) NULL,
	[LastName] [nvarchar](50) NULL,
	[CreateDateTime] [datetime] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Customers] ADD  DEFAULT (DATEADD(DAY, ABS(CHECKSUM(NEWID()) % 3650), '2000-01-01') ) FOR [CreateDateTime]
GO
CREATE INDEX idx_CreateDateTime ON Customers(CreateDateTime)
GO
DECLARE @LoopCounter int = 1000000
 
WHILE @LoopCounter != 0
BEGIN
	INSERT INTO Customers(FirstName, LastName)
	SELECT	CONVERT(varchar(50), NEWID()),  CONVERT(varchar(50), NEWID())
 
	SELECT @LoopCounter = @LoopCounter - 1
END
 
SELECT	OBJECT_NAME(A.object_id) as 'TableName', i.name as 'IndexName',
		A.index_type_desc, A.avg_fragmentation_in_percent
FROM	sys.dm_db_index_physical_stats
		(DB_ID(), OBJECT_ID(N'dbo.Customers'), NULL, NULL , 'LIMITED') as A
		LEFT OUTER JOIN sys.indexes AS i ON A.object_id = i.object_id AND A.index_id = i.index_id	
 
-- NOW TURN ON INCLUDE ACTUAL EXECUTION PLANS
/*
SELECT	FirstName, LastName, CreateDateTime
FROM	dbo.Customers
WHERE	CreateDateTime = '2007-01-01'
*/

As you can see even though the index idx_CreateDateTime is HEAVILY fragmented the optimizer will still use the index to return the data requested.

A fun little quicky to hopefully help clear up any doubts out there about this SQL Server myth.

Enjoy!! (Follow me on Twitter: @ColinStasiuk)

Post to Twitter Post to Delicious Post to Digg Post to StumbleUpon

SQL Server 2008 R2 SP2 Now Available

From the Microsoft SQL Server Team Blog

“As part of our commitment to delivering and supporting high-quality software to our customers, Microsoft is pleased to announce SQL Server 2008 R2 SP2. Customers with existing investments on SQL Server 2008 R2 are encouraged to explore Service Pack 2. Service Pack 2 for SQL Server 2008 R2 includes product improvements based on requests from the SQL Server community and hotfix solutions provided in SQL Server 2008 R2 SP1 Cumulative Updates 1 to 5. A few highlights are as follows:

  • Reporting Services Charts Maybe Zoomed & Cropped

Customers using Reporting Services on Windows 7 may sometime find charts are zoomed in and cropped. To work around the issue some customers set ImageConsolidation to false.

  • Batch Containing Alter Table not Cached

In certain situations with batch files containing the alter table command, the entire batch file is not cached.

  • Collapsing Cells or Rows, If Hidden Render Incorrectly

Some customers who have hidden rows in their Reporting Services reports may have noticed rendering issues when cells or rows are collapsed. When writing a hidden row, the Style attribute is opened to write a height attribute. If the attribute is empty and the width should not be zero.

Customers are highly encouraged to stay on a supported service pack to ensure they are on the latest and most secure version of SQL Server 2008 R2.

Enjoy!! (Follow me on Twitter: @ColinStasiuk)

Post to Twitter Post to Delicious Post to Digg Post to StumbleUpon

Developers… Please love your DBA – (Connection Strings)

I’ve been working alot with Resource Governor for SQL Server 2008 lately and one thing that has turned into quite a challenge is coming up with a good “Classifier Function“.

After much debate I decided that the best route to go is to use the “Application Name” primarily as my deciding factor as to what workload group a user’s connection will get placed in.

Through this process I found that a good portion of application connection strings DID NOT have this variable populated so most apps had the nice generic “.NET sqlClient Data Provider” description for their Program Name.

Needless to say an email was drafted and sent out requesting that all connection strings include the:

ApplicationName = <<Your App Name>>;

Now this is not just helpful for building Resource Governor classifier functions but if you’ve ever done some performance monitoring/tuning and used SQL Profiler think of the benefit of being able to possibly filter/investigate on a particular application name.

I’ll post more updates as to how developers can show their DBAs some love as I think of them.

Have a great weekend!!

Enjoy!! (Follow me on Twitter: @ColinStasiuk)

Post to Twitter Post to Delicious Post to Digg Post to StumbleUpon

#OKPASS was more than “ok” it was GREAT!

On the last PASS Regional Mentor call it was announced that (on a trail basis) the Regional Mentors would be allowed a fixed budget of money to recoup travel expenses for getting out and seeing chapters in their region.

This is AMAZING for regions like mine (Canada) which span thousands and thousands of kilometers (or miles for my friends in the south) to be able to go out and meet some of the Chapter leaders and members face to face.  The first chapter I decided to go see and speak at was #OKPASS (Okanagan PASS).  For those of you that don’t know the Okanagan is in the interior of British Columbia Canada and not where Mr. Miyagi from the Karate Kid is from (Okinawa).

Driving to OKPASS would take approx 11 hours (due to having to navigate through the Rocky Mountains)… flying there takes 50 minutes.

This was the inaugural (or Kick-Off) meeting for OKPASS so it just seemed like the logical choice so that not only can they have a warm body presenter there but also so I could help the new Chapter Leaders (Jamie @littlefuzz and Melody @KootenayDBA) with any questions/concerns they might have.

The meeting went great and I am looking forward to the next time I can get out to this beautiful region.  Any speakers that are planning a trip to B.C. or have the means to find your way there I HIGHLY recommend it as Kelowna (the biggest city in the Okanagan) is probably the most beautiful city in Canada.  It’s surrounded by mountains and beautiful lakes and would be a great place to spend a few days for a little RnR.

HUGE HUGE HUGE thanks to PASS for putting this type of benefit in for the Regional Mentors (if only on a trail basis for now) as it helps not only me as a Regional Mentor but the chapters as well to get more “face time” and a warm body presenter in the room.

If you’d like to chat with me about this or anything else (SQL or other) please leave a comment or hit me up on my Twitter: @ColinStasiuk

Post to Twitter Post to Delicious Post to Digg Post to StumbleUpon

#OKPASS – I’m Leaving on a Jetplane

OK so this will be another first for me…. I’ve done plenty of remote presentations (via LiveMeeting) for PASS Chapters around the world but this will be the first time I’ve actually flown to another PASS Chapter (OKPASS)  and presented live in person! :)

Something that I’ve always envied about speakers in the US is how easy it is for them to travel (either by plane or driving) to other PASS chapters that are nearby.  From where I live (Edmonton) the ONLY PASS Chapter that is not an minimum of a 10 hour drive is Calgary PASS (Where I’ll be presenting in July) so this is really exciting for me.

PASS is trying to help Regional Mentors get out to the chapters in their region for more “face time” by (currently on a trial basis) allocating some budget money for regional mentors travel expenses.  I think this is an amazing opportunity for Regional Mentors to get out to a chapter that they haven’t been to before, do a presentation, and get some face to face contact.  I know I wouldn’t of been able to present in person for OKPASS if it wasn’t for this so I’m really excited (and thankful) for the opportunity!

Meeting details below:

Date: May 26th 2012
Time: 3:00 pm – 5:00 pm
Location: Landmark II Building, Suite #702, 1708 Dolphin Ave. Kelowna, B.C.
Speaker: Colin Stasiuk
Topic: SQL 2012 – When Worlds Collide

Session Abstract:

This intro to SQL Server 2012 presentation is going to look at some of the new features and functionality for not only the DBAs (EDMPASS) but also the developers (EDMUG). For the developers in the audience we’re going to discuss and demo new features like paging, result sets, sequence numbers and throws and for the DBAs in the room we’ll discuss AlwaysOn, contained databases, and user defined server roles. Hopefully this will help get you prepared and excited for the upcoming newly released version – SQL Server 2012.

Presenter Information: Colin Stasiuk ( blog | twitter )

Colin Stasiuk is an MCP, MCTS SQL 2005/2008, MCITP DBDEV, and MCITP DBA. Currently, he is an independent consultant contracted to Invidi Technologies Corporation and also provides remote database support and monitoring through his SQL Wingmen service offering. Colin is an accomplished Microsoft SQL Server DBA who has been working with SQL Server since 1996. He is the owner of Benchmark IT Consulting and is always willing to lend a hand with questions in many SQL Server community forums and via Twitter. His specialties include SQL Server Administration, Performance Tuning, Security, Best Practice / Standards, Upgrades, and Consolidation. Colin is the Regional Mentor for PASS Canada, President and founder of EDMPASS, and co-author of Pro SQL Server 2008 Policy-Based Management.

If you’d like to chat with me about this or anything else (SQL or other) please leave a comment or hit me up on my Twitter: @ColinStasiuk

Post to Twitter Post to Delicious Post to Digg Post to StumbleUpon

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.