As you have been reading multiple posts around the database fundamentals topic in the past few weeks. Here is yet another post that will discuss around the basics of SQL Server Database Pages concept. I have seen there are many junior DBA’s who come from other platforms and ask what is a page, why is it restrictive, Is there a way to configure, are there optimizations I need to be aware etc. For most part the answer to this question is – there is hardly anything we can configure as parameters, but there is a reason why one must learn why SQL Server uses these defaults, what are the nuances we need to be aware and how can we understand the inner working. In this post, we just take a look at the basics of what constitute a database page and what are the various functions of each of these page types. This is not exhaustive, but still the learning can never stop right :).
- Logical pages are 8KB in size by design and default.
- The pages are numbered sequentially from 0 to N in each file, while the size of the file decides the number of pages that it contains.
- SQL Server can identify any page using the database-id, file-id and page-number combination. Many times there are error messages with this nomenclature for reference.
- With all math done, it means SQL Server has 128 pages per MB.
- When new space is allocated to a file because of expanding file, the first page of the newly created space is page# is N + 1.
- Though shrinking removes pages, SQL Server also ensures page numbers within a file are always contiguous.
- Space in a database file is managed in units called extents.
- Extent is made up of 8 logically contiguous pages therefore having a capacity of 64KB.
- 96 byte of each page is allocated for header information such as what type of page, amount of free space on page, object owing it etc.
- Post the header information is the data rows placed serially.
- Bottom of the page contains a row offset – there is one entry for every row inside that page and its offset from the start of the page.
- Row Offsets are in reverse sequence from the sequence of the rows.
- Maximum amount of data that one can store in a single row is 8060 bytes. Whenever we are able to fit the rows in this limit – it is called In-Row Data.
- From SQL 2008, the page restriction has been relaxed with an concept called as Row-Overflow Data. Read more from MSDN.
- SQL Server doesn’t allocate entire extents to tables which have small amounts of data.
- Extents are of 2 Types:
- Uniform extents: As name suggests it is owned by a single object; all eight pages in the extent can be used only by this single object
- Mixed extents: These are shared between objects. Up to eight objects can share an extent (a.k.a each object using a single page)
- Whenever a new table or index needs allocation it is given a mixed extents; when the table or index grows to eight pages, future allocations can use uniform extents. Between versions of SQL Server the # of pages post which uniform extents are given may vary.
- If a table or index needs more space and is still less than 8 pages total, SQL Server must find a mixed extent with space available, else if it is 8 pages or larger, uniform extents are located.
- When there is no mixed extents with free space, a new extent marked as mixed extent is allocated and SGAM updated accordingly.
- Basic Page Layouts:
- Page 0 is the File Header
- Page 1 is the Page Free Space (PFS)
- Page 2 is GAM
- Page 3 is SGAM
- Page 6 is Differential Changed Map (DCM)
- Page 7 is Bulk Changed Map (BCM)
- 2 special types of pages to record which extents have been allocated and what type it is being use for:
Global Allocation Map (GAM) pages (always on page 2)
- These pages record which extents have been allocated for any type of use.
- GAM has a bit to indicate value 0 means in use and 1 means free extent.
- After header and other overhead are accounted, there are 8000 bytes or 64000 bits to cover 64000 extents.
- Each GAM page covers 4 GB of data and a GAM page exists in a file for every 4 GB of file size.
- Shared Global Allocation Map (SGAM) pages (always on page 3)
- Record which extents are currently used as mixed events and have at least one unused page.
- Similar to GAM, it covers 64000 extents every 4GB of data.
- Has a bit for each extent in the interval it covers: 1 if the extent is being used a mixed extent and has free pages, 0 if the extent isn’t being used a mixed extent or it’s a mixed extent with no free pages
Index Allocation Maps (IAM)
- Any structure of data or index needs an IAM page.
- IAM pages keep track of the extents in a 4 GB section of a database file.
- An allocation unit is a set of pages belonging to a single partition in a table or index. The pages of can be of three storage types:
- Pages with regular in-row data
- Pages with Large Object (LOB) data
- Pages with row-overflow data
- IAM page contains a 96 byte page header, followed by IAM header which contains 8 page-pointer slots.
- IAM pages contain a set of 8 bits that map a range of extents onto a file. The IAM header has the address of the first extent in the range mapped by the IAM.
- A bit in the IAM bitmap represents an extent in the range: 1 means extent is allocated to the object owning the IAM, 0 means the extent isn’t allocated to the object owning the IAM.
- IAMs are allocated as needed for each object. Each of the IAM covers a possible range of 512,000 pages.
- PFS (page 1) – keeps track of how each particular page in a file is used.
- PFS also tracks if the page is empty, 1 to 50 percent full, 51 to 80 percent full, 81 to 95 percent full or 96 to 100 percent full.
- There is a PFS page approximately 8,000 pages in size after the first PFS page.
- Differential Changed Map (DCM page 6) – keeps track of which extents in a file have been modified since the last full database backup.
- Bulk Changed Map (BCM page 7)- is used when a page in a file is in a minimally or bulk-logged operation.
- Like the GAM and SGAM pages, DCM and BCM pages have 1 bit for each extent in the section of the file they represent.
- There is a GAM page 64,000 extents after the first GAM page on page 2 and another SGAM page 64,000 extents after the first SGAM page on page 3.
- Log files donot have the concepts of pages, they are a series of log entries written sequentially.
- In the previous versions the text, ntext and image data are stored in separate pages and are not inside the Data pages.
I think I have touched most parts of how database pages exist inside SQL Server. Though there are even more subtle nuances on the page functioning, we will keep it outside of this post for later. Please refer to MSDN BOL for more information on the page architectures.
This entry was posted on Thursday, November 22nd, 2012 at 08:30 and is filed under Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.