sqlschool.gr logo

articles

Articles of SQLschool.gr Team

SQL Server Allocation Units

Antonios Chatzipavlis
Tuesday 20 July 2021

Overview

Συνεχίζοντας την "κατάδυση" μας στα SQL Server internals σε αυτό το άρθρο θα ασχοληθούμε με τα Allocation Units τα οποία μπορούμε να δούμε με την χρήση της DMV sys.allocation_units.

Στο προηγούμενο άρθρο μου (In which pages (data or index) are my table data stored?) είδαμε πώς μπορούμε να δούμε τα pages που περιέχουν τα δεδομένα ή τα index data ενός συγκεκριμένου object κάνοντας χρήση ενός undocumented DMF.

Example

Αν στο query στο οποίο κάνουμε την χρήση του DMF προσθέσουμε το πεδίο allocation unit θα παρατηρήσουμε ότι για τις σελίδες του clustered index έχουμε την ίδια τιμή και το ίδιο ισχύει και για τις σελίδες του nonclustered index. Άρα έχουμε δύο διαφορετικά allocation units όπως δείχνω στο παρακάτω παράδειγμα.

Example Script

create database bindb;
go

use bindb;
go

create table X (col1 int, col2 datetime2(7), col3 char(100));
go

insert into X values (1,SYSDATETIME(),'a') ;
go

create clustered index cidx on x(col1);
go

create  index idx on x(col2);
go

select 
         db_name(p.database_id)          as [database_name]
    ,    object_name(p.object_id)        as table_name
    ,    i.[name]                        as index_name
    ,    is_allocated
    ,    allocated_page_file_id          as [file_id]
    ,    allocated_page_page_id          as [page_id]
    ,    page_type_desc
    ,    page_level
    ,    previous_page_page_id           as previous_page_id
    ,    next_page_page_id               as next_page_id
    ,     p.allocation_unit_id
from 
    sys.dm_db_database_page_allocations (
                            db_id()
                        ,   object_id('x')
                        ,   null
                        ,   null
                        ,   'detailed'
                                        ) as p
left outer join sys.indexes i  on i.object_id = p.object_id and i.index_id = p.index_id
order by  table_name , index_name, page_level desc, is_allocated desc, previous_page_page_id;
go
Execution Result of sys.dm_db_database_page_allocations
database_name table_name index_name is_allocated file_id page_id page_type_desc page_level previous_page_id next_page_id allocation_unit_id
bindb X cidx 1 1 127 IAM_PAGE 0 0 0 72057594049724416
bindb X cidx 1 1 280 DATA_PAGE 0 0 0 72057594049724416
bindb X cidx 0 1 281 0 0 0 72057594049724416
bindb X cidx 0 1 282 0 0 0 72057594049724416
bindb X cidx 0 1 283 0 0 0 72057594049724416
bindb X cidx 0 1 284 0 0 0 72057594049724416
bindb X cidx 0 1 285 0 0 0 72057594049724416
bindb X cidx 0 1 286 0 0 0 72057594049724416
bindb X cidx 0 1 287 0 0 0 72057594049724416
bindb X idx 1 1 126 IAM_PAGE 0 0 0 72057594049789952
bindb X idx 1 1 264 INDEX_PAGE 0 0 0 72057594049789952
bindb X idx 0 1 265 0 0 0 72057594049789952
bindb X idx 0 1 266 0 0 0 72057594049789952
bindb X idx 0 1 267 0 0 0 72057594049789952
bindb X idx 0 1 268 0 0 0 72057594049789952
bindb X idx 0 1 269 0 0 0 72057594049789952
bindb X idx 0 1 270 0 0 0 72057594049789952
bindb X idx 0 1 271 0 0 0 72057594049789952

Query allocation units

Αν τώρα πάρουμε αυτές τις τιμές και ρωτήσουμε για αυτές το DMV sys.allocation_units θα δούμε τα συγκεντρωτικά νούμερα που πραγματικά ταιριάζουν.
Το total_pages δείχνει το σύνολο των σελίδων.
Το used_pages είναι θεωρώ ξεκάθαρο.
Το data_pages δεν περιλαμβάνει τις IAM.

Query allocation units

select * from sys.allocation_units
where allocation_unit_id in (72057594049724416,72057594049789952);
go
Execution Result of allocation units
allocation_unit_id type type_desc container_id data_space_id total_pages used_pages data_pages
72057594049724416 1 IN_ROW_DATA 72057594043236352 1 9 2 1
72057594049789952 1 IN_ROW_DATA 72057594043301888 1 9 2 1

What is Allocation Unit - Summary

Εύκολα πλέον μπορούμε να καταλάβουμε τι είναι το allocation unit αλλά ας το εξηγήσουμε.

Όπως έχω πει πολλές φορές ο SQL Server τοποθετεί τα δεδομένα σε 8KB pages οι οποίες μπορεί να περιέχουν τα πραγματικά δεδομένα του πίνακα, τα index data, τα large object data και τα row overflow data. Όλα αυτά τα pages αντί να είναι άμεσα συνδεδεμένα με το εκάστοτε πίνακα οργανώνονται σε λογικές οντότητες που είναι τα allocation units για κάθε ένα από τα παραπάνω περιεχόμενα που αυτά μπορεί να έχουν.

Μια τελευταία παρατήρηση που πρέπει να γίνει είναι ότι αν υπάρχουν partitions αυτά είναι ανά partition.


//Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis is a highly experienced Data Solutions Consultant and Trainer. He has been working in the IT industry since 1988, holding various roles such as senior developer, IT Manager, Data & AI Solutions Architect and Consultant.

Since 1995, Antonios has focused on modern technologies and software development tools, primarily by Microsoft. He has specialized in Data & AI since 2000, with expertise in Microsoft Data Platform (SQL Server, Azure SQL Databases, Azure Synapse Analytics, Microsoft Fabric, Power BI, AI) and Databricks.

Antonios is also a Microsoft Certified Trainer (MCT) for over 25 years, has been recognized as a Microsoft Most Valuable Professional (MVP) in Data Platform since 2010 and he is in the Data Expert 40 Powerlist 2024 by Boussias. He is the co-founder and visionary behind XLYTiCA, a company dedicated to Data & AI solutions.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

Get Certified: Become a Fabric Data Engineer

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn

sqlschool.gr © 2010-2025 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.