0% found this document useful (0 votes)
56 views31 pages

Jobsheet 2

The document discusses how data is stored in SQL Server databases using three types of files: primary data files, secondary data files, and transaction log files. It describes the purpose and characteristics of each file type, including recommended file extensions and how data is organized on pages within the files. The document also covers best practices for determining file placement across multiple disks or storage locations and ensuring sufficient file capacity for future database growth.

Uploaded by

Irham Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views31 pages

Jobsheet 2

The document discusses how data is stored in SQL Server databases using three types of files: primary data files, secondary data files, and transaction log files. It describes the purpose and characteristics of each file type, including recommended file extensions and how data is organized on pages within the files. The document also covers best practices for determining file placement across multiple disks or storage locations and ensuring sufficient file capacity for future database growth.

Uploaded by

Irham Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Jurusan Teknologi Informasi Politeknik Negeri Malang

Jobsheet-1: File and Filegoups


Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

1. How Data is Stored in SQL Server

1.1 Key Points


The data stored by SQL Server databases is contained within a set of files allocated for
the database to use. There are three types of files that are used by SQL Server: primary data
files, secondary data files, and transaction log files.

1.2 Primary Data Files


The primary data file is the starting point of the database. Every database has a single
primary data file. As well as holding data in the same way that other database files do, the
primary data file holds pointers to the other files in the database. Primary data files typically
use the file extension .mdf. While the use of this file extension is not mandatory, using .mdf as
a file extension for primary data files is highly recommended.

1.3 Secondary Data Files


Secondary data files are optional, user-defined, additional data files that can be used to
spread the data through more files for performance and/or maintenance reasons. Secondary
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

files can be used to spread data across multiple disks by putting each file on a different disk
drive. Additionally, if a database exceeds the maximum size for a single Windows file, you can
use secondary data files so the database can continue to grow. The recommended extension for
secondary data files is .ndf.

1.4 Data Files Pages


Pages in a SQL Server data file are numbered sequentially, starting with zero for the
first page in the file. Each file in a database has a unique file ID number. To uniquely identify
a page in a database, both the file ID and the page number are required. Each page is 8KB in
size. After allowing for header information that is needed on each page, there is a region of
8096 bytes remaining for holding data. Data rows can hold fixed length and variable length
column values. All fixed length columns of a data row need to fit on a single page, within an
8060 byte limit. Data pages only hold data from a single database object, such as a table or an
index.
The first allocation for an object is at the page level, and always comes from a mixed
extent. If they are free, other pages from the same mixed extent will be allocated to the object
as needed. Once the object has grown bigger than its first extent, then all future allocations are
from uniform extents.
In both primary and secondary data files, a small number of pages is allocated to
tracking the usage of extents within the file.

1.5 Log File


Log files hold information that is used to recover the database when necessary. There
must be at least one log file for each database. The recommended extension for log files is .ldf.
All transactions are written to the log file using the write-ahead logging (WAL)
mechanism to ensure the integrity of the database in case of a failure and to support rollbacks
of transactions.
When data pages need to be changed, they are fetched into memory and changed in
memory. The "dirty" pages are then written to the transaction log in a synchronous manner. At
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

some time later, during a background process known as a "checkpoint" process, the dirty pages
are written to the database files. For this reason, the pages that are contained in the transaction
log are critical to the ability of SQL Server to recover the database to a known committed state.
Transaction logs are discussed in detail in later modules in this course.

2. Determining File Placement and Number of Files

2.1 Key Points


It is important to isolate log and data files for both performance and recovery reasons.
This isolation needs to be at the physical disk level.

2.2 Access Patterns


The access patterns of log and data files are very different. Data access on log files
consists primarily of sequential, synchronous writes to log files, with occasional random disk
access. Data access on data files is predominantly asynchronous random disk access to the data
files from the database. A single physical drive system does not tend to provide good response
times when these types of data access are combined.

2.3 Recovery
While disk subsystems are increasing in reliability, failures do still occur. If a SQL
Server data file is lost, the database could be restored from a backup and the transaction log
reapplied to recover the database to a recent point in time. If a SQL Server log file is lost, the
database can be forced to recover from the data files, with the possibility of some data loss or
inconsistency in the database. But if both the data and log files are on a single disk subsystem
that is lost, the recovery options usually involve restoring the database from an earlier backup
and losing all transactions since that time. Isolating data and log files can help to avoid the
worst impacts of drive subsystem failures.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

