Windows, RIP (1992 – 2013)

Today is a very sad day for me. My love, my dear Windows has passed away. We were together for over 20 years, her illness took control over her, and during the last days she passed away.

Before I give you further details about the disease she had over the last year, I want to switch back the wheel of time, and talk about our unique love story that we have shared over the last decades.

It was a nice sunny day in 1992, where I met her in school. She wasn’t the best looking girl, but she wasn’t the ugliest one. The biggest problem was that the selection of girls wasn’ really huge, you had to live with what you got. Mainly she looked like a gothic type: black clothes with some white blinking cursors. I just called her DOS – don’t ask me why…

There were also these other girls, but they were just crazy and hippy, we have just called them Linux: hard to handle, impossible to understand how they work internally.

In the first weeks and months we had struggled very much with each other, she wasn’t really easy to handle. As long as I was playing with my fingers on her keyboard everything was fine. She told me a lot about her family, especially about her dad. Bill was his name – a business man from the greater Seattle area. He was working during the whole nights and days in his little garage, dreaming of becoming a famous man.

Once upon a time her dad told her that money isn’t everything, and that 640k of cash is enough for everyone on earth. Today I think her dad was completely wrong on that. But that’s another story.

Since we have met in 1992 we were dating each other regularily and we have known each other better and better over the days, and months. In 1993 she changed her look completely – and she turned into a very nice looking girl. She had nice windows around her whole body, I was allowed to touch her with my mouse, it was just an awesome experience. Everyone else at school was just jealous about her. Man, I was really happy during these days – I just called her Windows 3.1

But as soon was we had to do more and more work concurrently at school, she didn’t really scaled very much. Sometimes her heart just stopped working for some seconds, and she just feel down to the floor. I was very very anxious about that, because that happenend over the weeks multiple times. When I talked to her dad, he told that this behaviour of her was fine, because she just scheduled here work cooperatively. She completely relied on me for the scheduling, and when I was giving her too much work to do, she just stopped working. So it was mainly my fault…

Over the time this problem was really, really annoying, so I finally send her to the hospital for doing some medical checks in 1995. Sometimes she was so unstable that it was impossible to do any work with her. After a few weeks in the hospital she came back – as a completely different women. OMG – she was now so hot!!! Nice looking face, an awesome body, and she was also able to do concurrent work much better. In the hospital they have made a surgery and changed her to preemptive scheduling – whatever this means. The fact about this was, that finally I had no control over her anymore, she just decided herself when to do work, and when not. My brother (his name is SQL Server) couldn’t really deal with her about for this reason very good – but that’s again a completely different story. From this time on I just called her Windows 95.

Over the years she was getting more and more the look of a nice looking young woman. The craziest thing happed when she attended once upon a time a ME (Millenium Edition) party. She was getting so much drunk during that party, that she was completely destroyed over the next months. When I now look back at that time, it was just an crazy awesome time: sex, drugs, and rock & roll. But it seemed that we have enjoyed a little bit too much the various parties. Here father was very angry about us, and he hired a watchdog for her: his name was Dave Cuttler. His goal was to change her in a radical way!

That man was really serious, and I had a huge respect from him! He changed her completely – but in a very positive way. We were just getting adults – with all pro’s and con’s. Dave called her from this time on a professional woman, so I just called her Windows 2000 Professional. This was also some time around the year 2000.

In 2001 we graduated from school, and she finally got her XP degree. She and all her friends worked so long for this degree. Some of her friends just stopped studying and learning in 2001, and are still doing work with their XP degree – unbelieveable! From that time on she was my Windows XP girl.

Over the next years our life wasn’t really an adventure. Both of us were working, working, and working. We haven’t seen each other too much over the time, but life was good. In 2007 she had the crazy idea of doing a plastic surgeon (you know, getting bigger boo…, etc.). I wasn’t really a big fan about this idea, but she wanted to it, so I let her. But the result was just aweful. Before the surgeon she just looked better. Her doctor was a man called Vista…

But Mr. Vista knew that he made a big mistake, so she want back to him in 2009/2010, and she finally returned as the hottest woman on earth: from there on she was my Windows 7. We had finally an awesome time together, everything was fine, everything was perfect, we already talked about getting children, but back in 2012 she had a major car accidient. She stayed several weeks in the hospital, and they were able to recover her completely – almost. I got her back as my Windows 8 girl. When I first looked on her, I though – OMG is this really her? She looked completely different – she had tiles on her face. Yes, tiles!

Besides the tiles, she also had a splitted personality. Sometimes she called herself Classic Desktop, sometimes she called herself Metro. That accident changed her completely. I have talked to the doctors in the hospital, but they said, that we have to life with that splitted personality, and that they can’t change anything about it. They just told us that people with splitted personalities are the future. Sounds like a crazy joke, but when a doctor tells you that, you have to take that as granted. In addition the evangelists in the various churches around the world were also preaching that splitted personalities are the future of human life. I thought I was Neo from Matrix: the whole life that you have lived in the past was just one big fake!!!

