All about Storage - Series 2 Defining Data
=> Data & Data Types
=> Text and Image Locations
=> Page Structures & Internals
by Dr. Subramani Paramasivam
5. Selecting the Correct Data Types
• Identify
• Data
• Data types
• Organize
• Categorize
• integer,
• Currency,
• Datetime,
• Geography,
• Money,
• Varchar,
• NVarchar
ID Name Age Sex Date_of_Joining Salary
1 Mark 42 M 04/08/2011 £55,000.00
2 Peter 32 M 10/03/2007 £48,000.00
3 Julie 36 F 11/11/2009 £49,000.00
Column_name Data_type Length
ID Integer
Name Varchar 10
Age Integer
Sex Char 2
Date_Of_Joining Datetime
Salary Money
www.dageop.com
All About Storage
6. Selecting the Correct Data Types
• Can’t store text in a numeric field.
• Reserves the storage space with the chosen datatype.
www.dageop.com
All About Storage
7. Selecting the Correct Data Types
Data Type Explanation
Character Alphanumeric Text
Example : A customer address
Currency Monetary Amounts
Example : The price of an item
Date Chronological data consisting of month,
day and year.
Example : Date of arrival
DateTime Chronological data consisting of month,
day, year, hours, minutes and seconds
Example : Date and Time of arrival
Data Type Explanation
Numeric Integers or Decimal numbers
Example : The quantity of Items Ordered
Double A double- precision floating point number
Example : Scientific Data requiring a high
degree of precision.
Float Same as Numeric
Integer Numeric Value With no decimals
Example: Line number in a order
Memo Alphanumeric text of indeterminate length
Example : Notes about a phone call in a
phone log
www.dageop.com
All About Storage
8. Selecting the Correct Data Types
Column_Name Datatype
BusinessID Integer
NationalIDNumber NVARCHAR()
LoginID NVARCHAR()
OrganizationNode NVARCHAR()
OrganizationLevel Integer
JobTitle NVARCHAR()
BirthDate Datetime
Marital Status Char
Column_Name Datatype
Gender Char
HireDate Datetime
SalariedFlag Bit
VacationHours Smallint
SickLeaveHours Smallint
CurrentFlag Bit
Rowguid Uniqueidentifier
ModifiedDate datetime
www.dageop.com
All About Storage
9. Selecting the Correct Data Types
Humanresource.Employee
www.dageop.com
All About Storage
11. Text and Image Locations
www.dageop.com
All About Storage
12. Text and Image Locations
• SQL Server stores character strings more than 8k Characters and binary data more than 8k
bytes as special data types TEXT and IMAGE.
• The below scenario will help you to understand the use of TEXT and Image data type in
much deeper.
www.dageop.com
All About Storage
13. Text and Image Locations
• By default text and image values are not considered as part of the data row but as
collection of pages of their own.
• For single text and image column the data page contain 16 byte pointer which in turn will
point to the location of the initial page of the text or image data.
• The page that contains text and image data are also of 8kb size. A single text or image
page can hold text, ntext or image data from different columns and also from different
rows but from a single table. This supports minimizing the storage.
• A table contains text and image data in the database will have their own single set of
pages. The information about starting location of the collection of pages is stored in the
sysindexes system table.
• The text or image collection will have an index id of 255 always.
www.dageop.com
All About Storage
14. Text and Image Locations
text
ntext
Image
varchar(max)
nvarchar(max)
varbinary(max)
Decision:
• Filestream Vs Database
• <256KB = In DB
• >256KB = Filestream in hard drive
• Separate table for storing images.
• A separate FG is highly recommended
REPLACE
www.dageop.com
All About Storage
15. Text and Image Locations
DATATYPE SIZE
Database <256KB
Filestream in a location >256KB
Varbinary(max) maximum size of 2 GB.
Varbinary(max) with Filestream option images larger than 2 GB
www.dageop.com
All About Storage
16. Text and Image Locations
Read Throughput After Bulk load
0
2
4
6
8
10
12
256K 512K 1M
Object Size
MB/sec
Database
Filesystem
Databases are fastest on small
objects. As object size increases,
NTFS throughput improves faster
than SQL Server throughput.
Read Throughput After Two Overwrites
0
2
4
6
8
10
12
256K 512K 1M
Object Size
MB/sec
Database
Filesystem
Read Throughput After Four Overwrites
0
1
2
3
4
5
6
7
8
9
10
256K 512K 1M
Object Size
MB/sec
Database
Filesystem
Fragmentation causes read performance to degrade over time.
The filesystem is less affected by this than SQL Server. Over
time NTFS outperforms SQL Server when objects are larger
than 256KB.
Credit: Microsoft Research Paper by Russell Sears, Catharine Van Ingen, and Jim Gray, 2006
www.dageop.com
All About Storage
17. Text and Image Locations
• Text and Image information is presented externally to user as a lengthy string of bytes but
internally the information is stored within a set of pages.
• The pages are not organized sequentially but are logically organized as a B tree Structure
• If any operation addresses to find any data in the middle of the B TREE Structure, the operation can be easily diverted to
the exact page instead of going to the page in sequential manner.
B TREE Structure
www.dageop.com
All About Storage
20. Page Structures & Internals
• 8kb per page is the basic unit of IO.
• Allocation units GAM, SGAM.
• Types of pages to store variety of data like data, index, BLOB etc.
• Three sections Page Header, Actual data and Row offset array
HEADER
ROW 1
ROW 2
ROW 3
ROW 4
3 2 14
Empty space to be
used
Row Offset
www.dageop.com
All About Storage
21. Page Structures & Internals
• Pages allocated to a table can be viewed with undocumented DBCC IND Command
• Page type 10 (IAM page With Page ID 154) and 1 (data page and the page id is 153).
www.dageop.com
All About Storage
22. Page Structures & Internals
• Page Type – The Type of Page
• 1 – Datapage
• 2 – Index Page
• 3 & 4 - Text page
• 8 – GAM Page
• 9 – SGAM Page
• 10 –IAM page
• 11- PFS Page
Description of columns displayed from DBCC IND
PageFID File ID of Page
PagePID Page ID
IAMFID,IAMPID File ID & Page ID -> IAM mapping
ObjectID object ID(Table)
IndexID Clustered index (IndexID = 1), Non clustered index (IndexID = 2)
PartitionNumber Number of Partition which holds data & index pages
PartitionID ID of Partition which holds data & index pages
iam_chain_type In-row data,Row-Overflow data
PageType 1=Data page,2=Index page,3&4=Text page,10=IAM
IndexLevel 0=leaf level
NextPageFID,NextPagePID,PrevPageFID,P
revPagePID Next & Previous Page ID 's & File ID 's of a page
www.dageop.com
All About Storage
23. Page Structures & Internals
• Index Level – What level the page is in INDEX
• Next Page FID and Next Page PID – The Page ID of the next page
• Prev Page FID and Prev Page PID - The Page ID of the Previous page
• Now let us examine the row data stored in the page.
DBCC TRACEON (3604); -- By default the output is sent to error log
DBCC PAGE ()
• 4 levels of output
1. Buffer (about memory allocation)
www.dageop.com
All About Storage
24. Page Structures & Internals
2. Page header - having 96 byte of size and same for all the pages
www.dageop.com
All About Storage
25. Page Structures & Internals
3. Slot section - Each records are stored in a slot. Slot 0 will have the first record in the first page and slot 1 will have
second record and so on, but there is no mandatory that the slots should be in physical order of data.
www.dageop.com
All About Storage
26. Page Structures & Internals
4. Row offset table and the code should be executed with option number 1 for offset table
DBCC PAGE (‘Database’, 1, pagenumber, 1)
www.dageop.com
All About Storage
28. Review
Data And Data Types
Choosing The Correct Data Type
Text And Image Location
Text and Image Data Requirement and storage
Page structures & Internals
Page
Anatomy of Page
www.dageop.com
All About Storage