Looking
to optimize the performance of your SQL Server database? If your database
contains very large tables, you may benefit from partitioning those tables onto
separate filegroups. This technology, introduced in SQL Server 2005, allows you
to spread data onto different physical disks, leveraging the concurrent
performance of those disks to optimize query performance.
Partitioning
a SQL Server database table is a three-step process:
- Create the partition function
- Create the partition scheme
- Partition the table
Step
1: Creating a Partition Function
The
partition function defines how you want SQL Server to partition the data. At
this point, we’re not concerned with any particular table, we’re just
generically defining a technique for splitting data.
We
define the partitions by specifying the boundaries of each partition. For
example, suppose we have a Employee table that contains information on all of
our Employees, identified by a unique customer number, ranging from 1 to
1,000,000. We might decide to partition that table into four equally spaced
partitions, using the following partition function (I’ll call it Emp_partfunc):
CREATE PARTITION FUNCTION Emp_partfunc (int)
AS RANGE RIGHT FOR VALUES (250000, 500000, 750000)
These
boundaries define four partitions. The first contains all values less than
250,000. The second contains values between 250,000 and 499,999. The third
contains values between 500,000 and 749,999. All values greater than or equal
to 750,000 go in the fourth partition.
Notice
that we used the "RANGE RIGHT" clause in this example. This indicates
that the boundary value itself should go in the partition on the right side.
Alternatively, if I had used "RANGE LEFT", the first partition would
have included all values less than or equal to 250,000; the second partition
would have included values between 250,001 and 500,000, and so on.
Step
2: Creating a Partition Scheme
Once
you have a partition function describing how you want to split your data, you
need to create a partition scheme defining where you want to partition it. This
is a straightforward process that links partitions to filegroups. For example,
if I had four filegroups named "fg1" through "fg4", the
following partition scheme would do the trick:
CREATE PARTITION SCHEME Emp_partscheme AS PARTITION Emp_partfunc TO (fg1, fg2, fg3, fg4)
CREATE PARTITION SCHEME Emp_partscheme AS PARTITION Emp_partfunc TO (fg1, fg2, fg3, fg4)
Notice
that we now link a partition function to the partition scheme, but we still
haven’t linked the partition scheme to any specific database table. That’s
where the power of reusability comes into play. We could use this partition
scheme (or just the partition function, if we desired) on any number of
database tables.
Step
3: Partitioning a Table
After
defining a partition scheme, you’re now ready to create a partitioned table.
This is the simplest step of the process. You simply add an "ON"
clause to the table creation statement specifying the partition scheme and the
table column to which it applies. You don’t need to specify the partition
function because the partition scheme already identifies it.
For
example, if you wanted to create a Employee table using our partition scheme,
you would use the following Transact-SQL statement:
CREATE TABLE Employee (FirstName nvarchar(40), LastName nvarchar(40), CustomerNumber int) ON customer_partscheme (CustomerNumber)
CREATE TABLE Employee (FirstName nvarchar(40), LastName nvarchar(40), CustomerNumber int) ON customer_partscheme (CustomerNumber)
That’s
everything you need to know about partitioning tables in Microsoft SQL Server!
Remember to leverage the power of reusability by writing generic partition
functions and partition schemes that might apply to multiple tables!
About Author:
Bhanu Prakash is budding technology geek, who actively contributes to Systems Plus with his creativity and research on technology. He can be contacted at bhanu.p@spluspl.com
ReplyDeleteTHANK YOU FOR THE INFORMATION
PLEASE VISIT US
Seo Consultant Services