After her splitted personality was getting really hard and aweful, I had to put her completely in a medical care center. I was not able to give her the necessary support at home. I’m travelling a lot, I have to do a lot of customers work (together with my brother SQL Server), so I can’t care about her the whole day. She just needed a 24/7 support service. A very hard decision, but it was the only way how to deal with it. We have met each other about every weekend, done some nice chats together, and enjoyed the weekends together.

Over the time, when her illness was getting worser and worser, I also started dating another woman. I have blogged about that new love story back in 2012. And last week, I got a call from the hospital. Yes that call: she passed away…

In the first, it was very hard for me to live with that fact, but on the other hand I’m now already dating that other girl for almost 9 months. So she is now my big love story, and my future. Of course, we have lived almost 21 years together, but I can’t grieve forever, I also have to look forward for my own life. My dear, my love, Windows I will miss you forever!

Interesting enough I have read during the last days in the news, that researchers in the Seattle area found a medicine against splitted personalities. And it seems that this medicine will be also released to all affected people over the next months without any fees. Unfortunately it’s too late for my love, she’s gone – forever. Rest in peace.

My former love Windows also had a big, powerful car, which is now useless to me, therefore I’m selling it. Here are the specs of it:

*) Intel i7 CPU, Q840 @ 1.87 GHz

*) 8 GB RAM (4×2 GB)

*) NVIDIA Quadro FX 880M (1920×1080)

*) Two USB ports version 3.0, one Powered USB port version 2.0

*) eSATA

*) LAN/WLAN

*) DVD/CD

*) Ultrabay for 2nd 2.5″ hard disk

*) Docking Station

*) 250 GB 5400rpm hard disk

*) TPM Module

*) Smart Card Reader

*) German Keyboard

So if you are interested in buying a piece of history from my life, that travelled across the whole world with me, please send me an email to “klaus DOT aschenbrenner AT sqlpassion DOT at” to discuss the further details.

Thanks for reading

-Klaus

The dangerous beauty of Bookmark Lookups

Every one of us knows Bookmark Lookups in SQL Server. They are great, because you don’t need to care about your indexing strategy. They are bad, because with Bookmark Lookup you just introduce a bunch of other problems for your SQL Server installation. Almost every time when I’m on client side for consulting gigs, I have to fight with Bookmark Lookups, it’s like juggling hand grenades – you have to be very careful!

Because of this I’m speaking this Thursday evening at the Belgian SQL Server User Groups in Brussels about “The dangerous beauty of Bookmark Lookups“. Here’s the session abstract:

You know Bookmark Lookups in SQL Server, and you rely on them for your indexing strategy? But do you also know that they are the most dangerous thing in SQL Server, which can lead to massive performance losses that blows up your CPU and I/O resources? Join this session to get a basic understanding of Bookmark Lookup, and their associated performance problems. In detail you will learn about: 

  • The Tipping Point
  • Parameter Sniffing Problems
  • Bad Statistics
  • Auto/Forced Parametrization
  • Bookmark Lookup Deadlocks

If you are interested in seeing the other side of Bookmark Lookups, you are very welcome to attend the event, and you can register here: http://sqlug.be/blogs/official_sqlugbe_blog/archive/2013/04/08/the-dangerous-beauty-of-bookmark-lookups-april-18-with-klaus-aschenbrenner.aspx

Thanks for reading and see you soon in Brussels!

-Klaus

Become an expert in SQL Server Performance Tuning & Troubleshooting!

Since September 2012 I’m a Microsoft Certified Master SQL Server 2008, which is the highest technical certification for SQL Server that is currently available. Because I want to share my performance tuning & troubleshooting knowledge, and because of the huge demand on performance tuning I’m running again this year my popular SQL Server Performance Tuning & Troubleshooting Workshop across Europe in an extended version – 4 days performance tuning & troubleshooting on a SQLMCM level!

Based on the feedbacks on past attendees I have extended the workshop with one additional day. On that 4th day we take a real SQL Server workload and execute it on our database server. Throughout the whole day we are digging into SQL Server and trying to optimize SQL Server itself based on the knowledge we have acquired in the past 3 days. At the end of the day we have a better performing SQL Server which handles a larger workload than earlier.

I have already presented the shiny new content of the 4rd day at various conferences around the world, like the SQLPASS summit last year in Seattle in front of about 130 participants. Here is some feedback what you can expect:

  • “The presentation was like listening to a story, not just technical info. Even though it was demo based it had practical and application real-world applications.”
  • “Although I already knew a lot of the principles that was gone over, I also learned a lot that I did not know. It will be good to take the additional knowledge back to work and see if I can improve our production environment.”
  • “The speaker provided plenty of information and techniques for SQL Server performance troubleshooting that are not evident in standard SQL Server training and from available online information.”
  • “My notes have a bunch of TODOs for follow up research and implementation. Mostly it’s a bunch of settings to check and probably tweak to suit our environment, but I also found a couple new tools to check out.”
  • “Baselining is the key to troubleshooting. Solving a problem can lead to more opportunities and sometimes solving problems can create more problems, without a baseline you’ll never know.”

