Sei sulla pagina 1di 30

Module 3

Database Structures
Module Overview

• Database Structure Internals


• Data File Internals
• tempdb Internals
Lesson 1: Database Structure Internals

• Database Components
• File Groups and Files
• Extents
• Page Structure and Page Types
• Record Structure and Record Types
• Allocation Bitmaps and Special Pages
• Page Allocation and Allocation Units
• Demonstration: Analyzing Database Structures
Database Components

Database

File Group
(one or more)

Data File
(one or more)

Extent
(mixed or
uniform)

Pages
(8 KB each)
File Groups and Files

File groups
• Logical grouping of data files
• Can bring administration and performance benefits
• FILESTREAM has its own file group
Database Files
• Data Files
• At least one per database
• Multiple files can help improve performance, aid
maintainability and circumvent operating system file size
limitations
• Transaction Log Files
• Usually only one per database—no performance advantage
from multiple files
Extents

Comprises eight physically continuous pages


• Mixed: shared between different database objects
• Uniform: owned by a single database object
Page Structure and Page Types

Pages:
8 KB in size Page Types
96-byte page header
Data (1)
Index (2)
Text (3 and 4)
Boot (13)
File Header (15)
PFS (11)
GAM (8)
SGAM (9)
IAM (10)
DIFF_MAP (16)
ML_MAP (17)
Record Structure and Record Types

• Record Structure
• Record Types:
• Data records
• Forwarding/forwarded records
• Index records
• Leaf level index records
• Non-leaf level index records
• Text records
• Versioned records
• Ghost records
Allocation Bitmaps and Special Pages

Page allocations are tracked by special pages:


• Global Allocation Map (GAM)
• Shared Global Allocation Map (SGAM)
• Differential Change Map (DCM)
• Bulk Change Map
• Page Free Space (PFS)
• Index Allocation Map (IAM)
Page Allocation and Allocation Units

• Page allocation
• The process of allocating new pages to an object

• Allocation units
• IN_ROW_DATA
• LOB_DATA
• ROW_OVERFLOW_DATA

• Page allocations can be analysed using DBCC


page
Demonstration: Analyzing Database Structures

In this demonstration, you will see how to:


• Analyze SQL Server page structures
• Analyze record structures
Lesson 2: Data File Internals

• Volume Configuration Best Practices


• Number of Data Files and Placement
• Physical I/O vs. Logical I/O
• Allocations Within Data Files
• Instant File Initialization
• Auto Grow and Auto Shrink
• Data File Shrinking
• Demonstration: Shrinking Databases
• Monitoring Database Files
Volume Configuration Best Practices

• RAID 10 for transaction log files


• RAID 10 or RAID 5 for database data files
• Store data and transaction log files on physically
separate volumes
• Defragment disks frequently
• Size data and transaction log files to avoid auto
growth
Number of Data Files and Placement

• For most databases a single data file is sufficient:


• Store data and transaction log files on physically
separate disks

• To improve performance:
• Place additional data files in a separate file group to
the primary data file and mark the new file group as
DEFAULT
• Split multiple data files across different physical disks
• Place heavily used tables and their nonclustered
indexes on different physical disks
Physical I/O vs. Logical I/O

• Logical I/O – pages read from disk


• Physical I/O – pages read from cache
• Buffer cache hit ratio – percentage of pages read
from cache without having to be read from disk
Allocations Within Data Files

• Allocation is simple in a file group that has a single


file
• In a multifile group, SQL Server uses:
• Round robin allocation
• Proportional fill
Instant File Initialization

• Improves performance by skipping zeroing of


data pages on file creation and growth
• Disabled by default: security risk
• Best practice is to enable following assessment of
security risk
Auto Grow and Auto Shrink

• Auto Grow
• Automatically expands a file when space is low
• Manually manage file size for better performance
• Auto grow best practices:
• Leave enabled to avoid downtime in case file size limit is reached
• Set to a fixed rather than a percentage
• Auto Shrink
• Shrinks data file automatically to release disk space
• Use with caution as:
• Auto shrink causes index fragmentation
• It’s uncontrollable and affects performance
• It can lead to growth-shrink cycle severely affecting performance
Data File Shrinking

Shrink
• Causes index fragmentation
• Resource intensive
• Degrades performance
Best used for
• Emptying a file before removing it
• Changing a file or file group to read only
• Reclaiming free space following large delete operation
Alternative
• Move all indexes to a new file group
• Move heaps with shrink. Heaps are not fragmented with shrink
• Remove old file group
Demonstration: Shrinking Databases

In this demonstration, you will see:


• How to check free space in database files
• How to shrink a data file
• The impact of shrinking data files on index
fragmentation
Monitoring Database Files

• Database file configuration


• SSMS database properties
• sp_helpfile and sp_helpfilegroup
• sys.database_files and sys.filegroups
• sys.master_files
• sys.dm_db_file_space_usage

• Database file activity


• SSMS Activity Monitor – Data File I/O
• Wait statistics – PAGEIOLATCH_*, WRITELOG
• sys.dm_io_virtual_file_stats
Lesson 3: tempdb Internals

• tempdb Usage
• Version Store Internals
• tempdb Configuration
• Demonstration: Monitoring tempdb Usage
tempdb Usage

tempdb
• Stores:
• Internal objects
• Version store
• Some user objects
• Recreated at startup
• Must not be allowed to run out of space
• Should be actively monitored
Version Store Internals

• Version store:
• Stores all version records from all databases
• New allocation unit created every minute
• Cleanup happens every minute
• Nonlogged operation
tempdb Configuration

• tempdb configuration
• Enable instant file initialization
• Size for expected workload
• Isolate tempdb data files
• Use multiple data files
• Enable auto growth as a contingency
• Prohibited operations
• Backup and restore
• Add file groups
• Change collation
• Set offline or read only
Demonstration: Monitoring tempdb Usage

• In this demonstration, you will see how to


monitor tempdb usage
Lab: Database Structures

• Exercise 1: Exploring Page Allocation Structure


• Exercise 2: Configuring Instant File Initialization
• Exercise 3: Reconfiguring tempdb Data Files

Logon Information
Virtual machine: 10987C-MIA-SQL
User name: ADVENTUREWORKS\Student
Password: Pa55w.rd

Estimated Time: 30 minutes


Lab Scenario

You have reviewed the AdventureWorks database


and noticed high wait statistics for CPU, memory,
I/O, blocking, and latching. In this lab, you will
explore database structures and internals for a
user database. You will enable instant file
initialization and note the performance
improvement. Finally, you will reduce tempdb
latch contention by adding more data files to
tempdb.
Lab Review

• You will now be able to:


• Explore database structures and data file internals.
• Improve performance by enabling instant file
initialization.
• Reduce tempdb latch contention.
Module Review and Takeaways

This module covered database structures, data files,


and tempdb internals. It focused on the
architectural concepts and best practices related to
data files, for user databases and tempdb, which
enable you to configure SQL Server for maximum
performance.

Potrebbero piacerti anche