Professional Documents
Culture Documents
Creating ·-·
a Partitioned Table
""''"�•••J2c-,..,..,. ,,_.,.,..,,.-,..,�,.___,..,,,_ . ....,..,,,,,.�..:,,,.,,,__,.3,,,,,.,,..,.,.;.iiii.:�--:,,i�....lf±'-"·"".<•;..;:•,. ··.. ;-�,.,,..,.,. ___,._
.;,;;;,;..,c-· "-"' >c-::-<!.,_._�..x•>-� .,... ..._..,,__,...,_,.L, ;, ,,.,; f
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.
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.
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:
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.
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.
1.
!fr T u,nsactiCR'l Log Ship..,-,g
I �-•..t"'-
Vhr,,, l�H4fl .... v.:,di:.ittttiAgg;.11 =:!I
1"
Se,ver:
SQLSEflVEROl
Coonection:
sa
�.f View connection properties
r
I_; Er!tended Properties
0
; -<:r Mirroring
! · ,q, Transaction Log Shipping
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.
Server:
SQLServer01
Connection:
sa
:Ji View connection properties
Add
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
'�•
����
'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:
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:
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.