Counter of Festivals

Ashok Blog for SQL Learners and Beginners and Experts

Thursday 16 June 2016

Understanding Pages and Extents in SQL

Understanding Pages and Extents

SQL Server 2008 R2 
 
The fundamental unit of data storage in SQL Server is the page. 
The disk space allocated to a data file (.mdf or .ndf) in a database is logically divided into pages numbered contiguously from 0 to n. Disk I/O operations are performed at the page level. That is, SQL Server reads or writes whole data pages.

Extents are a collection of eight physically contiguous pages and are used to efficiently manage the pages. All pages are stored in extents.

In SQL Server, the page size is 8 KB or  8,192 bytes
This means SQL Server databases have 128 pages per megabyte. i.e 1024 KB (128*8)

1 MB= 1024 KB= 128 Pages (128*8)

Each page begins with a 96-byte header that is used to store system information about the page. This information includes the page number, page type, the amount of free space on the page, and the allocation unit ID of the object that owns the page.

The following table shows the page types used in the data files of a SQL Server database.
Page typeContents
DataData rows with all data, except text, ntext, image, nvarchar(max), varchar(max), varbinary(max), and xml data, when text in row is set to ON.
IndexIndex entries.
Text/ImageLarge object data types:
  • text, ntext, image, nvarchar(max), varchar(max), varbinary(max), and xml data
Variable length columns when the data row exceeds 8 KB:
  • varchar, nvarchar, varbinary, and sql_variant
Global Allocation Map, Shared Global Allocation Map Information about whether extents are allocated.
Page Free SpaceInformation about page allocation and free space available on pages.
Index Allocation MapInformation about extents used by a table or index per allocation unit.
Bulk Changed MapInformation about extents modified by bulk operations since the last BACKUP LOG statement per allocation unit.
Differential Changed MapInformation about extents that have changed since the last BACKUP DATABASE statement per allocation unit.
NoteNote
Log files do not contain pages; they contain a series of log records.
Data rows are put on the page serially, starting immediately after the header. A row offset table starts at the end of the page, and each row offset table contains one entry for each row on the page. Each entry records how far the first byte of the row is from the start of the page. The entries in the row offset table are in reverse sequence from the sequence of the rows on the page.
SQL Server data page with row offsets

Large Row Support

Rows cannot span pages, however portions of the row may be moved off the row's page so that the row can actually be very large. The maximum amount of data and overhead that is contained in a single row on a page is 8,060 bytes (8 KB). However, this does not include the data stored in the Text/Image page type. This restriction is relaxed for tables that contain varchar, nvarchar, varbinary, or sql_variant columns. When the total row size of all fixed and variable columns in a table exceeds the 8,060 byte limitation, SQL Server dynamically moves one or more variable length columns to pages in the ROW_OVERFLOW_DATA allocation unit, starting with the column with the largest width. This is done whenever an insert or update operation increases the total size of the row beyond the 8060 byte limit. When a column is moved to a page in the ROW_OVERFLOW_DATA allocation unit, a 24-byte pointer on the original page in the IN_ROW_DATA allocation unit is maintained. If a subsequent operation reduces the row size, SQL Server dynamically moves the columns back to the original data page. For more information, see Row-Overflow Data Exceeding 8 KB.

Extents are the basic unit in which space is managed.

An extent is 8 eight physically contiguous pages, or 64 KB. (8*8)


This means SQL Server databases have 16 extents per megabyte.

1MB=1024 KB= 16 Extents (1024KB/64KB=16 Extents)

A page is the smallest unit of reading and writing. Pages are further organized into extents. An extent consists of eight consecutive pages. Pages from an extent can belong to a single object or to multiple objects.
If the pages belong to multiple objects, then the extent is called a mixed extent;
 if the pages belong to a single object, then the extent is called a uniform extent.
SQL Server stores the first eight pages of an object in mixed extents. When an object exceeds eight pages, SQL Server allocates additional uniform extents for this object.

To make its space allocation efficient, SQL Server does not allocate whole extents to tables with small amounts of data. SQL Server has two types of extents:
  • Uniform extents are owned by a single object; all eight pages in the extent can only be used by the owning object.
  • Mixed extents are shared by up to eight objects. Each of the eight pages in the extent can be owned by a different object.
A new table or index is generally allocated pages from mixed extents. When the table or index grows to the point that it has eight pages, it then switches to use uniform extents for subsequent allocations. If you create an index on an existing table that has enough rows to generate eight pages in the index, all allocations to the index are in uniform extents.
Mixed and uniform extents 
 
 
 
 
 
 

No comments:

Post a Comment