You are on page 1of 23

Module 6

Implementing Table
Structures in
SQL Server 2012

Module Overview
SQL Server Table Structures
Working with Clustered Indexes
Designing Effective Clustered Indexes

Lesson 1: SQL Server Table Structures


What is a Heap?
Operations on Heaps
Forwarding Pointers
What is a Clustered Index?
Operations on Clustered Indexes
Unique vs. Non-Unique Clustered Indexes
Demonstration 1A: Rebuilding Heaps

What is a Heap?
A table with:
No specified order for pages within the table
No specified order for data within each page
Data that is inserted or modified can be placed anywhere

within the table


id

index_id=0

first_iam_page

IAM Page

Heap
Heap
Data Pages

Operations on Heaps
INSERT

Each new row can be placed in the first available page with
sufficient space

UPDATE

The row can either remain on the same page if it still fits,
otherwise, it can be removed from the current page and
placed on the first available page with sufficient space

DELETE

Frees up space on the current page

Data is not overwritten, space is just flagged as available for


reuse

SELECT

Entire table needs to be read for most queries, if no indexes


are available

Forwarding Pointers
Forwarding
Forwarding pointers
pointers are
are references
references left
left at
at the
the original
original location
location
of
of a
a row,
row, when
when the
the row
row has
has been
been moved.
moved.
Data modifications in heaps can leave forwarding pointers

Row IDs in other indexes do not need to be updated

Can lead to performance issues over time

Only a single forwarding pointer is used

Performance would be improved by removing forwarding

pointers and updating other indexes

No easy option for this prior to SQL Server 2008

SQL Server 2008 and later introduced ability to rebuild a table


(including a heap)

ALTER TABLE <tablename> WITH REBUILD

What is a Clustered Index?


Table pages stored in logical order
Rows stored in logical order within table pages
Single clustered index per table

id

index_id=1

root_page

Root Index
Page

Index Pages

Data Pages

Operations on Clustered Indexes


INSERT

Each new row must be placed into the correct logical position

May involve splitting pages of the table

UPDATE

The row can either remain in the same place if it still fits and if the
clustering key value is still the same

If the row no longer fits on the page, the page needs to be split

If the clustering key has changed, the row needs to be removed


and placed in the correct logical position within the table

DELETE

Frees up space by flagging the data as unused

SELECT

Queries related to the clustering key can seek

Queries related to the clustering key can scan and avoid sorts

Unique vs. Non-Unique Clustered Indexes


Clustered indexes can be
Unique
Non-Unique
SQL Server must be able to identify individual rows
Adds a uniqueifier (4 bytes long) when needed for

non-unique indexes

Always specify indexes as unique if they are

Demonstration 1A: Rebuilding Heaps


In this demonstration you will see how to:

Create a table as a heap

Check the fragmentation and forwarding pointers for a heap

Rebuild a heap

Lesson 2: Working with Clustered Indexes


Creating Clustered Indexes
Dropping a Clustered Index
Altering a Clustered Index
Incorporating Free Space in Indexes
Demonstration 2A: Clustered Indexes

Creating Clustered Indexes


Can be created by specifying PRIMARY KEY on table
Can be created directly
CREATE
CREATE TABLE
TABLE dbo.Article
dbo.Article
(( ArticleID
ArticleID int
int IDENTITY(1,1)
IDENTITY(1,1) PRIMARY
PRIMARY KEY,
KEY,
ArticleName
ArticleName nvarchar(50)
nvarchar(50) NOT
NOT NULL,
NULL,
PublicationDate
PublicationDate date
date NOT
NOT NULL
NULL
);
);
CREATE
CREATE TABLE
TABLE dbo.LogData
dbo.LogData
(( LogID
LogID int
int IDENTITY(1,1),
IDENTITY(1,1),
LogData
LogData xml
xml NOT
NOT NULL
NULL
);
);
ALTER
ALTER TABLE
TABLE dbo.LogData
dbo.LogData
ADD
ADD CONSTRAINT
CONSTRAINT PK_LogData
PK_LogData
PRIMARY
PRIMARY KEY
KEY (LogId);
(LogId);
CREATE
CREATE CLUSTERED
CLUSTERED INDEX
INDEX CL_LogTime
CL_LogTime
ON
ON dbo.LogTime(LogTimeID);
dbo.LogTime(LogTimeID);

