Professional Documents
Culture Documents
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.
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 :
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
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
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.
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.
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.