2.4 Logical vs, Physical Separation


It is best practice to do the separation on the physical level. With storage area network
(SAN) systems, it is easy to configure storage so that it appears to be separated but the
separation is only logical. The same underlying physical storage is being used. This is generally
a poor design choice.

2.5 Data File Management


Ideally, all data files that are defined for a database should be the same size. Data is
spread evenly across all available data files. The main performance advantages from doing this
come when the files are spread over different storage locations.

A number of management advantages are gained from allocating multiple data files:

• The main management advantage gained from allocating multiple data files is the
possibility of moving files and part of the data later.
• Another management advantage gained through the use of multiple data files is that if
a database file is being restored separately, the recovery time can be minimized. This
could be useful where only part of the data was corrupt.
• Splitting a database across multiple data files can increase the parallelism in the I/O
channel.
• The final advantage is that if a database exceeds the maximum size for a single
Windows file, you can

use secondary data files so the database can continue to grow.


Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

3. Ensuring Sufficient File Capacity

3.1 Key Points


It is important to consider capacity planning. To perform capacity planning, you should
estimate the maximum size of the database, indexes, transaction log, and tempdb, through a
predicted growth period. For most sites, you should aim to create database files that are large
enough to handle the data expected to be stored in the files over a twelve month period.

3.2 Realistic Planning


Performance and capacity testing for SQL Server is usually accomplished through the
load testing of the actual application(s) that will be using the SQL Server, rather than trying to
estimate the requirements.

3.3 Autogrowth vs. Planned Growth


SQL Server can automatically expand a database according to growth parameters that
were defined when the database files were created. While the options for autogrowth should
be enabled to prevent downtime when unexpected growth occurs, it is important to avoid the
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

need for SQL Server to ever autogrow the files. Instead, you should monitor file growth over
time and ensure that files are large enough for many months or years.
Many administrators are concerned that larger database files will somehow increase the
time it takes to perform backups. The size of a SQL Server backup is not related directly to the
size of the database files. Only used portions of the database are backed up.
One significant issue that arises with autogrowth is that there is a tradeoff related to the
size of the growth increments. If a large increment is specified, a significant delay can be
experienced in the execution of the T-SQL statement that triggered the need for growth. If a
too small increment is specified, the filesystem can become very fragmented and the database
performance can suffer because the data files have been allocated in small chunks all over a
disk subsystem.

3.4 Log File Growth Planning


If the transaction log is not set up to expand automatically, the transaction log can run
out of space when certain types of activity occur in the database. For example, performing
large-scale bulk operations, such as bulk import or index creation, can cause the transaction
log to fill rapidly.
In addition to expanding the size of the transaction log, it is possible to truncate a log
file. Truncating the log purges the file of inactive, committed, transactions and allows the SQL
Server Database Engine to reuse this unused part of the transaction log.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

4. System Databases Supplied with SQL Server

4.1 Key Points


There are five system databases that are created during installation. They are master,
msdb, model, tempdb, and resource. These databases contain metadata and cannot be dropped.

master
The master database contains all system-wide information. Anything that is defined at the
server instance level is typically stored in the master database. If the master database is
damaged or corrupted, SQL Server will not start, so it is very important to backup the master
database on a regular basis.

msdb
The msdb database holds information for the SQL Server Agent. Jobs, operators, and alerts
which are stored in the msdb database. It is also important to perform a backup of the msdb
database regularly, to ensure that jobs, schedules, and history for backups, restores, and
maintenance plans are not lost. In earlier versions of SQL Server, SQL Server Integration
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

Services (SSIS) packages were often stored within the msdb database also. In SQL Server
2012, these should be stored in the dedicated SSIS database instead.

model
The model database is the template on which all user databases are based. Any new database
that is created uses the model database as a template. Any objects created in the model database
will be present in all new databases that are created on the server instance. Many sites do not
ever modify the model database. Note that even though the model database does not seem
overly important, SQL Server will not start if the model database is not present.