Dropping a Clustered Index


Drop an external index via DROP INDEX
Drop a PRIMARY KEY constraint via ALTER TABLE

May not be possible where foreign key references exist

DROP
DROP INDEX
INDEX CL_LogTime
CL_LogTime ON
ON dbo.LogTime;
dbo.LogTime;
ALTER
ALTER TABLE dbo.LogData
dbo.LogData
DROP CONSTRAINT PK_LogData;
PK_LogData;

Altering a Clustered Index


Some modifications permitted via ALTER INDEX

Can REBUILD or REORGANIZE

Can DISABLE

Cannot modify the key columns of the index

CREATE INDEX WITH DROP_EXISTING can do this

ALTER
ALTER INDEX
INDEX CL_LogTime
CL_LogTime ON
ON dbo.LogTime
dbo.LogTime
REBUILD;
REBUILD;
ALTER
ALTER INDEX
INDEX ALL
ALL ON
ON dbo.LogTime
dbo.LogTime
REBUILD;
REBUILD;
ALTER
ALTER INDEX
INDEX ALL
ALL ON
ON dbo.LogTime
dbo.LogTime
REORGANIZE;
REORGANIZE;

Incorporating Free Space in Indexes


Free space can be left in indexes, including clustered indexes

FILLFACTOR

PAD_INDEX

Free space can improve performance of certain operations


Default value can be changed using sp_configure

ALTER
ALTER TABLE
TABLE Person.Contact
Person.Contact
ADD
ADD CONSTRAINT
CONSTRAINT PK_Contact_ContactID
PK_Contact_ContactID
PRIMARY
PRIMARY KEY
KEY CLUSTERED
CLUSTERED
(
(
ContactID
ContactID ASC
ASC
)
) WITH
WITH (PAD_INDEX
(PAD_INDEX =
= OFF,
OFF, FILLFACTOR
FILLFACTOR = 70);
70);
GO
GO

Demonstration 2A: Clustered Indexes


In this demonstration you will see how to:

Create a table with a clustered index

Detect fragmentation in a clustered index

Correct fragmentation in a clustered index

Lesson 3: Designing Effective Clustered Indexes


Characteristics of Good Clustering Keys
Appropriate Data Types for Clustering Keys

Characteristics of Good Clustering Keys


Good clustering keys have specific properties

Short

Static

Increasing (not necessarily monotonically)

Unique

Limits on clustering keys

16 columns

900 bytes

Appropriate Data Types for Clustering Keys

Data Type

Comments

int

Good candidate particularly if IDENTITY

bigint

Good candidate particularly if IDENTITY

uniqueidentifier Some concerns with size, big concern with


lack of increasing values
varchar

Concerns with size, concerns with sorting


performance, concerns with static nature

date

Concern with uniqueness but excellent for


range queries

smalldatetime

Concern with uniqueness

Lab 6: Implementing Table Structures in SQL Server


Exercise 1: Creating Tables as Heaps
Exercise 2: Creating Tables with Clustered Indexes
Challenge Exercise 3: Comparing the Performance of

Clustered Indexes vs. Heaps (Only if time permits)

Logon information

Virtual machine

10776A-MIA-SQL1

User name

AdventureWorks\Administrator

Password

Pa$$w0rd

Estimated time: 45 minutes

Lab Scenario
One of the most important decisions when designing a table
is to choose an appropriate table structure. In this lab, you
will choose an appropriate structure for some new tables
required for the relationship management system.

Lab Review
When is it important that a clustered index has an

increasing key?

Which table structure is automatically assigned when a

table is assigned a primary key during the table creation,


without specifying a structure?

Module Review and Takeaways


Review Questions
Best Practices

You might also like