Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « MongoDB: Using the web-based administrative toolSSRS Properties - Chart Axis Minimum, Maximum, and Interval »
    comments

    To list all the tables that are partitioned you can use the sys.partitions view. However be aware that all tables and indexes in SQL Server contain at least one partition, whether or not they are explicitly partitioned.

    If you were to do the following, you would get back every table

    1. SELECT partition_number,rows,object_name(object_id)
    2. FROM sys.partitions

    So what can you do? Let's take a look. First we are going to create a partitioned table in case you don't have one so that you can get the same output as me.

    1. CREATE TABLE SalesPartitioned(YearCol SMALLINT NOT NULL,OrderID INT NOT NULL, SomeData UNIQUEIDENTIFIER DEFAULT newsequentialid())
    2. GO


    We are going to insert some data into the table

    1. INSERT SalesPartitioned (YearCol,OrderID)
    2. SELECT 2007,number
    3. FROM master..spt_values
    4. WHERE type = 'P'
    5. UNION ALL
    6. SELECT 2008,number + 2048
    7. FROM master..spt_values
    8. WHERE type = 'P'
    9. UNION ALL
    10. SELECT 2009,number + 4096
    11. FROM master..spt_values
    12. WHERE type = 'P'


    We will now add a primary key

    1. ALTER TABLE dbo.SalesPartitioned ADD CONSTRAINT
    2.     PK_Sales PRIMARY KEY NONCLUSTERED (YearCol,OrderID)


    Now it is time to create our partition function. Here is how we will do it

    1. CREATE PARTITION FUNCTION pfFiscalYear(SMALLINT)
    2. AS RANGE RIGHT FOR VALUES(2007,2008,2009)


    What that does is actually create 4 partitions, one for 2007, one for 2008, one for everything after 2008, and one for everything before 2006.
    <=2006
    = 2007
    = 2008
    >= 2009

    You can verify this by using the function $partition

    1. select 1 AS val,$partition.pfFiscalYear(1) AS partition     UNION all
    2. select 2006,$partition.pfFiscalYear(2006)   UNION all
    3. select 2007,$partition.pfFiscalYear(2007)   UNION all
    4. select 2008,$partition.pfFiscalYear(2008)   UNION all
    5. select 2009,$partition.pfFiscalYear(2009)   UNION all
    6. select 2010,$partition.pfFiscalYear(2010)   UNION all
    7. select 3000,$partition.pfFiscalYear(3000)

    And here is the output

    val         partition
    ----------- -----------
    1           1
    2006        1
    2007        2
    2008        3
    2009        4
    2010        4
    3000        4

    Now that we have the partition function, we need a partition scheme. A partition scheme is used to map boundary values in partition functions to filegroups. You can have one filegroup for each year placed on a different spindle, this way you don't have to wait for the disk if all partitions are on the same spindle. For the sake of simplicity we only have one filegroup. Here is how to create the partition scheme

    1. CREATE PARTITION SCHEME psFiscalYear
    2. AS PARTITION pfFiscalYear ALL TO ([PRIMARY])

    Partition scheme 'psFiscalYear' has been created successfully. 'PRIMARY' is marked as the next used filegroup in partition scheme 'psFiscalYear'.

    Now we will add a clustered index and partition this on the YearCol column, the syntax looks like this

    1. CREATE CLUSTERED INDEX IX_Sales ON SalesPartitioned(YearCol,OrderID)
    2. ON psFiscalYear(YearCol)

    Now it is time to list all the tables that are partitioned. Here is how you do it

    1. SELECT partition_number,rows,object_name(object_id)
    2. FROM sys.partitions s
    3. WHERE EXISTS(SELECT NULL
    4.                 FROM sys.partitions s2
    5.                 WHERE s.object_id = s2.object_id
    6.                 AND partition_number > 1
    7.                 AND s.index_id = s2.index_id)

    Here is the output

    partition_number	rows	TableName
    1	                0	SalesPartitioned
    2	                2048	SalesPartitioned
    3	                2048	SalesPartitioned
    4	                2048	SalesPartitioned

    As you can see we used WHERE EXISTS, we checked that the object had a partition_number higher than two and we also matched on index_id since a table can have more than one index

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    1259 views
    InstapaperVote on HN

    1 comment

    Comment from: alzdba [Member] Email
    alzdba Nice article.

    this query may come in handy too :

    Select OBJECT_SCHEMA_NAME(object_id) as ObjSchema
    ,OBJECT_NAME(object_id) as ObjName

    , *
    from sys.dm_db_partition_stats

    order by ObjSchema, ObjName, index_id , partition_number
    02/23/13 @ 05:03

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)