tempdb
The tempdb database holds temporary data. This database is truncated or created every time
that SQL Server starts so there is no need to perform a backup on this database. In fact, there
is no option to perform a backup of the tempdb database. The tempdb database is discussed
further in the next topic.

resource
The resource database is a read-only hidden database that contains system objects that are
mapped to the sys schema in every database. This database also holds all system stored
procedures, system views and system functions. In SQL Server versions before SQL Server
2005 these objects were defined in the master database.

5. Overview of tempdb
5.1 Key Points
The tempdb database consists of the internal objects, the row version store and user
objects. The performance of the tempdb database is critical to the overall performance of most
SQL Server installations.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

5.2 Internal Objects


Internal objects are objects that are used by SQL Server for its own operations. Internal
objects include work tables for cursor or spool operations, temporary large object (LOB)
storage, work files for hash join or hash aggregate operations, and intermediate sort results.
Transactions that are associated with snapshot-related transaction isolation levels can
cause alternate versions of rows to be briefly maintained in a special row version store within
tempdb. Row versions can also be produced by other features like online index rebuilds,
Multiple Active Result Sets (MARS) and triggers.

5.3 User Objects


Most objects that reside in the tempdb database are user-generated objects and consist
of temporary tables, table variables, and the result sets of multi-statement table-valued
functions, and other temporary row sets.

5.4 size of tempdb


As tempdb is used for so many purposes, it is difficult to predict the required size for
tempdb in advance. Appropriate sizes for tempdb should be carefully tested and monitored in
real live scenarios for new installations.
Running out of disk space in the tempdb database can cause significant disruptions in
the SQL Server production environment and can prevent applications that are running from
completing their operations. You can use the sys.dm_db_file_space_usage dynamic
management view to monitor the disk space that is used by the tempdb files. Additionally, to
monitor the page allocation or deallocation activity in tempdb at the session or task level, you
can use the sys.dm_db_session_space_usage and sys.dm_db_task_space_usage dynamic
management views.
By default, the tempdb database automatically grows as space is required, because the
MAXSIZE of the files is set to UNLIMITED. Therefore, tempdb can continue growing until
space on the disk that contains tempdb is exhausted.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

5.5 Multiple Files


To overcome I/O restrictions but also to avoid a concept called latch contention that
affects the allocation pages of tempdb data pages, increasing the number of files can improve
the overall performance of SQL Server systems. Do not, however, create too many files. As a
general rule it is advised to have 0.25-1 file per core nowadays. As a general rule it can be said
that the ratio should be lower with the increase of cores on the system but the optimal
configuration must be identified by doing real live tests.

Demo 1 : Working with tempdb

1. Click Start, click All Programs, click Microsoft SQL Server 2012, click SQL Server
Management Studio. In the Connect to Server window, type Proseware and click
Connect. From the File menu, click Open, click Project/Solution, navigate to
10775A_04_PRJ.ssmssln and click Open.
2. From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql
script file from within Solution Explorer.
3. Open the 11 – Demonstration 1A.sql script file.
4. Follow the instructions contained within the comments of the script file.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

6 Creating User Databases

6.1 Key Points


Databases can be created with either the GUI in SSMS or via the CREATE
DATABASE command in T-SQL. The CREATE DATABASE command offers more flexible
options but the GUI is easier to use. This topic will concentrate on the CREATE DATABASE
command but is equally applicable to the options chosen in the GUI within SSMS.

CREATE DATABASE
Database names must be unique within an instance of SQL Server and comply with the
rules for identifiers. A database name is of data type "sysname" which is currently defined as
nvarchar(128). This means that up to 128 characters can be present in the database name and
that each of the characters can be chosen from the double-byte Unicode character set. While
database names can be quite long, you will find that long names become awkward to work
with.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

6.2 Data Files


As discussed earlier in this module, a database must have a single primary data file and
one log file. The ON and LOG ON clauses of the CREATE DATABASE command specify
the required files.
In the example shown in the slide, the database named Branch is being created. It is
comprised of two files: The primary data file has an initial file size of 100MB and a maximum
file size of 500MB. It will grow by 20% of its current size whenever autogrowth needs to occur.
The log file has an initial file size of 20MB and has no limit on maximum file size. Each time
it needs to autogrow, it will grow by a fixed 10MB allocation.