I’m running the workshop at the following locations and dates:

  • April 22 – 25, Vienna/Austria
  • May 13 – 16, Zurich/Switzerland,
  • May 27 – 30, Utrecht/Netherlands

You can find further information about the registration at http://www.SQLpassion.at/events.html. There is also an early-bird price available until the end of February. So don’t lose time and register and get an expert in SQL Server Performance Tuning & Troubleshooting!

Here is the detailed outline of the agenda for the 4 days.

  • Database Internals
    • Structures
    • Fixed Length Data Types
    • Variable Length Data Types
    • Sparse Columns
    • LOB Columns
    • Data Modifications
  • Execution Plans
    • Understanding Execution Plans
    • Physical Operators
    • Plan Generation & Caching
    • Plan Reuse & Recompilations
    • Plan Cache Pollution
    • Parameter Sniffing
  • Indexing
    • Table Scan/Index Scan/Index Seek
    • Clustered/Non-Clustered Indexes
    • Bookmark Lookups
    • Index Intersection
    • Filtered Indexes
    • Indexed Views
    • Page Fragmentation
    • Page Splits
    • Fill Factor
    • Searchable Arguments
    • Index Maintenance
  • Statistics
    • Overview
    • Working with Statistics
    • Multi-Column Statistics
    • Histogram/Density Vectors
    • Statistics Maintenance
  • Concurrency
    • Pessimistic Concurrency
    • Optimistic Concurrency
    • Isolation Levels
    • Troubleshooting Locking
    • Viewing Locks
    • Lock Granularity
    • Lock Hierarchy
    • Lock Escalations
    • Lock Compatibility
  • Deadlocking
    • Detection
    • Avoidance
    • Deadlocking Types
    • Troubleshooting
  • Latches & Spinlocks
    • Latch Architecture
    • Latch Types
    • Latch Modes
    • Hash Partitioning
    • Spinlocks
    • Troubleshooting
  • Performance Monitoring & Troubleshooting
    • Performance Monitoring Methology
    • PAL Tools
    • Windows Performance Monitor
    • SQL Server Profiler
    • Establishing a Baseline
    • Wait Statistics
    • OS & Storage Configuration
    • Database Configuration
    • Memory Management
    • Parallelism
    • TempDb

Thanks

-Klaus

SQL Server Quickie #5 – Bookmark Lookup Deadlocks

Today I have uploaded the 5th SQL Server Quickie to YouTube. This time Santa Claus talks about Bookmark Lookup Deadlocks in SQL Server. You can find the script used during the demonstration here as a download.


 

Thanks for watching!

-Santa Claus

Practical SQL Server Performance Troubleshooting Day

