• What is a database table partitioning?

    Partitioning is the database process where very large tables are divided into multiple smaller parts. The main purpose of partitioning is to maintain large tables and to reduce the overall response time to read and load data for particular SQL operations.

    There are two types of partitioning:

    1. Vertical Partitioning
    2. Horizontal Partitioning

    Vertical Partitioning

    Vertical table partitioning is mostly used to increase SQL Server performance especially in cases where a query retrieves all columns from a table. In this case to reduce access times the BLOB columns can be split to its own table. Vertical partitioning splits a table into two or more tables containing different columns:

    In above Diagram used two tables. One is Student_master which contain unique studentid, studentname, address and email and here created another Student_detail table because one student have multiple documents images so that use vertical partitioning.

    Horizontal Partitioning

    Horizontal partitioning divides a table into multiple tables that contain the same number of columns, but fewer rows. For example, if a table contains a large number of rows that represent monthly reports it could be partitioned horizontally into tables by years, with each table representing all monthly reports for a specific year. This way queries requiring data for a specific year will only reference the appropriate table.

    Tables are horizontally partitioned based on a column which will be used for partitioning and the ranges associated to each partition. Partitioning column is usually a datetime column but all data types that are valid for use as index columns can be used as a partitioning column, except a timestamp column. The ntext, text, image, xml, varchar(max), nvarchar(max), or varbinary(max), Microsoft .NET Framework common language runtime (CLR) user-defined type, and alias data type columns cannot be specified.

    An example of horizontal partitioning with creating a new partitioned table

    To create a partitioned table for storing monthly reports.

    Step:1 First we will create database.

    Step:2 Give database name is PartitioningDB. Then click on ok button.

    Step:3 After creating database then open new query tab.

    Step:4 In new query we will create additional filegroups. Every database has a primary filegroup that contains the primary data file (.mdf). An additional, user-defined, filegroups can be created to contain secondary files (.ndf). We will create 12 filegroups for every month. Then click on execute button.

    ALTER DATABASE PartitioningDB
    ADD FILEGROUP January
    GO
    ALTER DATABASE PartitioningDB
    ADD FILEGROUP February
    GO
    ALTER DATABASE PartitioningDB
    ADD FILEGROUP March
    GO
    ALTER DATABASE PartitioningDB
    ADD FILEGROUP April
    GO
    ALTER DATABASE PartitioningDB
    ADD FILEGROUP May
    GO
    ALTER DATABASE PartitioningDB
    ADD FILEGROUP June
    GO
    ALTER DATABASE PartitioningDB
    ADD FILEGROUP July
    GO
    ALTER DATABASE PartitioningDB
    ADD FILEGROUP August
    GO
    ALTER DATABASE PartitioningDB
    ADD FILEGROUP September
    GO
    ALTER DATABASE PartitioningDB
    ADD FILEGROUP October
    GO
    ALTER DATABASE PartitioningDB
    ADD FILEGROUP November
    GO
    ALTER DATABASE PartitioningDB
    ADD FILEGROUP December GO
    

    Step:5 To check created and available file groups in the current database run the following query:

    SELECT name AS AvailableFilegroups
    FROM sys.filegroups
    WHERE type = 'FG

    Step:6 When filegroups are created we will add .ndf file to every filegroup using following query:

    ALTER DATABASE [PartitioningDB]
        ADD FILE 
        (
        NAME = [PartJan],
        FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.LENOVO\MSSQL\DATA\PartitioningDB.ndf',
            SIZE = 3072 KB, 
            MAXSIZE = UNLIMITED, 
            FILEGROWTH = 1024 KB
        ) TO FILEGROUP [January]

    Step:7 To check files created added to the filegroups run the following query:

    SELECT 
    name as [FileName],
    physical_name as [FilePath] 
    FROM sys.database_files
    where type_desc = 'ROWS'
    GO

    Step:8 After creating additional filegroups for storing data we’ll create a partition function. A partition function is a function that maps the rows of a partitioned table into partitions based on the values of a partitioning column.

    In this example we will create a partitioning function that partitions a table into 12 partitions, one for each month of a year’s worth of values in a datetime column:

    CREATE PARTITION FUNCTION [PartitioningByMonth] (datetime)
    AS RANGE RIGHT FOR VALUES ('20140201', '20140301', '20140401',
                   '20140501', '20140601', '20140701', '20140801', 
                   '20140901', '20141001', '20141101', '20141201');

    To map the partitions of a partitioned table to filegroups and determine the number and domain of the partitions of a partitioned table we will create a partition scheme:

    CREATE PARTITION SCHEME PartitionBymonth
    AS PARTITION PartitioningBymonth
    TO (January, February, March, 
        April, May, June, July, 
        August, September, October, 
        November, December);