Vous êtes sur la page 1sur 9

System databases:

SQL Server 2008 (and 2005) contains five special databases: master, model, tempdb, msdb, and
mssqlsystemresource (aka Resource). These databases are used by SQL Server for its own
maintenance and management.

Below are the further details of the databases.

Master:
The master database contains all of the system level information for SQL Server – all of the logins,
linked servers, endpoints, and other system-wide configuration settings. The master database is also
where SQL Server stores information about the other databases on this instance and the location of
their files. If the master database is not present, SQL Server cannot start.
Model:
The model database is used as a template whenever a new user database is created. You can change
most database properties, create users, stored procedures, tables, views, etc – whatever you do will
be applied to any new databases.
The nice thing is that you can create a guaranteed set of users, stored procedures, and options
(including collation options) by making changes to the model database. Once those changes are in
place, they are applied to every new database.

Msdb:
Msdb is used to store the SQL Server Agent information (Jobs, schedules, Alerts, Operators), dbamil.
One important item is that msdb holds backup history. Using the msdb tables (we can start by taking
a look at msdb.dbo.backupset), it’s possible to determine when each database \ was last backed up.

Resource Database:

The Resource database is a read-only database that contains all the system objects that are included with
SQL Server. SQL Server system objects, such as sys.objects, are physically persisted in the Resource
database, but they logically appear in the sys schema of every database. The Resource database does not
contain user data or user metadata.
The resource database is designed to make it easy for quick database upgrades. If new system
objects are being put in place, it is only necessary to swap out the resource database MDF file.
Typically, the only way to view the contents of the resource database is using the
OBJECT_DEFINITION system function.
SELECT OBJECT_DEFINITION(OBJECT_ID('sys.objects'))
The ID of the Resource database is always 32767. Other important values associated with the Resource
database are the version number and the last time that the database was updated.
SELECT SERVERPROPERTY('ResourceVersion');
GO
To determine when the Resource database was last updated, use:
SELECT SERVERPROPERTY('ResourceLastUpdateDateTime');
GO

Tempdb :

