You are on page 1of 9

Lecture 2 �-

Creating ·-·

a Partitioned Table
""''"�•••J2c-,..,..,. ,,_.,.,..,,.-,..,�,.___,..,,,_ . ....,..,,,,,.�..:,,,.,,,__,.3,,,,,.,,..,.,.;.iiii.:�--:,,i�....lf±'-"·"".<•;..;:•,. ··.. ;-�,.,,..,.,. ___,._
.;,;;;,;..,c-· "-"' >c-::-<!.,_._�..x•>-� .,... ..._..,,__,...,_,.L, ;, ,,.,; f

Dr. Mohammed Zayed-

r
When the volume of data in a table increases and it takes time to query the data, you can
partition the tables and store different parts of the tables in multiple physical locations
based on a range of values for a specific column. This helps in managing the data and
improving the query performance.
Consider the example of a manufacturing organization. The details of inventory
movements are stored in the Inventory Issue table. The table contains a large volume of
data and the queries take a lot of time to execute thereby slowing the report generation
process.

To improve the query performance, you can partition the table to divide the data based on
a condition and store different parts of the data in different locations. The condition can
be based on the date of transc;1.ction and you can save the data pertaining to five years at a
location·. After partitioning the table, data can be retrieved directly from a particular
partition by mentioning the partition number in the query.

In the preceding example, the partitioned tables were created after the database had
already been designed. You can also create partitioned tables while designing the database
and creating tables. You can plan to create a partitioned table when you know that the
data to be stored in the table-will be large. For example, if you are creating a database for
?- banking application and you know that the transaction details will be voluminous, you
can create the transaction tables with aiiitions.

Managing Databases and Tables 4.23


r7DNote
Part Woning a table is only oil uwed in En ferprise Edi lion of SQL Se,ver 2 005.
w: s..:::::t=: I'

To create a partitioned table, perfonn the following tasks:


1. Create a partition function.
2. Create a partition scheme.
3. Create a table by using the partition scheme.

For ex.ample, the AdventureWorks database stores the data of all the employees for the
last 11 years. This data includes the personal details of the employees and their payment
rates. Whenever there is a change in the payment rate of an employee, it is recorded in a
separate record. However, this results in generation of large volume of data. This
adversely affects the query performance. To improve the query performance, the database
developer needs to partition the table storing the changes in wage rate.

Creating a Partition Function

A pa1tition function specifies how a table should be partitioned. It specifies the range of
values on a particular column, based on which the table is partitioned. For example, in the
scenario of Adventure Works, you can partition the data based on years. The following
statement creates a partition function for the same:

CREATE PARTITION FUNCTION RateChngDate datetime)


AS RANGE RIGHT FOR VALUES (' 1996-01-01', '2000-01-01', '2004-01-01',
'2008-01-01')

The preceding query creates a partition function named RateChngDate. It specifies that
the data pertaining to the changes in the payment rate will be partitioned based on the
years.

Creating a Partition Scheme

After setting the partition function, you need to create the partition scheme. A partition
scheme associates a partition function with various filegroups resulting in the physical
layout of the data. Therefore, before creating a partition scheme, you need to create
filegroups.

4.24 Managing Databases and Tables ©NIIT


To create partition filegroups, you need to perform the following steps:
1. Expand the Databases folder in the Object Explorer window and right-click the
Adventure-Works. database.
2. Select the Properties option from the short-cut menu to display the Database
Properties - Adventure Works window, as shown in the following figure.
8 Database PJ-operties -AdventureWorlcs . __
• , �13
��Sa�� '?H�­
eneial
1�Files
. §l F degroup$
� Options
lh__�J
fEl Backup
L.:.__ __. -
� Permissions j' L�.! L'�'¾,,se B"'-' ""
P Extended PlOl)elties j· la, D<01;,t..,_,, !.vg !;:,.-=! '--'?
£ MH101ing -j!3 Oatal!ase

1.
!fr T u,nsactiCR'l Log Ship..,-,g
I �-•..t"'-
Vhr,,, l�H4fl .... v.:,di:.ittttiAgg;.11 =:!I
1"

(,�c; C,t:"�1.:d i.'1•! •2<)J6 ,, 4.').1£)(,}-..'


4i( 9,1 �16
I :'.,cc.
j.,;. '- r ,,.,. -'"'"I 'lb!• ·394F;,;,g
humttei ·l U..:ei! 9
'.!3 Maintenance
Co1!.1?\i-)}')

Se,ver:
SQLSEflVEROl
Coonection:
sa
�.f View connection properties

The Database Properties -Adventure Works Window