6.3 Collations and Default Values


A specific collation can be allocated at the database level if required. If not specified,
the collation will default to the collation that was specified for the server instance during SQL
Server installation. Keeping individual databases with the same collation as the server is
considered a best practice.
While it is possible to create a database by just providing the database name, you would
create a database that is based on the model database and with data and log files in the default
locations. It is unlikely to represent a best practice.
7. Configuration Database Options
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

7.1 Key Points


Each database has a set of options that can be configured. These options are unique to
each database and do not affect other databases. All database options are taken from the model
database when you create a database and can be changed by using the SET clause of the
ALTER DATABASE statement or via the Properties page for each database in SSMS.

7.2 Categories of Options


There are several categories of database options:
• Auto options - Control certain automatic behaviors. As a general guideline, Auto Close
and Auto Shrink should be turned off on most systems but Auto Create and Update
Statistics should be turned on.
• Cursor options - Control cursor behavior and scope. In general, the use of cursors when
working with SQL Server is not recommended apart from particular applications such
as utilities. Cursors are not discussed further in this course but it should be noted that
the overuse of cursors is a common cause of performance issues.
• Database Availability Options - Control whether the database is online or offline, who
can connect to the database, and whether the database is in read-only mode.
• Maintenance and recovery options such as:
o Recovery Model - Database recovery models will be discussed in Next Section.
o Page Verify - Early versions of SQL Server offered an option called Torn Page
Detection. This option caused SQL Server to write a small bitmap across each
disk drive sector within a database page. There are 512 bytes per sector, so this
means that there are 16 sectors per database page (8KB). This was a fairly crude
yet reasonably effective way to detect a situation where only some of the sectors
that were required to write a page, were in fact written. In SQL Server 2005, a
new CHECKSUM verification option was added. The use of this option causes
SQL Server to calculate and add a checksum to each page as it is written and to
recheck the checksum whenever a page is retrieved from disk.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

8. Instant File Initialization


8.1 Key Points
Part of the security provided by the Windows operating system is that a user is not
provided with raw access to disk space that was previously used by another user, without first
overwriting the disk space.

8.2 Instant File Initialization


The time taken to overwrite the space allocated to data files for use with SQL Server
can be substantial. When a 200GB file is allocated, 200GB of data needs to be written.
The main risk associated with not overwriting the disk space is that an administrator
could read the previous contents of the disk. If this risk is considered minimal, SQL Server can
take advantage of instant file initialization (IFI) to avoid the time taken when clearing existing
data.
SQL Server can request uninitialized space from the operating system but only when it
has sufficient rights to request this. If the Perform Volume Maintenance Tasks right has been
assigned to the SQL Server service account, SQL Server can use instant file initialization. This
option was introduced in SQL Server 2005 and only affects data files.
Another advantage of this option is that it decreases the time it takes to restore databases
as the disk space for the restored database does not need to be overwritten before it is reused.

Demo 2 : Creating Databases

1. If Demo 1was not performed:


• Click Start, click All Programs, click Microsoft SQL Server 2012, click SQL Server
Management Studio. In the Connect to Server window, type Proseware and click
Connect. From the File menu, click Open, click Project/Solution, navigate to
10775A_04_PRJ.ssmssln and click Open.
• From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql script
file from within Solution Explorer.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

2. Open the 21 – Demonstration 2A.sql script file.


3. Follow the instructions contained within the comments of the script file

9. Altering Databases
9.1 Key Points
When databases are in operation, they may need to be modified. The most common
requirement is to add additional space by either expanding existing files or by adding additional
files.

ALTER DATABASE
Expanding files and adding files can both be done through the ALTER DATABASE
statement or via the GUI in SSMS. Another option that you might need to implement is to drop
a file. SQL Server will not allow you to drop a file that is currently in use within the database.
Dropping a file has to be done in two steps. First the file has to be emptied using the
EMPTYFILE option of DBCC SHRINKFILE and then it can be removed using the ALTER
DATABASE command.

10. Expanding and Shrinking Databases Files


