• 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);

    Step:9 Now we’re going to create the table using the PartitionBymonth partition scheme, and fill it with the test data:

    CREATE TABLE Reports
    (ReportDate datetime PRIMARY KEY,
    MonthlyReport varchar(max))
    ON PartitionBymonth (ReportDate);
    GO
     
    INSERT INTO Reports (ReportDate,MonthlyReport)
    SELECT '20140105', 'ReportJanuary' UNION ALL
    SELECT '20140205', 'ReportFebruary' UNION ALL
    SELECT '20140308', 'ReportMarch' UNION ALL
    SELECT '20140409', 'ReportApril' UNION ALL
    SELECT '20140509', 'ReportMay' UNION ALL
    SELECT '20140609', 'ReportJune' UNION ALL
    SELECT '20140709', 'ReportJuly' UNION ALL
    SELECT '20140809', 'ReportAugust' UNION ALL
    SELECT '20140909', 'ReportSeptember' UNION ALL
    SELECT '20141009', 'ReportOctober' UNION ALL
    SELECT '20141109', 'ReportNovember' UNION ALL
    SELECT '20141209', 'ReportDecember'

    Step:10 We will now verify the rows in the different partitions:

    SELECT 
    p.partition_number AS PartitionNumber, f.name AS PartitionFilegroup, p.rows AS NumberOfRows FROM sys.partitions p JOIN sys.destination_data_spaces dds ON p.partition_number = dds.destination_id JOIN sys.filegroups f ON dds.data_space_id = f.data_space_id WHERE OBJECT_NAME(OBJECT_ID) = 'Reports'

    Partitioning a table using the SQL Server Management Studio Partitioning wizard

    SQL Server 2008 introduced a table partitioning wizard in SQL Server Management Studio.

    Step:1 Right click on a table in the Object Explorer pane and in the Storage context menu choose the Create Partition command:

    Step:2 In the Select a Partitioning Column window, select a column which will be used to partition a table from available partitioning columns and click on Next button

    Step:3 In the Select a Partition Function window enter the name of a partition function to map the rows of the table or index into partitions based on the values of the ReportDate column, or choose the existing partition function and click on Next button

    Step:4 In the Select a Partition Scheme window create the partition scheme to map the partitions of the MonthlyReport table to different filegroups and click on Next button

    Step:5 In the Map Partitions window choose the rage of partitioning and select the available filegroups and the range boundary. The Left boundary is based on Value <= Boundary and the Right boundary is based on Value < Boundary and click on Next button

    Step: 6 The next screen of the wizard offers to choose the option to whether to execute the script immediately by the wizard to create objects and a partition table, or to create a script and save it. A schedule for executing the script to perform the operations automatically can also be specified:

    Step:7 The next screen of the wizard shows a review of selections made in the wizard and click on Finish button to complete the process.

0 Years in
Operation
0 Loyal
Clients
0 Successful
Projects

Words from our clients

 

Tell Us About Your Project

We’ve done lot’s of work, Let’s Check some from here