Tuesday, January 25, 2011
How SQL Server stores indexes on variable length columns
In the last months I had done a lot of performance tuning workshops, and there is almost the same question: How SQL Server stores indexes on variable length columns? Therefore I thought this would be a great topic for a weblog posting. To examine the concrete storage details of an index defined on a variable length column, I have created the following table and populated it with 80.000 records:
-- Create a new table
CREATE TABLE Customers
(
CustomerName VARCHAR(255) NOT NULL PRIMARY KEY,
Filler CHAR(138) NOT NULL
)
GO
CREATE TABLE Customers
(
CustomerName VARCHAR(255) NOT NULL PRIMARY KEY,
Filler CHAR(138) NOT NULL
)
GO
-- Insert 80.000 records
DECLARE @i INT = 1
WHILE (@i <= 80000)
BEGIN
INSERT INTO Customers VALUES
(
'CustomerName' + CAST(@i AS VARCHAR),
'Filler' + CAST(@i AS VARCHAR)
)
SET @i += 1
END
GO
DECLARE @i INT = 1
WHILE (@i <= 80000)
BEGIN
INSERT INTO Customers VALUES
(
'CustomerName' + CAST(@i AS VARCHAR),
'Filler' + CAST(@i AS VARCHAR)
)
SET @i += 1
END
GO
As you can see I have created the PRIMARY KEY constraint on a VARCHAR(255) column, which is enforced by a UNIQUE CLUSTERED INDEX by SQL Server. In the next step I have retrieved some physical information about the created clustered index by using the DMF sys.dm_db_index_physical_stats:
-- Retrieve physical information about the clustered index
SELECT * FROM sys.dm_db_index_physical_stats
(
DB_ID('VariableClusteredKeyStructure'),
OBJECT_ID('Customers'),
NULL,
NULL,
'DETAILED'
)
GO
SELECT * FROM sys.dm_db_index_physical_stats
(
DB_ID('VariableClusteredKeyStructure'),
OBJECT_ID('Customers'),
NULL,
NULL,
'DETAILED'
)
GO
When you look into the output, you can see that the column min_record_size_in_bytes show you a value of 7 and the column max_record_size_in_bytes shown you the value 28 in the index page. This leads us to the conclusion that the clustered keys are stored as variable length columns inside an index record. So let’s examine an index record on an index page. I’ve used the DBCC IND command to retrieve all pages for our clustered index and stored that output in a little helper table.
-- Create a helper table
CREATE TABLE HelperTable
(
PageFID TINYINT,
PagePID INT,
IAMFID TINYINT,
IAMPID INT,
ObjectID INT,
IndexID TINYINT,
PartitionNumber TINYINT,
PartitionID BIGINT,
iam_chain_type VARCHAR(30),
PageType TINYINT,
IndexLevel TINYINT,
NextPageFID TINYINT,
NextPagePID INT,
PrevPageFID INT,
PrevPagePID INT,
PRIMARY KEY (PageFID, PagePID)
)
GO
CREATE TABLE HelperTable
(
PageFID TINYINT,
PagePID INT,
IAMFID TINYINT,
IAMPID INT,
ObjectID INT,
IndexID TINYINT,
PartitionNumber TINYINT,
PartitionID BIGINT,
iam_chain_type VARCHAR(30),
PageType TINYINT,
IndexLevel TINYINT,
NextPageFID TINYINT,
NextPagePID INT,
PrevPageFID INT,
PrevPagePID INT,
PRIMARY KEY (PageFID, PagePID)
)
GO
-- Write everything in a table for further analysis
INSERT INTO HelperTable EXEC('DBCC IND(VariableClusteredKeyStructure, Customers, 1)')
GO
INSERT INTO HelperTable EXEC('DBCC IND(VariableClusteredKeyStructure, Customers, 1)')
GO
-- Retrieve the root index page (1 page)
SELECT * FROM HelperTable
WHERE IndexLevel = 2
GO
SELECT * FROM HelperTable
WHERE IndexLevel = 2
GO
In my case SQL Server stored the index root page on the page id 458, which I have dumped out through the DBCC PAGEcommand (after enabling the trace-flag 3604 to get the DBCC PAGE output):
DBCC TRACEON (3604)
GO
GO
-- Dump out the root index page
DBCC PAGE(VariableClusteredKeyStructure, 1, 458, 1)
GO
DBCC PAGE(VariableClusteredKeyStructure, 1, 458, 1)
GO
Each slot in the output represents one index record, like:
26 95020000 0100 0100 1b00 43757374 6f6d6572 4e616d65 31333533
Let’s examine those hex values.
- 26: The first byte represents status bits.
- 95020000: The next 4 bytes are the child-page-id to which this index record points
- 0100: The next 2 bytes are the child-file-id to which this index record points
- 0100: The next 2 bytes are the number of variable length columns
- 1b00: For each variable length column SQL Server stores a 2 byte entry which points to the offset at which the variable length column ends – it’s the same as when you store variable length columns on a regular data page. In this case we have one variable length column, therefore SQL Server has to store one 2 byte offset – byte offset 27 in this case. This means that the next bytes until the byte offset 27 are part of our variable length column – the clustered key.
- 43757374 6f6d6572 4e616d65 31333533: The hex value for our clustered key – the column CustomerName
As you can see from this explanation SQL Server stores variable length index columns in the same format as regular variable length columns on data pages. But you have to be aware that there is a slight overhead with this, because you need 2 additional bytes for storing the count of variable length columns and for each variable length column you need 2 bytes in the variable column offset array. Keep this in mind when you are designing your indexes and when you calculate how many index records fits onto one index page. You can download the whole script from here and play around with it.
-Klaus
.NET Web Services
Web services are small units of code built to handle a limited task.
What are Web Services?
- Web services are small units of code
- Web services are designed to handle a limited set of tasks
- Web services use XML based communicating protocols
- Web services are independent of operating systems
- Web services are independent of programming languages
- Web services connect people, systems and devices
Small Units of Code
Web services are small units of code designed to handle a limited set of tasks.
An example of a web service can be a small program designed to supply other applications with the latest stock exchange prices. Another example can be a small program designed to handle credit card payment.
Benefits of Web Services
- Easier to communicate between applications
- Easier to reuse existing services
- Easier to distribute information to more consumers
- Rapid development
Web services make it easier to communicate between different applications. They also make it possible for developers to reuse existing web services instead of writing new ones.
Web services can create new possibilities for many businesses because it provides an easy way to distribute information to a large number of consumers. One example could be flight schedules and ticket reservation systems.
5 Websites to Edit PDF files Online for free
But it is also said that PDF files can’t be edited, thats just a old story. There are many free softwares and even free Online tools to edit the PDF files of yours or from the third party. So in this post here are 5 best websites where you can easily edit the PDF files for free.
PDFfiller
PDFvue
PDFescape
FillanyPDF
PDFhammer
20 Web Services To Send Free Fax Online without the Fax Machine
Here is the list of 20 free web services that allow users to send faxes via internet connection. Most of the internet faxing services are totally free. But some services are supported by the advertisements on the coverpage of the faxes sent due to which the faxes may look unprofessional.