Search This Blog

Saturday, November 13

sql server internal architecture

Folks,
             I am very glad to share my experience.I have two reasons to write this article here.
First one is I had an interview with one of the company, they asked about the SQL server internals.(On that time I struggled)

When I Googled I didn’t get an entire information in any of the website.

The thing is many of DBA's and Developers don't know the SQL server internals Allocation Map.
I try to write everything in this post using my simple English.Hope, someone gets benefit by reading this article.

Database Physical Structure & Internals:
Database:
A Database is a collection of data that is stored (or) organized.We can manage and access data easily.
When we create a new database, by default two files will be created physically.

We can create more than one log files and data files (NDF).
  • Data file(MDF)
  • Log file(LDF)

Data File:

All the data will be stored in a data file.Internally the data’s are stored in data pages.
A database file has unique FileID and PageID.
Log files do not have pages. They have series of log records. I will cover the Log file section in my next article.

Data Page:

Data page is a fundamental unit of data storage. Each data page has 8KB size (128 pages per MB).Each page has 96 byte header system information.
All the data rows are stored in data page. Expect BLOB (text, ntext, image, xml, varchar (max), nvarchar & varbinary)

Extents:
An extents are fundamental units in which space is allocated to table (or) an index. Extents are 8 contiguous pages 64KB size. (16 extents per MB).

Two Types of extents
  • Uniform/Dedicated extents
  • Mixed extents

Uniform Extents:
Uniform extents are owned by a single object. All the 8-pages can be used for owning object.

Mixed Extents:
When we create a new table (or) an index by default pages are allocated to mixed extents first.
When the data grow and its reach 8 pages then its switched to uniform extents.

How the Data Storage Size Internally For Page & Extents:

Storage SizePageExtents
Size per KB8KB(1-Page)64KB (8-Pages)
Size per MB128 Pages16 Extents


IAM: (Index Allocation Map)

IAM page tracks with which extents within the specified GAM interval belongs to a single entity (or) file.A Database file have multiple files some of the DB size more than 4 GB

IAM interval size is 64000 extents i.e. 4GB

GAM: (Global Allocation Map)
Data files split up into GAM intervals (Conceptually)
Bit -->1 Free
An extent is available for allocation.
Bit --> 0 Allocated
An extent is already allocated.

GAM interval size is 64000 extents i.e. 4GB

SGAM: (Shared Global Allocation Map)
SGAM bitmaps is exactly same as GAM.


Bit --> 1 mixed extent one (or) more free page available
An extent is mixed one (or) more unallocated page available.
Bit --> 0 Either Uniform/Mixed extent No unallocated pages
An extent is either uniform (or) mixed all are already allocated.

SGAM interval size is 64000 extents i.e. 4GB


PFS: (Page Free Space)

PFS has tracks with which pages have free space.

A PFS page is the first page after the file header page in a data file (with page number 1).
Next comes a GAM (with page number 2) followed by an SGAM (page 3).
PFS interval size is 8088 pages i.e. 64MB

PFS does not have bitmap only have byte map.


DCM: (Differential Changed Map)
DCM tracks extents that have changed since last BACKUP DATABASE statement.

Bit --> 1 extent modified
Extent has been modified since the last BACKUP DATABASE.
Bit --> 0 extent is not modified
Extent has not been modified since the last BACKUP DATABASE.

BCM: (Bulk Changed Map)
BCM tracks extents that have been modified by bulk operations since last BACKUP LOG statement.
Bit --> 1 extent modified
Extent has been modified by bulk logged operation after the last BACKUP LOG statement.
Bit --> 0 extent is not modified
Extent has not been modified by bulk logged operations .Although the BCM pages appear by default in all databases.

How the Data Stored/Allocated Internally for the entity:
(Allocation Maps)

Type of storage
(Bit Type)
GAM
SGAM
DCM
BCM
Bit 1Extent FreeMixed Extent at least one free pages available Extent modifiedExtent modified
Bit 0Extent AllocatedEither Uniform (or) Mixed No free spaceExtent not modified
Extent not modified





Referred links :