10.1 Key Points
By default, SQL Server automatically expands a database according to growth
parameters defined when the database files were created. You can also manually expand a
database by allocating additional space to an existing database file or by creating a new file.
You may have to expand the data or transaction log space if the existing files are becoming
full.
If a database has already exhausted the space allocated to it and it cannot grow a data
file automatically, error 1105 is raised. (The equivalent error number for the inability to grow
a transaction log file is 9002). This can happen if the database is not set to grow automatically
or if there is not enough disk space on the hard drive.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

10.2 Adding Files


One option for expanding the size of a database is to add files to the database. You can
add files using either the GUI in SSMS or by the ALTER DATABASE ... ADD FILE command
in T-SQL. You will see an example that shows the addition of a file in Demo 3.

10.3 Growing Files


When expanding a database, you must increase the size of the database by at least 1
MB. Ideally, any file size increase should be much larger than this. Increases of 100MB or
more are common.
When a database is expanded, the new space is immediately made available to either
the data or transaction log file, depending on which file was expanded. When you expand a
database, you should specify the maximum size to which the file is permitted to grow. This
prevents the file from growing until disk space is exhausted. To specify a maximum size for
the file, use the MAXSIZE parameter of the ALTER DATABASE statement, or use the
Restrict filegrowth (MB) option when you use the Properties dialog box in SQL Server
Management Studio to expand the database.

10.4 Transaction Log


If the transaction log is not set up to expand automatically, the transaction log can run
out of space when certain types of activity occur in the database. In addition to expanding the
size of the transaction log, the log file can be truncated. Truncating the log purges the file of
inactive, committed transactions and allows the SQL Server Database Engine to reuse this
unused part of the transaction log. If there are active transactions, the log file might not be able
to be truncated and expanding the log file might be the only available option.

10.5 Shrinking a Database


Each file within a database can be reduced in size by removing unused pages. Although
the Database Engine will reuse space effectively, there are times when a file no longer needs
to be as large as it once was. Shrinking the file may then become necessary but shrinking should
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

be considered a rarely used option. Both data and transaction log files can be reduced, or
shrunk. The database files can be shrunk manually, either as a group or individually, or the
database can be set to shrink automatically at specified intervals.

10.6 Methods for Shrinking


You can shrink a database or specific database files using the DBCC
SHRINKDATABASE and DBCC SHRINKFILE commands. The DBCC SHRINKFILE is
preferred as it provides much more control of the operation than DBCC SHRINKDATABASE.
Regular shrinking of files tends to lead to regrowth of files. For this reason, even though
SQL Server provides an option to automatically shrink databases, this should only be used in
rare situations, as for most databases, enabling this option will cause substantial fragmentation
issues on the disk subsystem. It is best practice to perform any form of shrink operation only if
absolutely needed.

Demo 3 : Altering Databases

1. If Demo 2 was not performed:


• Click Start, click All Programs, click Microsoft SQL Server 2012, click SQL
Server Management Studio. In the Connect to Server window, type Proseware and
click Connect. From the File menu, click Open, click Project/Solution, navigate to
10775A_04_PRJ.ssmssln and click Open.
• From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql
script file from within Solution Explorer.
• Open the 21 – Demonstration 2A.sql script file and follow the instructions contained
within it.
2. Open the 22 – Demonstration 2B.sql script file.
3. Follow the instructions contained within the comments of the script file.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

11. Working with Filegroups

11.1 Key Points


Filegroups are named collections of data files and are used to simplify data placement
and administrative tasks such as backup and restore operations. Using files and filegroups may
improve database performance, because it lets a database be created across multiple disks,
multiple disk controllers, or RAID systems.

11.2 File Placement


Files and filegroups enable data placement, because a table or an index can be created
in a specific filegroup. When using a statement such as CREATE TABLE, a parameter can be
supplied to specify the filegroup that the object should be created on. This can improve
performance, because the I/O for specific tables can be directed at specific disks. For example,
a heavily used table can be put on a single file in one filegroup, located on one disk, and the
other less heavily accessed tables in the database can be put on the other files in another
filegroup, located on other disks.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

11.3 Proportional Fill


Filegroups use a proportional fill strategy across all the files within each filegroup. As
data is written to the filegroup, the SQL Server Database Engine writes an amount of data
proportional to the free space in a file to each file within the filegroup, instead of writing all
the data to the first file until full. As soon as all the files in a filegroup are full, the Database
Engine automatically expands one file at a time in a round- robin manner to allow for more
data, provided that the database is set to grow automatically.

