{"id":1031,"date":"2012-07-08T18:45:23","date_gmt":"2012-07-08T22:45:23","guid":{"rendered":"http:\/\/sqlity.net\/en\/?p=1031"},"modified":"2014-11-13T13:57:13","modified_gmt":"2014-11-13T18:57:13","slug":"partitions-boundaries-filgroups","status":"publish","type":"post","link":"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/","title":{"rendered":"Partitions, Boundaries and Filegroups"},"content":{"rendered":"<div>\n<h3>Introduction<\/h3>\n<p>\nEarlier today on twitter I ran across an interesting question. The question was, how to identify all partitions and their boundaries for a given filegroup. The solution is not as trivial as it may sound, so I will expand on it here a little.\n<\/p>\n<h3>Partitions and Filegroups<\/h3>\n<p>\nFirst we need to find out which filegroup a partition is on. All partitions are listed in the <span class=\"tt\">sys.partitions<\/span> view. That view has an <span class=\"tt\">object_id<\/span> column that we can use to join to <span class=\"tt\">sys.tables<\/span>.  Joining to <span class=\"tt\">sys.tables<\/span> makes sure we are dealing only with partitions of user tables. <span class=\"tt\">sys.partitions<\/span> has a <span class=\"tt\">hobt_id<\/span> column that allows us to join to <span class=\"tt\">sys.allocation_units<\/span> on its <span class=\"tt\">container_id<\/span> column. Each table and each index has at least one partition. All normal data pages of a partition are stored together in a single allocation unit. The <span class=\"tt\">sys.allocation_units<\/span> view also has a <span class=\"tt\">data_space_id<\/span> column that can be used to join to <span class=\"tt\">sys.filegroups<\/span>.\n<\/p>\n<h3>Partition Functions<\/h3>\n<p>\nTo get from a partition to its partition function we need to first join <span class=\"tt\">sys.partitions<\/span> with <span class=\"tt\">sys.indexes<\/span> on both the <span class=\"tt\">object_id<\/span> and on the <span class=\"tt\">index_id<\/span> columns. <span class=\"tt\">sys.indexes<\/span> also has a <span class=\"tt\">data_space_id<\/span> column that we can use to join to <span class=\"tt\">sys.partition_schemes.data_space_id<\/span>. From <span class=\"tt\">sys.partition_schemes<\/span> we get to <span class=\"tt\">sys.partition_functions<\/span> using the <span class=\"tt\">function_id<\/span> column of both tables.\n<\/p>\n<h3>Boundaries<\/h3>\n<p>\nA partition function defines the boundaries of the partitions. The first partition does not have a lower boundary and the last partition does not have an upper boundary. Wether the boundary value itself belongs to its left or its right partition is specified by the <span class=\"tt\">LEFT<\/span> or the <span class=\"tt\">RIGHT<\/span> key word that was used at the time the partition function was created. It is always the same for all boundaries of a single partition function. Which one was used can be seen in the <span class=\"tt\">boundary_value_on_right<\/span> column of the <span class=\"tt\">sys.partition_functions<\/span> view. The actual boundary values can be found in the <span class=\"tt\">value<\/span> column of the <span class=\"tt\">sys.partition_range_values<\/span> view.\n<\/p>\n<p>\nAs each partition has up to two boundaries, we need to join to <span class=\"tt\">sys.partition_range_values<\/span> twice.  The <span class=\"tt\">function_id<\/span> column ties it to the <span class=\"tt\">sys.partition_functions<\/span> view. The <span class=\"tt\">boundary_id<\/span> can be correlated with the <span class=\"tt\">partition_number<\/span> column of <span class=\"tt\">sys.partitions<\/span>. The first partition has boundary one as upper boundary; the second partition has boundary one as lower boundary and boundary two as upper boundary. So for the lower boundary the join condition has to be <span class=\"tt\">sys.partition_range_values.boundary_id + 1 = sys.partitions.partition_number<\/span> and for the upper boundary it is <span class=\"tt\">sys.partition_range_values.boundary_id = sys.partitions.partition_number<\/span>.\n<\/p>\n<h3>Final Solution<\/h3>\n<p>\nWhen putting this all together, you need to pay attention to the fact, that while every table has at least one partition, not everyone is located on a partition scheme. Also, not every partition of a partitioned table has both the upper and the lower boundary defined. That means that the four joins to the partition scheme related views have to be outer joins.\n<\/p>\n<p>\nThe final query can be found below.\n<\/p>\n<div>\n[sql]\nSELECT  f.data_space_id,<br \/>\n        f.NAME AS file_group_name,<br \/>\n        SCHEMA_NAME(t.schema_id) AS table_schema,<br \/>\n        t.name AS table_name,<br \/>\n        p.partition_number,<br \/>\n        ps.name AS partition_schem_name,<br \/>\n        pf.name AS partition_function_name,<br \/>\n        pf.boundary_value_on_right,<br \/>\n        left_prv.value AS left_range,<br \/>\n        right_prv.value AS right_value,<br \/>\n        ISNULL(STR(CAST(left_prv.value AS BIGINT)), '-INF')<br \/>\n        + CASE WHEN pf.boundary_value_on_right = 0 THEN ' &lt; '<br \/>\n               ELSE ' &lt;= '<br \/>\n          END + 'X' + CASE WHEN pf.boundary_value_on_right = 0 THEN ' &lt;= '<br \/>\n                           ELSE ' &lt; '<br \/>\n                      END + ISNULL(STR(CAST(right_prv.value AS BIGINT)), 'INF') AS range_desc<br \/>\nFROM    sys.partitions p<br \/>\nJOIN    sys.tables t<br \/>\n        ON p.object_id = t.object_id<br \/>\nJOIN    sys.indexes i<br \/>\n        ON p.object_id = i.object_id<br \/>\n        AND p.index_id = i.index_id<br \/>\nJOIN    sys.allocation_units au<br \/>\n        ON p.hobt_id = au.container_id<br \/>\nJOIN    sys.filegroups f<br \/>\n        ON au.data_space_id = f.data_space_id<br \/>\nLEFT JOIN    sys.partition_schemes ps<br \/>\n        ON ps.data_space_id = i.data_space_id<br \/>\nLEFT JOIN    sys.partition_functions pf<br \/>\n        ON ps.function_id = pf.function_id<br \/>\nLEFT JOIN sys.partition_range_values left_prv<br \/>\n        ON left_prv.function_id = ps.function_id<br \/>\n           AND left_prv.boundary_id + 1 = p.partition_number<br \/>\nLEFT JOIN sys.partition_range_values right_prv<br \/>\n        ON right_prv.function_id = ps.function_id<br \/>\n           AND right_prv.boundary_id = p.partition_number;<br \/>\n[\/sql]\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This post shows a way to link table and index partitions in SQL Server to their filegroups as well as their boundary values. <a href=\"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/\">[more&#8230;]<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[5,14],"tags":[],"class_list":["post-1031","post","type-post","status-publish","format-standard","hentry","category-general","category-sql-server-internals"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Partitions, Boundaries and Filegroups - sqlity.net<\/title>\n<meta name=\"description\" content=\"This post shows a way to link table and index partitions in SQL Server to their filegroups as well as their boundary values.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Partitions, Boundaries and Filegroups - sqlity.net\" \/>\n<meta property=\"og:description\" content=\"This post shows a way to link table and index partitions in SQL Server to their filegroups as well as their boundary values.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/\" \/>\n<meta property=\"og:site_name\" content=\"sqlity.net\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/sqlity.net\" \/>\n<meta property=\"article:published_time\" content=\"2012-07-08T22:45:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-11-13T18:57:13+00:00\" \/>\n<meta name=\"author\" content=\"Sebastian Meine\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@sqlity\" \/>\n<meta name=\"twitter:site\" content=\"@sqlity\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sebastian Meine\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/1031\\\/partitions-boundaries-filgroups\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/1031\\\/partitions-boundaries-filgroups\\\/\"},\"author\":{\"name\":\"Sebastian Meine\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#\\\/schema\\\/person\\\/bcffd8c572bc2f1bd10fdba80135e53c\"},\"headline\":\"Partitions, Boundaries and Filegroups\",\"datePublished\":\"2012-07-08T22:45:23+00:00\",\"dateModified\":\"2014-11-13T18:57:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/1031\\\/partitions-boundaries-filgroups\\\/\"},\"wordCount\":852,\"commentCount\":3,\"articleSection\":[\"General\",\"SQL Server Internals\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/sqlity.net\\\/en\\\/1031\\\/partitions-boundaries-filgroups\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/1031\\\/partitions-boundaries-filgroups\\\/\",\"url\":\"https:\\\/\\\/sqlity.net\\\/en\\\/1031\\\/partitions-boundaries-filgroups\\\/\",\"name\":\"Partitions, Boundaries and Filegroups - sqlity.net\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#website\"},\"datePublished\":\"2012-07-08T22:45:23+00:00\",\"dateModified\":\"2014-11-13T18:57:13+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#\\\/schema\\\/person\\\/bcffd8c572bc2f1bd10fdba80135e53c\"},\"description\":\"This post shows a way to link table and index partitions in SQL Server to their filegroups as well as their boundary values.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/1031\\\/partitions-boundaries-filgroups\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/sqlity.net\\\/en\\\/1031\\\/partitions-boundaries-filgroups\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/1031\\\/partitions-boundaries-filgroups\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/sqlity.net\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Partitions, Boundaries and Filegroups\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/sqlity.net\\\/en\\\/\",\"name\":\"sqlity.net\",\"description\":\"Quality for SQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/sqlity.net\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/sqlity.net\\\/en\\\/#\\\/schema\\\/person\\\/bcffd8c572bc2f1bd10fdba80135e53c\",\"name\":\"Sebastian Meine\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g\",\"caption\":\"Sebastian Meine\"},\"sameAs\":[\"http:\\\/\\\/sqlity.net\",\"https:\\\/\\\/x.com\\\/sqlity\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Partitions, Boundaries and Filegroups - sqlity.net","description":"This post shows a way to link table and index partitions in SQL Server to their filegroups as well as their boundary values.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/","og_locale":"en_US","og_type":"article","og_title":"Partitions, Boundaries and Filegroups - sqlity.net","og_description":"This post shows a way to link table and index partitions in SQL Server to their filegroups as well as their boundary values.","og_url":"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/","og_site_name":"sqlity.net","article_publisher":"https:\/\/www.facebook.com\/sqlity.net","article_published_time":"2012-07-08T22:45:23+00:00","article_modified_time":"2014-11-13T18:57:13+00:00","author":"Sebastian Meine","twitter_card":"summary_large_image","twitter_creator":"@sqlity","twitter_site":"@sqlity","twitter_misc":{"Written by":"Sebastian Meine","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/#article","isPartOf":{"@id":"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/"},"author":{"name":"Sebastian Meine","@id":"https:\/\/sqlity.net\/en\/#\/schema\/person\/bcffd8c572bc2f1bd10fdba80135e53c"},"headline":"Partitions, Boundaries and Filegroups","datePublished":"2012-07-08T22:45:23+00:00","dateModified":"2014-11-13T18:57:13+00:00","mainEntityOfPage":{"@id":"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/"},"wordCount":852,"commentCount":3,"articleSection":["General","SQL Server Internals"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/","url":"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/","name":"Partitions, Boundaries and Filegroups - sqlity.net","isPartOf":{"@id":"https:\/\/sqlity.net\/en\/#website"},"datePublished":"2012-07-08T22:45:23+00:00","dateModified":"2014-11-13T18:57:13+00:00","author":{"@id":"https:\/\/sqlity.net\/en\/#\/schema\/person\/bcffd8c572bc2f1bd10fdba80135e53c"},"description":"This post shows a way to link table and index partitions in SQL Server to their filegroups as well as their boundary values.","breadcrumb":{"@id":"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/sqlity.net\/en\/1031\/partitions-boundaries-filgroups\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/sqlity.net\/en\/"},{"@type":"ListItem","position":2,"name":"Partitions, Boundaries and Filegroups"}]},{"@type":"WebSite","@id":"https:\/\/sqlity.net\/en\/#website","url":"https:\/\/sqlity.net\/en\/","name":"sqlity.net","description":"Quality for SQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/sqlity.net\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/sqlity.net\/en\/#\/schema\/person\/bcffd8c572bc2f1bd10fdba80135e53c","name":"Sebastian Meine","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4ab0a6d02dd494849a584a2c3c8bc3bdcef1d0aa5f87e98bf905dbdb9ad2ce3a?s=96&d=mm&r=g","caption":"Sebastian Meine"},"sameAs":["http:\/\/sqlity.net","https:\/\/x.com\/sqlity"]}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2wXuw-gD","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/posts\/1031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/comments?post=1031"}],"version-history":[{"count":0,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/posts\/1031\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/media?parent=1031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/categories?post=1031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlity.net\/en\/wp-json\/wp\/v2\/tags?post=1031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}