Due to the huge demand of SQL Server Troubleshooting Know How and Techniques I’m running on January 29 (Vienna/Austria) and January 31 (Zurich/Switzerland) my 1 day workshop called “Practical SQL Server Performance Troubleshooting Day” (see http://www.SQLpassion.at/events.html for further information).

During this 1 day workshop we will take a default installation of SQL Server, and run an OLTP workload with a several hundred users to generate our initial baseline for performance tuning/troubleshooting. Throughout the day we will work across various areas of SQL Server to implement different performance optimizations, and check how those changes will impact the throughput of our test workload. At the end of the day we will have a well-performing SQL Server which can handle a much larger workload than the initial (default) installation.

Following areas are covered in this workshop:

  • Windows OS Settings
  • Storage Configuration
  • SQL Server Instance Settings
  • Database Settings
  • Index/Statistics Maintenance
  • Locking/Blocking/Deadlocking
  • Memory Management

I have also presented this content in November at the SQLPASS Summit in Seattle/USA as a precon with around 130 attendees. Here is some feedback from them:

  • “The presentation was like listening to a story, not just technical info. Even though it was demo based it had practical and application real-world applications.”
  • “Although I already knew a lot of the principles that was gone over, I also learned a lot that I did not know. It will be good to take the additional knowledge back to work and see if I can improve our production environment.”
  • “The speaker provided plenty of information and techniques for SQL Server performance troubleshooting that are not evident in standard SQL Server training and from available online information.”
  • “My notes have a bunch of TODOs for follow up research and implementation. Mostly it’s a bunch of settings to check and probably tweak to suit our environment, but I also found a couple new tools to check out.”
  • “Baselining is the key to troubleshooting. Solving a problem can lead to more opportunities and sometimes solving problems can create more problems, without a baseline you’ll never know.”

So if you are also interested in improving your SQL Server performance/throughput, don’t hesitate and register at http://www.SQLpassion.at/events.html. There is also an early-bird price available until the end of this year.

See you soon!

-Klaus

SQL Server Quickie #4 – IAM Pages

Today I have uploaded the 4rd SQL Server Quickie to YouTube. In this episode I’m talking about IAM Pages in SQL Server. You can find here the script I have used during the demonstration as a download.

Thanks for watching!

-Klaus

Nested Partitioned Views

A few weeks ago I had a customer engagement, where the customer wanted to implement Nested Partitioned Views. Before we go into the details of this approach and if it could work, I want to give you an overview about the requirements, and why the customer wanted to have Nested Partitioned Views.

The main idea was that the customer had to implement their SQL Server database for both Standard and Enterprise Edition. With the Enterprise Edition the customer used a combination of Partitioned Views and Partitioned Tables, as you can see it from the following picture.


Some of you might ask, why the heck you want to implement such a scenario. The answer is easy: Partitioned Views itself have some disadvantages, and on the other hand Partitioned Tables also have some disadvantages. But when you combine then you will get the benefits from BOTH worlds. The following list describes the pros and cons of Partitioned Views in SQL Server.

  • Pros
    • Available on any Edition of SQL Server
    • Each table has its own better Statistics
    • Index Rebuilds of any table is an ONLINE operation (when done on Enterprise Edition)
    • Each table can be indexed independently
      • Operational Data
      • Historical Data
  • Cons
    • Lots of tables to administer
    • Indexes must be created on individual tables
    • Check Constraints are needed for Table Elimination
    • Gaps and overlapping values are possible

The following list describes the pros and cons of Partitioned Tables in SQL Server.

  • Pros
    • Only one table to administer
    • Gaps and overlapping values are NOT possible
    • Completely transparent
  • Cons
    • Available only on the Enterprise Edition of SQL Server
    • Table-Level Statistics
      • Less accurate on larger Partitioned Tables
      • Filtered Statistics can help here…
    • Partition Level Index Rebuilds are OFFLINE operations
      • Only the whole Partitioned Table can be rebuild ONLINE
    • Supports Partitioning only over a single column
      • Persisted Computed Columns are needed

As you can see from this list, there are a lot of disadvantages of Partitioned Tables, especially in the area of maintenance (less accurate Statistics, Partition Level Index Rebuilds). As you have seen in the previous figure, you can achieve a very powerful solution, when you combine Partitioned Views with Partitioned Tables.

Because the customer also had to support the database on the Standard Edition of SQL Server (where Partitioned Tables are not available), they just wanted to substitute the Partitioned Tables with Partitioned Views – so in the final solution you just have Partitioned Views inside Partitioned Views – just Nested Partitioned Views. The following picture illustrates this concept.


So the question was now, if this could be done with SQL Server? Of course, you can make it this way, but what’s about performance? SQL Server can use with a single Partitioned View Table Elimination when each participating table has a corresponding CHECK constraint defined. Therefore SQL Server only has to query the necessary tables – it’s almost the same concept as Partition Elimination with Partitioned Tables. But would Table Elimination work with Nested Partitioned Views? Because there is no chance to define a CHECK constraint on the inner Partitioned Views…

Before I give you the ultimate answer on that question, I want to walk through a complete scenario, how you can setup and implement Nested Partitioned Views as illustrated in the last picture. In the first step we are creating a new database, and adding 2 new file groups to it, where the data of 2007 and 2008 is stored.

USE master
GO

-- Create a new database
CREATE DATABASE VLDB ON PRIMARY 
(
	NAME = N'VLDB', 
	FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\VLDB.mdf',
	SIZE = 5072KB, 
	FILEGROWTH = 1024KB
)
LOG ON 
(
	NAME = N'VLDB_log',
	FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\VLDB_log.ldf',
	SIZE = 2048000KB, -- Initialize the log with 2GB, this gives us 16 VLFs
	FILEGROWTH = 10%
)
GO

-- Create a new file group for the 2007 sales data
ALTER DATABASE VLDB
ADD FILEGROUP Sales2007FG
GO

-- Add a new file to the previous created file group
ALTER DATABASE VLDB
ADD FILE
(
	NAME = 'Sales2007_Data',
	FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Sales2007_Data.ndf',
	SIZE = 300,
	FILEGROWTH = 10%
)
TO FILEGROUP Sales2007FG
GO

-- Create a new file group for the 2008 sales data
ALTER DATABASE VLDB
ADD FILEGROUP Sales2008FG
GO

-- Add a new file to the previous created file group
ALTER DATABASE VLDB
ADD FILE
(
	NAME = 'Sales2008_Data',
	FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Sales2008_Data.ndf',
	SIZE = 300,
	FILEGROWTH = 10%
)
TO FILEGROUP Sales2008FG
GO


In each file group we create a Partitioned View which subdivides the data into 4 separate tables for each quarter (Q1, Q2, Q3, Q4). The following listing shows how to setup the Partitioned View for the 2007 sales data.

-- Table for 2007 Q1
CREATE TABLE [Sales2007_Q1]
(
	DateKey DATETIME NOT NULL
		CONSTRAINT Sales2007Q1 -- The CHECK constraint is still needed to do Partition Elimination for the Partitioned View
			CHECK (DateKey >= '20070101' AND DateKey < '20070401'),
	OnlineSalesKey INT NOT NULL,
	SalesOrderNumber NVARCHAR(20) NOT NULL,
	SalesAmount MONEY NOT NULL
)
GO

-- Add a primary key clustered
ALTER TABLE [Sales2007_Q1]
ADD CONSTRAINT PK_Sales2007Q1 PRIMARY KEY CLUSTERED 
(
	DateKey,
	OnlineSalesKey
)
GO

-- Load data into the table for the year 2007/Q1
INSERT INTO [Sales2007_Q1] (DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount)
SELECT DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount FROM ContosoRetailDW.dbo.FactOnlineSales
WHERE DateKey >= '20070101' AND DateKey < '20070401'
GO

-- Table for 2007 Q2
CREATE TABLE [Sales2007_Q2]
(
	DateKey DATETIME NOT NULL
		CONSTRAINT Sales2007Q2 -- The CHECK constraint is still needed to do Partition Elimination for the Partitioned View
			CHECK (DateKey >= '20070401' AND DateKey < '20070701'),
	OnlineSalesKey INT NOT NULL,
	SalesOrderNumber NVARCHAR(20) NOT NULL,
	SalesAmount MONEY NOT NULL
)
GO

-- Add a primary key clustered
ALTER TABLE [Sales2007_Q2]
ADD CONSTRAINT PK_Sales2007Q2 PRIMARY KEY CLUSTERED 
(
	DateKey,
	OnlineSalesKey
)
GO

-- Load data into the table for the year 2007/Q2
INSERT INTO [Sales2007_Q2] (DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount)
SELECT DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount FROM ContosoRetailDW.dbo.FactOnlineSales
WHERE DateKey >= '20070401' AND DateKey < '20070701'
GO

-- Table for 2007 Q3
CREATE TABLE [Sales2007_Q3]
(
	DateKey DATETIME NOT NULL
		CONSTRAINT Sales2007Q3 -- The CHECK constraint is still needed to do Partition Elimination for the Partitioned View
			CHECK (DateKey >= '20070701' AND DateKey < '20071001'),
	OnlineSalesKey INT NOT NULL,
	SalesOrderNumber NVARCHAR(20) NOT NULL,
	SalesAmount MONEY NOT NULL
)
GO

-- Add a primary key clustered
ALTER TABLE [Sales2007_Q3]
ADD CONSTRAINT PK_Sales2007Q3 PRIMARY KEY CLUSTERED 
(
	DateKey,
	OnlineSalesKey
)
GO

-- Load data into the table for the year 2007/Q3
INSERT INTO [Sales2007_Q3] (DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount)
SELECT DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount FROM ContosoRetailDW.dbo.FactOnlineSales
WHERE DateKey >= '20070701' AND DateKey < '20071001'
GO

-- Table for 2007 Q4
CREATE TABLE [Sales2007_Q4]
(
	DateKey DATETIME NOT NULL
		CONSTRAINT Sales2007Q4 -- The CHECK constraint is still needed to do Partition Elimination for the Partitioned View
			CHECK (DateKey >= '20071001' AND DateKey < '20080101'),
	OnlineSalesKey INT NOT NULL,
	SalesOrderNumber NVARCHAR(20) NOT NULL,
	SalesAmount MONEY NOT NULL
)
GO

-- Add a primary key clustered
ALTER TABLE [Sales2007_Q4]
ADD CONSTRAINT PK_Sales2007Q4 PRIMARY KEY CLUSTERED 
(
	DateKey,
	OnlineSalesKey
)
GO

-- Load data into the table for the year 2007/Q4
INSERT INTO [Sales2007_Q4] (DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount)
SELECT DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount FROM ContosoRetailDW.dbo.FactOnlineSales
WHERE DateKey >= '20071001' AND DateKey < '20080101'
GO

-- Create the Partitioned View for 2007
CREATE VIEW Sales2007
AS
	SELECT * FROM Sales2007_Q1
	UNION ALL
	SELECT * FROM Sales2007_Q2
	UNION ALL
	SELECT * FROM Sales2007_Q3
	UNION ALL
	SELECT * FROM Sales2007_Q4
GO


As you can see from the listing, each individual table has a corresponding CHECK constraint, so that SQL Server can eliminate the tables which don’t have to be accessed, when we query our Partitioned View. The next listing shows how to setup the Partitioned View for the 2008 sales data.

-- Table for 2008 Q1
CREATE TABLE [Sales2008_Q1]
(
	DateKey DATETIME NOT NULL
		CONSTRAINT Sales2008Q1 -- The CHECK constraint is still needed to do Partition Elimination for the Partitioned View
			CHECK (DateKey >= '20080101' AND DateKey < '20080401'),
	OnlineSalesKey INT NOT NULL,
	SalesOrderNumber NVARCHAR(20) NOT NULL,
	SalesAmount MONEY NOT NULL
)
GO

-- Add a primary key clustered
ALTER TABLE [Sales2008_Q1]
ADD CONSTRAINT PK_Sales2008Q1 PRIMARY KEY CLUSTERED 
(
	DateKey,
	OnlineSalesKey
)
GO

-- Load data into the table for the year 2007/Q1
INSERT INTO [Sales2008_Q1] (DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount)
SELECT DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount FROM ContosoRetailDW.dbo.FactOnlineSales
WHERE DateKey >= '20080101' AND DateKey < '20080401'
GO

-- Table for 2008 Q2
CREATE TABLE [Sales2008_Q2]
(
	DateKey DATETIME NOT NULL
		CONSTRAINT Sales2008Q2 -- The CHECK constraint is still needed to do Partition Elimination for the Partitioned View
			CHECK (DateKey >= '20080401' AND DateKey < '20080701'),
	OnlineSalesKey INT NOT NULL,
	SalesOrderNumber NVARCHAR(20) NOT NULL,
	SalesAmount MONEY NOT NULL
)
GO

-- Add a primary key clustered
ALTER TABLE [Sales2008_Q2]
ADD CONSTRAINT PK_Sales2008Q2 PRIMARY KEY CLUSTERED 
(
	DateKey,
	OnlineSalesKey
)
GO

-- Load data into the table for the year 2008/Q2
INSERT INTO [Sales2008_Q2] (DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount)
SELECT DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount FROM ContosoRetailDW.dbo.FactOnlineSales
WHERE DateKey >= '20080401' AND DateKey < '20080701'
GO

-- Table for 2008 Q3
CREATE TABLE [Sales2008_Q3]
(
	DateKey DATETIME NOT NULL
		CONSTRAINT Sales2008Q3 -- The CHECK constraint is still needed to do Partition Elimination for the Partitioned View
			CHECK (DateKey >= '20080701' AND DateKey < '20081001'),
	OnlineSalesKey INT NOT NULL,
	SalesOrderNumber NVARCHAR(20) NOT NULL,
	SalesAmount MONEY NOT NULL
)
GO

-- Add a primary key clustered
ALTER TABLE [Sales2008_Q3]
ADD CONSTRAINT PK_Sales2008Q3 PRIMARY KEY CLUSTERED 
(
	DateKey,
	OnlineSalesKey
)
GO

-- Load data into the table for the year 2008/Q3
INSERT INTO [Sales2008_Q3] (DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount)
SELECT DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount FROM ContosoRetailDW.dbo.FactOnlineSales
WHERE DateKey >= '20080701' AND DateKey < '20081001'
GO

-- Table for 2008 Q4
CREATE TABLE [Sales2008_Q4]
(
	DateKey DATETIME NOT NULL
		CONSTRAINT Sales2008Q4 -- The CHECK constraint is still needed to do Partition Elimination for the Partitioned View
			CHECK (DateKey >= '20081001' AND DateKey < '20090101'),
	OnlineSalesKey INT NOT NULL,
	SalesOrderNumber NVARCHAR(20) NOT NULL,
	SalesAmount MONEY NOT NULL
)
GO

-- Add a primary key clustered
ALTER TABLE [Sales2008_Q4]
ADD CONSTRAINT PK_Sales2008Q4 PRIMARY KEY CLUSTERED 
(
	DateKey,
	OnlineSalesKey
)
GO

-- Load data into the table for the year 2008/Q4
INSERT INTO [Sales2008_Q4] (DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount)
SELECT DateKey, OnlineSalesKey, SalesOrderNumber, SalesAmount FROM ContosoRetailDW.dbo.FactOnlineSales
WHERE DateKey >= '20081001' AND DateKey < '20090101'
GO

-- Create the Partitioned View for 2008
CREATE VIEW Sales2008
AS
	SELECT * FROM Sales2008_Q1
	UNION ALL
	SELECT * FROM Sales2008_Q2
	UNION ALL
	SELECT * FROM Sales2008_Q3
	UNION ALL
	SELECT * FROM Sales2008_Q4
GO


After we have now setup the Partitioned Views for 2007 and 2008, we finally create another Partitioned View which just unions the data of the other 2 Partitioned Views together:

-- Create a Partitioned View on top of the 2 Partitioned Views
CREATE VIEW Sales
AS
	SELECT * FROM Sales2007
	UNION ALL
	SELECT * FROM Sales2008
GO


So now we have created our Nested Partitioned View: Partitioned Views within Partitioned Views. The final thing that we now have to check is, if SQL Server is able to do Table Elimination with our Nested Partitioned View. When we talk about Table Elimination (or Partition Elimination with Partitioned Table) we have to differentiate between 2 types of elimination:

  • Static Elimination
  • Dynamic Elimination

Static Elimination means that you provide within your query a static value. So during the compilation of the Execution Plan the Query Optimizer already knows which data we are accessing. In that case SQL Server generates an Execution Plan, which only references the relevant data. Look at the following query:

-- Shows static Table Elimination across the Partitioned View.
-- This SELECT accesses the following table:
-- => Sales2007_Q3
SELECT * FROM Sales
WHERE DateKey >= '20070721' AND DateKey <= '20070725'
GO


We are providing here hard coded values, so the Query Optimizer can sniff them, and generates an Execution Plan, which only accesses the Sales2007_Q3 table. So Static Elimination works without any problems with Nested Partitioned Views, as you can see it in the following Execution Plan:


Static Elimination is fine, if you are doing demos etc., but in a production database when you are calling your query with different values through parameters, SQL Server has to do Dynamic Elimination. Dynamic Elimination means that the Query Optimizer has to generate an Execution Plan, which can’t take into account during the compilation which tables you are accessing, because those tables are just determined during runtime – therefore the name Dynamic Elimination. So let’s imagine we are executing the following parameterized query:

DECLARE @from DATETIME = '20070721'
DECLARE @to DATETIME = '20070725'

SELECT * FROM Sales
WHERE DateKey >= @from AND DateKey <= @to
GO


In that case SQL Server can’t determine which data is accessed, because the referenced variables are runtime constructs, they are not available during the compilation phase of our query. The above query produces the following Execution Plan:


When you look at the first step on that plan, you might think that SQL Server has to seek into each table to get the corresponding records. The Query Optimizer estimates each Clustered Index Seek with around 12%. But that’s only the half-truth of that Execution Plan! The real power lies in the Filter Operator which comes before the Clustered Index Seek. When you look at the Tooltip Window of the Filter Operator inside SQL Server Management Studio, you can see that the operator has a so-called Startup Expression Predicate:


As you can see from the picture, the Startup Expression Predicate has the same value as the CHECK constraint on the table itself. So that Filter Operator only calls the Clustered Index Seek operator on the underlying table when the Startup Expression Predicate is evaluated to true! You can also cross-check this with the property Actual Executions on the Clustered Index Seek Operators:


In our case SQL Server only executed the Clustered Index Seek Operator on the Sales2007_Q3 table. On all other tables the Filter Operator prevented through the Startup Expression Predicate the execution of the Clustered Index Seeks! That’s a really powerful concept for Dynamic Table Elimination inside Partitioned Views.

As you can see from this example, Nested Partitioned Views are working without any problems in SQL Server, and on the other hand SQL Server uses a Startup Expression Predicate inside a Filter Operator to drive Dynamic Table Elimination when you work with Partitioned Views – it doesn’t matter here if those Views are nested or not. In that case it is really important that you check the whole Execution Plan to get a better understanding what SQL Server is actually doing for your query. When you just look on that plan (without going into the details), you might think that SQL Server has to access every table, but that’s not really true!

A big Thank You goes to Paul White (Weblog, Twitter) who helped me to understand that behavior and implementation strategy of SQL Server. You can find the whole sample of Nested Partitioned Views here for download.

Thanks for reading

-Klaus

SQL Server Quickie #3 – Allocation Units

Today I have uploaded my 3rd SQL Server Quickie to YouTube. In this episode I’m talking about Allocation Units in SQL Server.


 

Thanks for reading & watching

-Klausy

I’m a man, I’m a husband, I’m a father of 2 boys, and my life just sucks.

I’m a man, I’m a husband, I’m a father of 2 boys, and my life just sucks. Don’t prejudice too fast on me, and let me tell you why life just sucks because of these reasons.

I’m a father, married man, and our first son Philip was born 2.5 years ago on a cold Thursday morning in February. It was really an awesome feeling to hold your own baby in your hands. But you have to be aware of the fact, that you are a man, a father, and not a woman, and not the mother. After Philip’s birth, my wife and Philip had to stay a few more days in the hospital, just to check that everything is fine with them. Every day the nurse told me how I have to hold my baby (e.g. you have to stabilize the head, because there are no muscles there after the birth). They are telling you this every day, because you are a man, you can’t know these tiny little facts after their first “introduction”.

We are a very open family, which means it doesn’t bother for us, who is doing what in our household. I was staying up in the night, when Philip was crying, I have changed his swaddling bands, I have stayed with him, when he couldn’t sleep during the night, so that at least my wife was able to get some sleep. That’s the great part of being a father.

I’ve also done a lot of different activities alone with Philip, because after 1.5 years my wife started to work again for some hours during the week. We were going to a near lake, feeding pigeons, and we were doing various other things. I really liked this time – did I? Just imagine you have to change his swaddling bands while you are not at home. A wife just goes to a public toilet, and there is some space where you can change his swaddling bands without any interruptions and without any questions. But as a man, as a father you don’t have this option. Your only option is to the gent’s toilet, but how you will change his swaddling bands there? There are just 2 options: directly on the floor, or directly in the urinal… not very handy, trust me… The other more interesting option is just to use a woman’s toilet. Just imagine you are standing at the changing table in the woman’s toilet, your baby is full of shi…, its summer, you wear short trousers, a lady enters the room, and the first thing she sees are your hairy legs… “Oh my god, there’s a man, there’s a man, I need help, I need help!!!!” You reaction: take your baby as fast as possible and run, run, – run for your life!

I’ve also visited with Philip every Monday for 1.5 hours a baby gaming round. I think it’s almost the same as Tubber parties, but just with babies. But there is one important difference: you are a man, a father, in a round of mothers. They are looking at you surprisingly, they are thinking how the hell he should handle that baby, when the swaddling bands are full of shi… etc. To get their acceptance there is only one option: you have to join their discussions. Easy isn’t it? They are talking about their latest lingerie shopping trips, about their latest love toys they have acquired, and how bad their last night was because of their awkwardly mans. So you will remain the outsider…

Normally you will say, why the heck you should care of these little things, you are a man, you are a father, just enjoy your life with your dears. But today my whole opinion just changed with one small sentence. But let’s start from the beginning. Since Monday this week Philip goes to the kindergarten – for 3 hours after the morning until lunch. Our second son Daniel was born exactly 4 weeks ago. For that reason, my wife and I have decided that we want to bring Philip together to the kindergarten for the first days, so that he is not in troubles when my wife or I are bringing him alone to the kindergarten. Sometimes our nights are currently a little bit challenging because of Daniel, therefore our plan was that I’m bringing him alone to the kindergarten after such nights, as long as my travel schedule permits it.

There is also a so-called familiarization phase of one month at the kindergarten, so that the kids are phased into that new world as smooth as possible. So their general rule of thumb is that only one part (mother or father) should bring the kid to the kindergarten. But we were going both, because my wife can’t just go with Philip AND Daniel, who is just 4 weeks old. His swaddling bands are ALWAYS full of shi…, and he is always hungry, and my wife is feeding him. So the only option to support my wife is that I’m coming along with her and help were I can. We have done this yesterday for the first time, and everything was fine. But today’s morning was different!

When we entered the kindergarten, the female director of the kindergarten was coming straight to my wife, and started talking to her: “Hello Mrs. Aschenbrenner. You know it’s not allowed and permitted that you and your “man” (her finger was pointing to me in the background) are coming both to the kindergarten? You also have to follow our rules. There are no exceptions to this.” I just though OMG…

Then she looked directly at me and said the following sentence directly into my eyes: “Fathers are not welcomed during the familiarization phase, because the mother is the psychological parent of the child.” Just for my German readers: “Väter sind nicht erwünscht während der Eingewöhnungsphase, weil die Mutter die Bezugsperson des Kindes ist“.

I’m a man, I’m a husband, I’m a father of 2 boys, and my life just sucks.

-Klaus

My upcoming speaking schedule

The summer goes by and in a few weeks the conference season starts over again. The last weeks were a little bit stressful, but now I had finally some time to prepare my sessions for upcoming SQL Server conferences, where I’m speaking over the next months.

24 Hours of PASS, September 12

At this online event (http://www.sqlpass.org/24hours/fall2012) I’m doing one session titled “Fast your Seatbelt – Troubleshooting the hardest SQL Server Problems“. I’m talking here about things like Thread Pool Starvation, Spinlock Contention, and VMware Memory Balloon Driver issues – expect to be this a Level 400 session.

SQLdays, September 20 – 21, Rosenheim/Germany

At this German speaking conference (http://www.sqldays.net) I’m doing 3 sessions about various SQL Server topics:

  • Troubleshooting SQL Server with Extended Events
  • Fast your Seatbelt – Troubleshooting the hardest SQL Server Problems
  • Migrating to SQL Server 2012 AlwaysOn

PASS SQLRally Nordic, October 1 – 3, Copenhagen/Denmark

It’s my first time that I’m speaking at a SQLRally event (http://www.sqlpass.org/sqlrally/2012/nordic), and I’m really looking forward going to Denmark – I’ve never been there! In Copenhagen I’m presenting one session about Troubleshooting SQL Server with Extended Events.

SQLPASS Summit, November 6 – 9, Seattle/USA

It’s now my 6th SQLPASS Summit where I’m presenting (2006, 2007, 2008, 2010, 2011, 2012) and I’m really proud to be able to do a day long precon about Practical SQL Server Performance Troubleshooting. In this precon I’m using a TPC-E workload to generate an initial baseline for performance improvements, and over the day I’m showing various techniques to improve SQL Server performance. But read yourself through the precon abstract:

Learn how to configure the Windows OS, SQL Server, and your database for optimal performance. In this pre-conference workshop, we’ll start by taking a default installation of SQL Server and running an OLTP workload with several hundred users to generate our initial baseline for performance tuning and troubleshooting. Throughout the day, we’ll work with various areas of SQL Server to implement different performance optimizations and then see how those changes impact the throughput of our initial test workload. At the end of the day, we’ll have a well-performing SQL Server that can handle a much larger workload than the initial (default) installation.

Topics for the day include:
• Windows OS settings
• Storage configuration
• SQL Server instance settings
• Database settings
• Index and statistics maintenance
• Locking, blocking, and deadlocking
• Memory management

Besides the precon I’m presenting the following 2 regular conference sessions:

  • Advanced SQL Server Troubleshooting
  • ColumnStore Indexes – the Turbobooster in SQL Server 2012

Besides all this conference engagements, I’m also running my “SQL Server Performance Tuning & Troubleshooting Workshop” in Austria, Germany, Switzerland, and Ireland! See http://www.SQLpassion.at/events.html for further details. You can also expect some new SQL Server Quickies over the next weeks at my YouTube channel: http://www.youtube.com/user/sqlpassion. Autumn 2012 will be a very busy for me (the same as last year), and I’m looking forward to see you at one of these great SQL Server events!

Thanks for reading

-Klaus