11.4 File Groups and Other Features


Filegroups can be used in combination with partitioned tables and indexes, which are
advanced topics, out of scope of this course.
One filegroup is designated as the default filegroup. When objects are created without
specifying a filegroup, the default filegroup is used. Unless you change the default filegroup,
the PRIMARY filegroup will be the default.
Filegroups can also be created as read-only filegroups. Read-only filegroups provide
you the following benefits:
• They can be compressed (using NTFS compression).
• During recovery, you do not need to apply logs to recover a read-only file group.
• The data is protected from accidental modifications.
• Archive data can be stored on cheaper storage.

Demo 4 : Filegroups
1. If Demo 2was not performed:
• Click Start, click All Programs, click Microsoft SQL Server 2012, click SQL Server
Management Studio. In the Connect to Server window, type Proseware and click
Connect. From the File menu, click Open, click Project/Solution, navigate to
10775A_04_PRJ.ssmssln and click Open.
• From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql
script file from within Solution Explorer.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

• Open and execute the 21 – Demonstration 2A.sql script file.

2. Open the 23 – Demonstration 2C.sql script file

12. Overview of Detach and Attach

12.1 Key Points


Databases can be detached using SSMS or sp_detach_db. Detaching a database does
not remove the data from the data files or remove the data files. All that is removed are the
metadata entries for the database. The detached database will no longer appear in the list of
databases shown in SSMS or reported by the sys.databases system view. The detached data
files can be moved or copied to another instance and reattached.

UPDATE STATISTICS
SQL Server maintains a set of statistics on the distribution of data within tables and
indexes. As part of the detach process, an option is provided to perform an UPDATE
STATISTICS operation on table and index statistics prior to the detach. While this might be
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

useful if the database is to be reattached as a read-only database, in general it is not a good


option to use while detaching a database.

12.2 Detachable Databases


Not all databases can be detached. Databases that are replicated, mirrored or in a suspect
state cannot be detached.
A more common problem that makes a database unable to be detached is that
connections are open to the database at the time the detach operation is attempted. All
connections must be dropped before detaching the database. SQL Server Management Studio
offers an option to force connections to be dropped during this operation.

12.3 Attaching Databases


SSMS offers an option to attach databases. Databases can also be attached using the
CREATE DATABASE ... FOR ATTACH command.
A common problem when databases are reattached is that database users can become
"orphaned". You will see how to deal with this problem in a later module.

13. Moving User Database Files


13.1 Key Points
Moving the files of a database requires the database to be taken offline. Moving a
database should be performed in a maintenance window.

13.2 Options for Moving Databases


In SSMS, the only option available for moving database files is to detach the database,
move the data files to their new location, and then attach them back into the instance. While
using detach and attach to move user database files can be used, the ALTER DATABASE can
also be used to move database files and is preferred, unless the database is being moved to
another server instance.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

The process of moving user database files requires the use of the logical names of the
database files. You can see the logical names of existing files by executing the following code:

SELECT * FROM sys.database_files;

The use of detach and attach for moving user database files across instances will be
shown in the next demonstration.

Demo 5 : Detach and Attach

1. If Demo 1 was not performed:


• Click Start, click All Programs, click Microsoft SQL Server 2012, click SQL Server
Management Studio. In the Connect to Server window, type Proseware and click
Connect. From the File menu, click Open, click Project/Solution, navigate to
10775A_04_PRJ.ssmssln and click Open.
• From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql
script file from within Solution Explorer.
2. Open the 31 – Demonstration 3A.sql script file.
3. Follow the instructions contained within the comments of the script file.

14. Moving System Database Files


14.1 Key Points
All system databases can be moved to new locations, to help balance I/O load, except
the resource database. The process for moving the master database is different from the process
for other databases.

14.2 Moving the master Database


The steps involved in moving the master database are as follows:
1. Open SQL Server Configuration Manager.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

