{"id":4236,"date":"2013-11-05T13:42:37","date_gmt":"2013-11-05T21:42:37","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/paul\/?p=4236"},"modified":"2013-11-05T14:25:16","modified_gmt":"2013-11-05T22:25:16","slug":"inside-the-storage-engine-how-are-allocation-unit-ids-calculated","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/","title":{"rendered":"Inside the Storage Engine: How are allocation unit IDs calculated?"},"content":{"rendered":"<p>It&#8217;s been a long time since I&#8217;ve written a post about pure internals, but every so often I get asked how an allocation unit ID is calculated from the <em>m_objId<\/em> and <em>m_indexId<\/em> fields that are stored in the header of every page.<\/p>\n<p>When <em>DBCC PAGE<\/em> dumps a page header&#8217;s contents, it does the necessary calculations and metadata look-ups to be able to tell you the allocation unit ID, partition ID, relational object ID, and relational index ID. Basically everything prefixed with &#8216;Metadata:&#8217; in the <em>DBCC PAGE<\/em> output below is NOT stored on the page itself:<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nPage @0x00000004ED8A2000\r\n\r\nm_pageId = (1:445)                  m_headerVersion = 1                 m_type = 1\r\nm_typeFlagBits = 0x0                m_level = 0                         m_flagBits = 0xa000\r\nm_objId (AllocUnitId.idObj) = 97    m_indexId (AllocUnitId.idInd) = 256\r\nMetadata: AllocUnitId = 72057594044284928\r\nMetadata: PartitionId = 72057594039304192                                Metadata: IndexId = 0\r\nMetadata: ObjectId = 599673184      m_prevPage = (0:0)                  m_nextPage = (0:0)\r\npminlen = 8                         m_slotCnt = 1                       m_freeCnt = 8069\r\nm_freeData = 121                    m_reservedCnt = 0                   m_lsn = (225:443:22)\r\nm_xactReserved = 0                  m_xdesId = (0:0)                    m_ghostRecCnt = 0\r\nm_tornBits = 0                      DB Frag ID = 1\r\n<\/pre>\n<p>The formula is as follows:<\/p>\n<ul>\n<li>Take the <em>m_indexId<\/em> and left-shift by 48, giving value A<\/li>\n<li>Take the <em>m_objId<\/em> and left-shift by 16, giving value B<\/li>\n<li><em>AllocUnitId<\/em> = A | B (where | is a logical OR operation)<\/li>\n<\/ul>\n<p>Using the page above:<\/p>\n<ul>\n<li><span style=\"line-height: 12.997159004211426px;\">A = 256 &lt;&lt; 48 =\u00a072057594037927936<\/span><\/li>\n<li>B = 97 &lt;&lt; 16 =\u00a06356992<\/li>\n<li>AllocUnitId =\u00a072057594044284928<\/li>\n<\/ul>\n<p>You can do this using SQL Server using the <em>POWER<\/em> function as a left shift of X bits is the same as multiplying by 2-to-the-power-X:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nSELECT 256 * CONVERT (BIGINT, POWER (2.0, 48)) | 97 * CONVERT (BIGINT, POWER (2.0, 16));\r\nGO\r\n<\/pre>\n<p>And then you can perform the various look-ups using <em>sys.system_internals_allocation_units<\/em> and <em>sys.partitions<\/em> like so:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nSELECT\r\n\t&#x5B;a].&#x5B;container_id] AS &#x5B;Partition ID],\r\n\t&#x5B;p].&#x5B;object_id] AS &#x5B;Object ID],\r\n\t&#x5B;p].&#x5B;index_id] AS &#x5B;Index ID]\r\nFROM sys.system_internals_allocation_units &#x5B;a]\r\nJOIN sys.partitions &#x5B;p]\r\n\tON &#x5B;p].&#x5B;partition_id] = &#x5B;a].&#x5B;container_id]\r\nWHERE\r\n\t&#x5B;a].&#x5B;allocation_unit_id] = 72057594044284928;\r\nGO\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nPartition ID         Object ID   Index ID\r\n-------------------- ----------- -----------\r\n72057594039304192    599673184   0\r\n<\/pre>\n<p>And you can see that the values match the <em>DBCC PAGE<\/em> output.<\/p>\n<p>To convert from an allocation unit ID to what you should see in the <em>DBCC PAGE<\/em> output:<\/p>\n<ul>\n<li><span style=\"line-height: 12.997159004211426px;\"><em>m_indexId<\/em> = <em>AllocUnitId<\/em> &gt;&gt; 48<\/span><\/li>\n<li><em>m_objId<\/em> = (<em>AllocUnitId<\/em> &#8211; (<em>m_indexId<\/em> &lt;&lt; 48)) &gt;&gt; 16<\/li>\n<\/ul>\n<p>The T-SQL for this involves floating point math as we need to use the reciprocal of <em>POWER<\/em>:<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nDECLARE @alloc BIGINT = 72057594044284928;\r\nDECLARE @index BIGINT;\r\n\r\nSELECT @index =\r\n\tCONVERT (BIGINT,\r\n\t\tCONVERT (FLOAT, @alloc)\r\n\t\t\t* (1 \/ POWER (2.0, 48)) -- right shift, reciprocal of left shift\r\n\t);\r\nSELECT\r\n\tCONVERT (BIGINT,\r\n\t\tCONVERT (FLOAT, @alloc - (@index * CONVERT (BIGINT, POWER (2.0, 48))))\r\n\t\t\t* (1 \/ POWER (2.0, 16)) -- right shift, reciprocal of left shift\r\n\t) AS &#x5B;m_objId],\r\n\t@index AS &#x5B;m_indexId];\r\nGO\r\n<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nm_objId              m_indexId\r\n-------------------- --------------------\r\n97                   256\r\n<\/pre>\n<p>An example of when you might use this information\/code is during programmatic analysis of a corrupt database that <em>DBCC CHECKDB<\/em> cannot process to allow you to extract data as a last resort.<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s been a long time since I&#8217;ve written a post about pure internals, but every so often I get asked how an allocation unit ID is calculated from the m_objId and m_indexId fields that are stored in the header of every page. When DBCC PAGE dumps a page header&#8217;s contents, it does the necessary calculations [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,62],"tags":[],"class_list":["post-4236","post","type-post","status-publish","format-standard","hentry","category-inside-the-storage-engine","category-on-disk-structures"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Inside the Storage Engine: How are allocation unit IDs calculated? - Paul S. Randal<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Inside the Storage Engine: How are allocation unit IDs calculated? - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"It&#8217;s been a long time since I&#8217;ve written a post about pure internals, but every so often I get asked how an allocation unit ID is calculated from the m_objId and m_indexId fields that are stored in the header of every page. When DBCC PAGE dumps a page header&#8217;s contents, it does the necessary calculations [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2013-11-05T21:42:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-11-05T22:25:16+00:00\" \/>\n<meta name=\"author\" content=\"Paul Randal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paul Randal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/\",\"name\":\"Inside the Storage Engine: How are allocation unit IDs calculated? - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2013-11-05T21:42:37+00:00\",\"dateModified\":\"2013-11-05T22:25:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Inside the Storage Engine: How are allocation unit IDs calculated?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\",\"name\":\"Paul S. Randal\",\"description\":\"In Recovery...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\",\"name\":\"Paul Randal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"caption\":\"Paul Randal\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/paul\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Inside the Storage Engine: How are allocation unit IDs calculated? - Paul S. Randal","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:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/","og_locale":"en_US","og_type":"article","og_title":"Inside the Storage Engine: How are allocation unit IDs calculated? - Paul S. Randal","og_description":"It&#8217;s been a long time since I&#8217;ve written a post about pure internals, but every so often I get asked how an allocation unit ID is calculated from the m_objId and m_indexId fields that are stored in the header of every page. When DBCC PAGE dumps a page header&#8217;s contents, it does the necessary calculations [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/","og_site_name":"Paul S. Randal","article_published_time":"2013-11-05T21:42:37+00:00","article_modified_time":"2013-11-05T22:25:16+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/","name":"Inside the Storage Engine: How are allocation unit IDs calculated? - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2013-11-05T21:42:37+00:00","dateModified":"2013-11-05T22:25:16+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/inside-the-storage-engine-how-are-allocation-unit-ids-calculated\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"Inside the Storage Engine: How are allocation unit IDs calculated?"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/","name":"Paul S. Randal","description":"In Recovery...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce","name":"Paul Randal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","caption":"Paul Randal"},"sameAs":["http:\/\/3.209.169.194\/blogs\/paul"],"url":"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/4236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/comments?post=4236"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/4236\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=4236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=4236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=4236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}