{"id":66,"date":"2013-03-15T15:55:00","date_gmt":"2013-03-15T20:55:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=66"},"modified":"2023-07-26T12:34:20","modified_gmt":"2023-07-26T17:34:20","slug":"sql-server-query-for-a-quick-overview-of-your-partitions","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=66","title":{"rendered":"SQL Server : Query For A Quick Overview Of Your Partitions"},"content":{"rendered":"<div>\n<div>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8212; =============================================<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8212; This is a basic query intended to provide<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8212; a quick look at partition info.&nbsp;&nbsp; <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8212; =============================================<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'><o:p>&nbsp;<\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> b<span style='color:gray'>.<\/span>name <span style='color:blue'>as<\/span> <span style='color:red'>&#8216;table_name&#8217;<\/span><span style='color:gray'>,<\/span> a<span style='color:gray'>.<\/span>partition_number<span style='color:gray'>,<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\"'>a<span style='color:gray'>.<\/span>[rows] <span style='color:blue'>as<\/span> <span style='color:red'>&#8216;row_count&#8217;<\/span><span style='color:gray'>,<\/span> a<span style='color:gray'>.<\/span>partition_id<span style='color:gray'>,<\/span> <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\"'>a<span style='color:gray'>.<\/span><span style='color:fuchsia'>object_id<\/span><span style='color:gray'>,<\/span> a<span style='color:gray'>.<\/span>hobt_id<span style='color:gray'>,<\/span> <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\"'>b<span style='color:gray'>.<\/span><span style='color:fuchsia'>schema_id<\/span><span style='color:gray'>,<\/span> c<span style='color:gray'>.<\/span>name <span style='color:blue'>as<\/span> <span style='color:red'>&#8216;schema_name&#8217;<\/span><span style='color:gray'>,<\/span>&nbsp; <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\"'>a<span style='color:gray'>.<\/span>index_id<span style='color:gray'>,<\/span> b<span style='color:gray'>.<\/span>create_date<span style='color:gray'>,<\/span> b<span style='color:gray'>.<\/span>modify_date<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>FROM<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'>&nbsp; <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>partitions<\/span> <span style='color:blue'>AS<\/span> a <span style='color:gray'>INNER<\/span> <span style='color:gray'>JOIN<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>sys<\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>.<\/span><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>objects<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:blue'>AS<\/span> b <span style='color:blue'>ON<\/span> a<span style='color:gray'>.<\/span><span style='color:fuchsia'>object_id<\/span> <span style='color:gray'>=<\/span> b<span style='color:gray'>.<\/span><span style='color:fuchsia'>object_id<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>INNER<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:gray'>JOIN<\/span> <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>schemas<\/span> <span style='color:blue'>AS<\/span> c <span style='color:blue'>On<\/span> c<span style='color:gray'>.<\/span><span style='color:fuchsia'>schema_id<\/span> <span style='color:gray'>=<\/span> b<span style='color:gray'>.<\/span><span style='color:fuchsia'>schema_id<\/span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>WHERE<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:fuchsia'>LOWER<\/span><span style='color:gray'>(<\/span>b<span style='color:gray'>.<\/span>name<span style='color:gray'>)<\/span> <span style='color:gray'>=<\/span> <span style='color:red'>&#8216;yourPartitionedTableName&#8217;<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8211;pull only first index for a simpler view<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'>and<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> a<span style='color:gray'>.<\/span>index_id <span style='color:gray'>&lt;<\/span> 2 <o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>ORDER<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:blue'>BY<\/span> b<span style='color:gray'>.<\/span>name<span style='color:gray'>,<\/span> partition_number<span style='color:gray'>;<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'><o:p>&nbsp;<\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8212; =============================================<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8211;Helpful info for a deeper dive<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8212; =============================================<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'><o:p>&nbsp;<\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:gray'>*<\/span> <span style='color:blue'>FROM<\/span> <span style='color:green'>SYS<\/span><span style='color:gray'>.<\/span><span style='color:green'>partition_range_values<\/span> <span style='color:gray'>;<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:gray'>*<\/span> <span style='color:blue'>from<\/span> <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>partitions<\/span><span style='color:gray'>;<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:gray'>*<\/span> <span style='color:blue'>FROM<\/span> <span style='color:green'>SYS<\/span><span style='color:gray'>.<\/span><span style='color:green'>partition_functions<\/span><span style='color:gray'>;<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:gray'>*<\/span> <span style='color:blue'>FROM<\/span> <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>partition_schemes<\/span><span style='color:gray'>;<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:gray'>*<\/span> <span style='color:blue'>from<\/span> <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>schemas<\/span><span style='color:gray'>;<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:gray'>*<\/span> <span style='color:blue'>FROM<\/span> <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>filegroups<\/span><span style='color:gray'>;<o:p><\/o:p><\/span><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:gray'><o:p>&nbsp;<\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:green'>&#8211;query sys.partitions to view and verify that the table contains partitions as expected<o:p><\/o:p><\/span><\/p>\n<p style='text-autospace:none'><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:gray'>*<\/span> <span style='color:blue'>FROM<\/span> <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>partitions<\/span> <span style='color:blue'>where<\/span> <span style='color:fuchsia'>OBJECT_NAME<\/span><span style='color:gray'>(<\/span><span style='color:fuchsia'>Object_Id<\/span><span style='color:gray'>)=<\/span><span style='color:red'>&#8216;yourPartitionedTableName&#8217;<\/span><span style='color:gray'>;<o:p><\/o:p><\/span><\/span><\/p>\n<p><span style='font-size:10.0pt;font-family:\"Courier New\";color:blue'>SELECT<\/span><span style='font-size:10.0pt;font-family:\"Courier New\"'> <span style='color:gray'>*<\/span> <span style='color:blue'>FROM<\/span> <span style='color:green'>sys<\/span><span style='color:gray'>.<\/span><span style='color:green'>objects<\/span> <span style='color:blue'>where<\/span> <span style='color:fuchsia'>OBJECT_NAME<\/span><span style='color:gray'>(<\/span><span style='color:fuchsia'>Object_Id<\/span><span style='color:gray'>)=<\/span><span style='color:red'>&#8216;yourPartitionedTableName&#8217;<\/span><span style='color:gray'>;<\/span><\/span><o:p><\/o:p><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&#8212; ============================================= &#8212; This is a basic query intended to provide &#8212; a quick look at partition info.&nbsp;&nbsp; &#8212; ============================================= &nbsp; SELECT b.name as &#8216;table_name&#8217;, a.partition_number, a.[rows] as &#8216;row_count&#8217;, a.partition_id, a.object_id, a.hobt_id, b.schema_id, c.name as &#8216;schema_name&#8217;,&nbsp; a.index_id, b.create_date, b.modify_date FROM&nbsp; sys.partitions AS a INNER JOIN sys.objects AS b ON a.object_id = b.object_id INNER JOIN sys.schemas &hellip; <a href=\"https:\/\/www.chrystalsander.com\/blog\/?p=66\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;SQL Server : Query For A Quick Overview Of Your Partitions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[52,50],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/66"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=66"}],"version-history":[{"count":1,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions\/178"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}