2. In the SQL Server Services node, right-click the instance of SQL Server and choose
Properties and click the Startup Parameters tab.
3. Edit the Startup Parameters values to point to the planned location for the master database
data (-d parameter) and log (-l parameter) files.
4. Stop the instance of SQL Server.
5. Move the master.mdf and mastlog.ldf files to the new location.
6. Restart the instance of SQL Server.

14.3 Moving Other System Database


Other system databases (except the resource database which cannot be moved) are moved
by this process:

1. For each file to be moved, execute ALTER DATABASE ... MODIFY FILE as for
user databases.
2. Stop the instance of SQL Server.
3. Move the files to the new location.
4. Restart the instance of SQL Server.

In the next demonstration, you will see how to move the tempdb database and how to
increase the number of files that it is using.

15. Copying Database


15.1 Key Points
There are several ways to copy databases to other locations, including to other
instances:
• You can detach a database, copy the files to new locations, reattach both the original
and new database files.
• You can backup a database and then restore it using the same database name (on a
different server instance) or using another database name (on the same server instance).
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

• You can use the copy database wizard.

The main disadvantage of detaching and attaching and backup and restore is that only
the database is copied and the DBA needs to take care of all dependent objects like logins, jobs,
user-defined error messages and so on.

Restoring a database on another instance has the advantage that backups should be
created regularly anyway and can therefore be restored easily without affecting the source
system. Performing the restoration is itself a good test of the current backup strategy. Also the
source database stays online during the whole operation. Restoring a database also has the issue
related to recovering dependent objects.

15.2 Copy Database Wizard


The Copy Database Wizard is a good way to work around this restriction. It provides
an easy to use wizard that can move or copy the database with all dependent objects without
the need for additional scripting. In addition it is possible to schedule the copy operation.
The wizard provides two methods for copying or moving the database. It can be
configured to use detach and attach (which is the fastest option) but has the downside that the
source database needs to be offline while the detach/copy/attach is occurring. The second
method uses the SQL Server Management Objects (SMO) programming library methods to
create the objects and transfer the data, which is slower but has the advantage that the source
database can kept online while copying.
Running the Copy Database Wizard requires sysadmin privileges on both instances and
a network connection must be present.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

Demo 6 : Moving and Reconfiguring tempdb

1. If Demo 1 was not performed:

• Open the 10775A_04_PRJ SQL Server script project within SQL Server Management
Studio.
• From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql
script file from within Solution Explorer.

2. Open the 32 – Demonstration 3B.sql script file.


3. Follow the instructions contained within the comments of the script file.

EXERCISE

Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab,
you must complete the following steps:

1. Click Start, click All Programs, click Microsoft SQL Server 2012, and click SQL
Server Management Studio.
2. In the Connect to Server window, type Proseware in the Server name text box.
3. In the Authentication drop-down list box, select Windows Authentication and click
Connect.
4. In the File menu, click Open, and click Project/Solution.
5. In the Open Project window, open the project 10775A_04_PRJ.ssmssln.
6. From the View menu, click Solution Explorer. In Solution Explorer, double-click the
query 00-Setup.sql. When the query window opens, click Execute on the toolbar.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

Lab Scenario

Now that the Proseware instance of SQL Server has been installed and configured on the
server, a number of additional database configurations need to be performed. As the database
administrator, you need to perform these configuration changes.

You need to create a new database on the server, based on requirements from an application
vendor’s specifications. A client has sent you a database that needs to be installed on the
Proseware instance. Instead of sending you a backup, they have sent a detached database and
log file. You need to attach the database to the Proseware instance.

A consultant has also provided recommendations regarding tempdb configuration that you
need to review and implement if appropriate

Tempdb Size Requirement

File Size (MB)


Data 30
Log 10

RateTracking Database Specification

Item Requirement
DatabaseName RateTracking
Primary Data File Logical name = RateTracking_dat
File name = RateTracking.mdf
Folder = D:\MKTG
Initial size = 10MB
Maximum file size = 100MB
Autogrowth amount = 10MB
Filegroup = PRIMARY
Log File Logical name = RateTracking_log
File name = RateTracking.ldf
Folder = L:\MKTG
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