3. Select the Filegroups folder from Select a page pane to display the list of all the
filegroups in AdventureWorks.

©NIIT Managing Databases and Tables 4.25


4. Click the Add button to add a file group. Specify the name of the filegroup in the
Name text box as Old, as shown in the following figure.

r
I_; Er!tended Properties
0

; -<:r Mirroring
! · ,q, Transaction Log Shipping

Adding a Filegroup in the Database

4.26 Managing Databases and Tables ©NIIT


5. Repeat Step 4 to add four more filegroups named First, Second, Third, and Fourth,
as shown in the following figure.
I DatabaseProperties - AdventureWorks ·
1!!1Ji113
� General
.i Files
Name, Files Read-Only Default
,ff} Fdegtoups
� Options PRIMARY P'
'..1r Permissions Old 0 r r
,,.
·.� Extended Properties First 0 r r
].!::.I ,,111orrng r r
---
Second 0
1�
·�

Transaction Log Shipping


Thud 0 r -+-
0
--· r

1
Ser,.,er:
SQLSeiver01
l saConnection:
] � View connection p10pe1ties

Md

OK I Cancel I /4
Adding All Filegroups in the Database
6. Select the Files folder from Select a page pane to display the list of all the files.

©NUT Managing Databases and Tables 4.27


7. Click the Add button and type the name of the file as Old File in the Logical Name
text box, and select Old from the Filegroup drop-down list, as shown in the
following figure.

I Database Properties - Adventu�eW�rks lllJllil f3


rn __ _
..,......,.---
l(oi,lli,..,ii.aili.ii..•;.__.-------1£3 Script y Help
� General
_jl Files
Database n1ime:
..ff Filegroups
� Options Owner:
� Permissions
.!::f Extended Properties P-
Use full-text indexing
.5' Mirroring
� Transaction Log Shipping Database fries:
-
Logical Name File Type Filegro•Jp lritfal Size (MB)
t>.dventureW_ Data PRIMARY 177
Adventure\v'... Log Not Applicable 2
OldFil� Data Old 2

Server:
SQLServer01
Connection:
sa
:Ji View connection properties

Add

Creating a File Related to a Filegroup

4.28 Managing Databases and Tables· ©:NIIT


8. Repeat Step 7 to create four files names Filel, File2, File3, and File4, select
filegroup as First, Second, Third, and Fourth for the files, as shown in the
following figure.

I Database Properties -AdventureWorks . l!llliI£1


� General
� Files
!fl f.i�roups
.fl Options Owner:
� Permissions
. .ff Extended Properties W Use f•Jll·lex\.indel(ing

l
1
.? Mi11orino -�
'!fl Transaction Log Shipping Database. tiles:
Logical Name. file Type
,
l Eileg1oup )Initial Size U"1B1, j Autogrow\h
Adventure\N'... Data PRIMA.RY 177 El}• 16 MB. unrestricted growth
.b.dventure\.1/... log Not .A.ppficable 2 By 16 MB, restricted growth to 2U.
OldFile Da\a Old 2 By 1 MB, unrestricted growth
File1 Data First 2 By 1 MB, unrestricted grow\h
File2 Data Second 2 By 1 MB, unresllicted growth
1 Data By 1 MB, unrestricted growth
1 File3
Data By 1 MB, unreslticled growth
File4

;:: � � • �1
'�•
����

.IL"' "- �""'""..,.,.. .


Connection:
sa

'r#n. Read}• ►�
--,;.:.-,,_,

f
'
!
Add
,.

l
J
I
OK Cancel j ']
4l
Files Added to the Filegroups
9. Click the OK button to close the Database Properties - AdventoreWorks window.
10. Execute the following statements in the Microsoft SQL Server Management
Studio window to create the partition scheme:

CREATE PARTITION SCHEME RateChngDate


AS PARTITION RateChngDate·
TO (Old, First, Second, Third, Fourth)

©NIIT Managing Databases and Tables 429


Creating a Table by Using the Partition Scheme

After you create a pmtition function and a partition scheme, you need to create a table that
wi 11 store the partition records. You can use the following statements for the same:

Create Table EmpPayHistPart

EmployeeID int,
RateChangeDate datetime,­
Rate money,
PayFrequency tinyint,
ModifiedDate datetime
)ON RateChngDate(RateChangeDate)

In the preceding statement, the RateChngDate refers to the partition scheme that is
applied to the RateChangeDate column. The records entered in the EmpPayHistPart table
will be stored based on the condition specified in the partition function.

4.30 Managing Databases and Tables ©NIIT

You might also like