Purpose - Temporary database to store temporary tables (#temptable or ##temptable), table


variables, cursors, work tables, row versioning, create or rebuild indexes sorted in TempDB, etc. Each
time the SQL Server instance is restarted all objects in this database are destroyed, so permanent
objects cannot be created in this database.
• Prominent Functionality
• Manage temporary objects listed in the purpose above
• Additional Information
• Properly Sizing the TempDB Database
• Each time a SQL Server instance is rebooted, the TempDB database is reset
to its original state

Database files and file groups:


SQL Server maps a database over a set of operating-system files. Data and log information are never mixed
in the same file, and individual files are used only by one database. File groups are named collections of
files and are used to help with data placement and administrative tasks such as backup and restore
operations.
Files:
SQL Server databases have three types of files:
• Primary data files
The primary data file is the starting point of the database and points to the other files in the
database. Every database has one primary data file. The recommended file name extension for
primary data files is .mdf.
• Secondary data files
Secondary data files make up all the data files, other than the primary data file. Some databases may
not have any secondary data files, while others have several secondary data files. The recommended
file name extension for secondary data files is .ndf.
• Log files
Log files hold all the log information that is used to recover the database. There must be at least one
log file for each database, although there can be more than one. The recommended file name
extension for log files is .ldf.
SQL Server does not enforce the .mdf, .ndf, and .ldf file name extensions, but these extensions help
you identify the different kinds of files and their use.
Logical and Physical File Names
SQL Server files have two names:
logical_file_name
The logical_file_name is the name used to refer to the physical file in all Transact-SQL statements. The
logical file name must comply with the rules for SQL Server identifiers and must be unique among logical
file names in the database.
os_file_name
The os_file_name is the name of the physical file including the directory path. It must follow the rules for
the operating system file names.

Run below command and see physical(os_file_name) and ligical name of a database
sp_helpdb 'master'

File Size
SQL Server files can grow automatically from their originally specified size. When you define a file, you can
specify a specific growth increment. Every time the file is filled, it increases its size by the growth increment.
If there are multiple files in a file group, they will not autogrow until all the files are full. Growth then occurs
in a round-robin fashion.
Each file can also have a maximum size specified. If a maximum size is not specified, the file can continue to
grow until it has used all available space on the disk. This feature is especially useful when SQL Server is
used as a database embedded in an application where the user does not have convenient access to a
system administrator. The user can let the files autogrow as required to reduce the administrative burden of
monitoring free space in the database and manually allocating additional space.

Database Filegroups
Database objects and files can be grouped together in filegroups for allocation and administration
purposes. There are two types of filegroups:
Primary
The primary filegroup contains the primary data file and any other files not specifically assigned to another
filegroup. All pages for the system tables are allocated in the primary filegroup.
User-defined
User-defined filegroups are any filegroups that are specified by using the FILEGROUP keyword in a CREATE
DATABASE or ALTER DATABASE statement.
Log files are never part of a filegroup. Log space is managed separately from data space.
No file can be a member of more than one filegroup. Tables, indexes, and large object data can be
associated with a specified filegroup. In this case, all their pages will be allocated in that filegroup, or the
tables and indexes can be partitioned. The data of partitioned tables and indexes is divided into units each
of which can be placed in a separate filegroup in a database. For more information about partitioned tables
and indexes, see Partitioned Tables and Indexes.
One filegroup in each database is designated the default filegroup. When a table or index is created
without specifying a filegroup, it is assumed all pages will be allocated from the default filegroup. Only one
filegroup at a time can be the default filegroup. Members of the db_owner fixed database role can switch
the default filegroup from one filegroup to another. If no default filegroup is specified, the primary
filegroup is the default filegroup.
File and Filegroup Example
The following example creates a database on an instance of SQL Server. The database has a primary data
file, a user-defined filegroup, and a log file. The primary data file is in the primary filegroup and the user-
defined filegroup has two secondary data files. An ALTER DATABASE statement makes the user-defined
filegroup the default. A table is then created specifying the user-defined filegroup.
The following illustration summarizes the results of the previous example.
USE master;
GO
-- Create the database with the default data
-- filegroup and a log file. Specify the
-- growth increment and the max size for the
-- primary data file.
CREATE DATABASE MyDB
ON PRIMARY
( NAME='MyDB_Primary',
FILENAME=
'c:\Program Files\Microsoft SQL
Server\MSSQL10_50.MSSQLSERVER\MSSQL\data\MyDB_Prm.mdf',
SIZE=4MB,
MAXSIZE=10MB,
FILEGROWTH=1MB),
FILEGROUP MyDB_FG1
( NAME = 'MyDB_FG1_Dat1',
FILENAME =
'c:\Program Files\Microsoft SQL
Server\MSSQL10_50.MSSQLSERVER\MSSQL\data\MyDB_FG1_1.ndf',
SIZE = 1MB,
MAXSIZE=10MB,
FILEGROWTH=1MB),
( NAME = 'MyDB_FG1_Dat2',
FILENAME =
'c:\Program Files\Microsoft SQL
Server\MSSQL10_50.MSSQLSERVER\MSSQL\data\MyDB_FG1_2.ndf',
SIZE = 1MB,
MAXSIZE=10MB,
FILEGROWTH=1MB)
LOG ON
( NAME='MyDB_log',
FILENAME =
'c:\Program Files\Microsoft SQL
Server\MSSQL10_50.MSSQLSERVER\MSSQL\data\MyDB.ldf',
SIZE=1MB,
MAXSIZE=10MB,
FILEGROWTH=1MB);
GO
ALTER DATABASE MyDB
MODIFY FILEGROUP MyDB_FG1 DEFAULT;
GO

-- Create a table in the user-defined filegroup.


USE MyDB;
CREATE TABLE MyTable
( cola int PRIMARY KEY,
colb char(8) )
ON MyDB_FG1;
GO

Moving all SQL Server 2005, SQL Server 2008 or SQL Server 2008R2 System
Databases
Once you have reviewed the KB articles above, you can follow these steps to move all
system databases at once.
• Update the -d and -l startup parameters for SQL Server for the new location of
the master data and log file
• Issue ALTER DATABASE commands to change the file location for the model,
msdb and tempdb database files
alter database msdb modify
file(name='MSDBData',filename='D:\data\msdbdata.mdf')
• Stop SQL Server
• Move the MDF and LDF files to the new locations specified in steps 1 and 2 for the
master, model and msdb databases
• Start SQL Server
• Delete the old tempdb files
In addition to the master, model, msdb and tempdb databases SQL Server 2005 introduces the
mssqlsystemresource database. Microsoft recommends not moving this database, but if you
do want to move this database as well you will follow these steps. Note you cannot move the
mssqlsystemresource database for SQL Server 2008 or SQL Server 2008R2.
• Update the -d and -l registry startup parameters for SQL Server for the new
location of the master data and log file
• Issue ALTER DATABASE commands to change the file location for the model,
msdb and tempdb database files
• Stop SQL Server
• Move the MDF and LDF files to the new locations specified in steps 1 and 2 for the
master, model and msdb databases
• Put SQL Server in minimal configuration mode by adding these two startup
parameters -f and -T3608 and then start SQL Server
• Issue ALTER DATABASE commands for the mssqlsystemresource MDF and LDF
files using same path as the master database
• Move the MDF and LDF files to the location specified in step 6 for the
mssqlsystemresource database
• Stop SQL Server
• Remove the startup options added in step 5
• Start SQL Server
• Delete the old tempdb files

Pages and extents:


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.
Pages
In SQL Server, the page size is 8 KB. This means SQL Server databases have 128 pages per
megabyte. 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 type Contents

Data rows with all data, except text, ntext, image, nvarchar(max),
Data varchar(max), varbinary(max), and xml data, when text in row
is set to ON.

Index Index entries.

Large object data types:


• text, ntext, image, nvarchar(max), varchar(max),
varbinary(max), and xml data
Text/Image
Variable length columns when the data row exceeds 8 KB:
• varchar, nvarchar, varbinary, and sql_variant

Global Allocation Map,


Shared Global Allocation Information about whether extents are allocated.
Map

Information about page allocation and free space available on


Page Free Space
pages.

Information about extents used by a table or index per allocation


Index Allocation Map
unit.

Information about extents modified by bulk operations since the


Bulk Changed Map
last BACKUP LOG statement per allocation unit.

Differential Changed Information about extents that have changed since the last
Map BACKUP DATABASE statement per allocation unit.

Note: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
Extents
Extents are the basic unit in which space is managed. An extent is eight physically contiguous
pages, or 64 KB. This means SQL Server databases have 16 extents per megabyte.
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.

Managing Extent Allocations


SQL Server uses two types of allocation maps to record the allocation of extents:
• Global Allocation Map (GAM)
GAM pages record what extents have been allocated. Each GAM covers 64,000
extents, or almost 4 GB of data. The GAM has one bit for each extent in the interval it
covers. If the bit is 1, the extent is free; if the bit is 0, the extent is allocated.

• Shared Global Allocation Map (SGAM)

SGAM pages record which extents are currently being used as mixed extents and also
have at least one unused page. Each SGAM covers 64,000 extents, or almost 4 GB of
data. The SGAM has one bit for each extent in the interval it covers. If the bit is 1, the
extent is being used as a mixed extent and has a free page. If the bit is 0, the extent is
not used as a mixed extent, or it is a mixed extent and all its pages are being used.

Each extent has the following bit patterns set in the GAM and SGAM, based on its current
use.

Vous aimerez peut-être aussi