Item Requirement
Initial size = 20MB
Maximum file size = unlimited
Autogrowth amount = 20MB
Filegroup = Not Applicable
Secondary Data File Logical name =
#1 RateTracking_dat_1
File name = RateTracking_1.ndf
Folder = D:\MKTG
Initial size = 20MB
Maximum file size = 100MB
Autogrowth amount = 10MB
Filegroup = USERDATA
Secondary Data File #2 Logical name =
RateTracking_dat_2
File name = RateTracking_2.ndf
Folder = D:\MKTG
Initial size = 20MB
Maximum file size = 100MB
Autogrowth amount = 10MB
Filegroup = USERDATA
Secondary Data File Logical name =
#3 RateTracking_dat_3
File name = RateTracking_3.ndf
Folder = D:\MKTG
Initial size = 200MB
Maximum file size = 500MB
Autogrowth amount = 50MB
Filegroup = ARCHIVE
Secondary Data File #4 Logical name =
RateTracking_dat_4
File name = RateTracking_4.ndf
Folder = D:\MKTG
Initial size = 200MB
Maximum file size = 500MB
Autogrowth amount = 50MB
Filegroup = ARCHIVE
Default Filegroup USERDATA
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

Tempdb Requirements from the Consultant

Filename Requirements
Secondary Data File Logical name = tempdev2
#1 File name =
tempdb_file2.ndf
Folder = D:\MKTG
Initial size = 20MB
Maximum file size =
unlimited
Autogrowth amount =
10MB
Secondary Data File Logical name = tempdev3
#2 File name =
tempdb_file3.ndf
Folder = D:\MKTG
Initial size = 20MB
Maximum file size =
unlimited
Autogrowth amount =
10MB
Secondary Data File Logical name = tempdev4
#3 File name =
tempdb_file4.ndf
Folder = D:\MKTG
Initial size = 20MB
Maximum file size =
unlimited
Autogrowth amount =
10MB
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

Exercise 1: Adjust tempdb Configuration

Scenario

You will adjust the current configuration of the tempdb database. The main tasks for this
exercise are as follows:

1. Adjust the size of tempdb.


2. Check that the tempdb size is still correct after a restart.

• Task 1: Adjust the size of tempdb


• Review the requirement for tempdb size in the Supporting Documentation.
• Adjust the tempdb size based on the requirement in the supporting
documentation.
• Task 2: Check that the tempdb size is still correct after a restart

• Restart the Proseware server using SQL Server Configuration Manager. • Check that
tempdb is still the correct size.

Results: After this exercise, you should have inspected and configured the tempdb
database.

Exercise 2: Create the RateTracking Database

Scenario

You will create a new database named RateTracking as per a supplied set of specifications.
The main tasks for this exercise are as follows:

1. Create the database.


2. Create the required filegroups and files.
3. Change the default filegroup for the database.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

• Task 1: Create the database


o Review the supplied requirements in the supporting documentation for the
exercise.
o Create a new RateTracking database based on the requirements.
• Task 2: Create the required filegroups and files
o Review the supplied requirements in the supporting documentation for the
required files and filegroups.
o Create the required filegroups and files.
• Task 3: Change the default filegroup for the database
o Review the supplied requirements in the supporting documentation for the
default filegroup.
o Modify the default filegroup.

Results: After this exercise, you should have created a new RateTracking database with
multiple filegroups.

Exercise 3: Attach the OldProspects Database

Scenario

A client has sent you a database that needs to be installed on the Proseware instance. Instead
of sending you a backup, they have sent a detached database and log file. You need to attach
the database to the Proseware instance.

The main tasks for this exercise are as follows:

1. Copy the database files.


2. Attach the database to the MKTG instance.
Jurusan Teknologi Informasi Politeknik Negeri Malang
Jobsheet-1: File and Filegoups
Mata Kuliah Sistem Manajemen Basis Data
(International)
Pengampu: Tim Ajar Sistem Manajemen Basis Data
Febuari 2020

• Task 1: Copy the database files


• Copy the files to new folders as per the table below:

Nama file Folder Sumber Folder tujuan


OldProspects.mdf D: \ SQLSERVER_Labs D: \ MKTG
OldProspects.ldf D: \ SQLSERVER_Labs L: \ MKTG

• Task 2: Attach the database to the MKTG instance • Attach the OldProspects
database to the MKTG instance.
Results: After this exercise, you should have attached a database to a client’s server.

You might also like