Sie sind auf Seite 1von 30

Module 4

Working with Databases


Module Overview
• Overview of SQL Server Databases

• Working with Files and Filegroups

• Moving Database Files


Lesson 1: Overview of SQL Server Databases
• How Data Is Stored in SQL Server

• Determining File Placement and Number of Files

• Ensuring Sufficient File Capacity

• System Databases Supplied with SQL Server

• Overview of tempdb

• Demonstration 1A: Working with tempdb


How Data Is Stored in SQL Server

Primary Data file: .mdf Transaction


Secondary data file: .ndf Log file: .ldf

Extent: 8 contiguous 8KB pages

Table and Index data is


stored in Pages that are
grouped in Extents. Special
allocation p
pages
g keepp track
Page: 8KB of page usage.
Determining File Placement and Number of Files
• Isolate log and data files at the physical disk level

• Use appropriate RAID levels

• Determine the number and location of data files based on


performance and maintenance considerations
• Use additional files to spread
p data over more spindles
p or
storage locations
• Use smaller data files when easier maintenance is needed
• Use data files as units of backup and restore

• Determine log file requirements


• Use a single log file in most situations as log files are written
sequentially
Ensuring Sufficient File Capacity

• Estimate the size of data, log files and tempdb


• Perform load testing with the actual application
• Check with the application vendor

• Set the size to a reasonable size:


• Leave enough splace for new data, without
the need to expand often
• Monitor data and log file usage
• Plan for manual expansion
• Keep autogrowth enabled to allow for unexpected
growth
System Databases Supplied with SQL Server

System Database Description

master Stores all system-level configuration

Holds SQL Server


Se e Agent configuration
config ation
msdb
including job, backup and restore history

model Is the template for new databases

Holds temporary data like temporary tables,


tempdb
p table variables,, hash tables and the row
version store
Hidden read-only database that contains
resource system objects that are mapped to the sys
schema
h off databases
d b
Overview of tempdb
• tempdb contains temporary data for internal objects, row
versioning, and user objects
• Is truncated or rebuilt with every
y restart of the instance
• Occupies space depending upon workload and usage patterns
on the SQL Server instance
• Should be tested with real-life
real life workloads

• Place tempdb on a fast and separate I/O subsystem to


ensure good performance
• Split tempdb into data files of equal size per core (up to a
maximum of about 8 data files)
• Use testing to confirm the best value
Demonstration 1A: Working with tempdb
In this demonstration, you will see how the size of the
tempdb database files is managed
Lesson 2: Working with Files and Filegroups
• Creating User Databases

• Configuring Database Options

• Instant File Initialization

• Demonstration 2A: Creating Databases

• Altering Databases

• Expanding and Shrinking Database Files

• Demonstration 2B: Altering Databases

• Working with Filegroups

• Demonstration 2C: Filegroups


Creating User Databases
• Can be created in GUI

• Can be created with CREATE


DATABASE T
T-SQL
SQL command
• Collation can be specified

CREATE DATABASE Branch


ON
( NAME = Branch_dat,
FILENAME = 'D:\Data\Branch.mdf', SIZE = 100MB,
MAXSIZE
S = 500
500MB,
, FILEGROWTH
G O = 20%
0% )
LOG ON
( NAME = Branch_log,
FILENAME = 'L:\Logs\Branch.ldf', SIZE = 20MB,
MAXSIZE = UNLIMITED,
UNLIMITED FILEGROWTH = 10MB );
Configuring Database Options
D t b
Database-level
l l options
ti are unique
i to
t each
h database
d t b

Important
p database options:
p

Option Description
Define whether some operations
p should occur
Auto Options
automatically within the database

Define how the page should be verified when


Page Verify
read from disk. Should be set to CHECKSUM

Recovery Model Defines the recovery model of the database

Sets the state of the database


database, such as
State options
Online/Offline, Restricted Access or Read Only
Instant File Initialization
• Allows reclaiming used disk space quickly

• Does not require first filling that space with zeros

• Is a minor security concern

• Is enabled by assigning the SQL Server service account


the Perform Volume Maintenance Tasks right
• Applies only to data files
• Does not apply
pp y to log
g files
Demonstration 2A: Creating Databases
In this demonstration you will see how to create a database
using the GUI in SSMS and using the CREATE DATABASE
statement in T-SQL
Altering Databases
• Database files can be added, dropped, expanded, and
shrunk
• All file operations are online operations using
• SSMS
• ALTER DATABASE
• DBCC (options such as CHECKDB and SHRINKFILE)

• Dropping a database file requires it to be emptied first


• Need to migrate data to other files
Expanding and Shrinking Database Files
• Files can be expanded manually

• Files can grow automatically using the auto-growth options


• Performed only when new space is needed
• Can lead to blocking and time-out problems
• Should be avoided: Plan for manual file expansion

• Shrinking files is possible


• Uses DBCC SHRINKDATABASE
SHRINKDATABASE, DBCC SHRINKFILE
• Should be performed only when absolutely needed
• Is a very intrusive operation
• Can lead to heavily fragmented databases
Demonstration 2B: Altering Databases
In this demonstration you will see:
• How to add a file to a database
• H
How to
t expand
dad
database
t b file
fil
• How to shrink a database file
• How to drop a database file
Working with Filegroups

Filegroups are named groups of database files that are used


to control placement of database objects. Filegroups can be
backed up or restored separately

Primaryy Data File(s)


Filegroup
*.mdf , *.ndf

Database
Other
Filegroups Data File(s)
*.ndf

Log
g File(s)
( )
*.ldf
Demonstration 2C: Filegroups
• In this demonstration you will see:
• How to create a database with several filegroups
• H
How tto create
t objects
bj t ini filegroups
fil
Lesson 3: Moving Database Files
• Overview of Detach and Attach

• Moving User Database Files

• Demonstration 3A: Detach and Attach

• Moving System Database Files

• Copying Databases

• Demonstration 3B: Moving and Reconfiguring tempdb


Overview of Detach and Attach

• Detaching a database unhooks the database from the instance


• Data and log files are kept intact
• Detached files can be attached again on the same or a different
instance

• Use detach/attach to move databases to other instances


• Detach/attach is also useful in disaster recovery situations
Moving User Database Files
• Data and log files can be moved within the instance
• Can be performed using Detach/Attach
• P f
Preference is
i to
t use th
the ALTER DATABASE statement
t t t
• Use Detach/Attach when moving between instances

• Logical name of the database files are needed


• Used to identify the files to be moved

• Database needs to be set offline

• Files need to be moved manually within the filesystem


Demonstration 3A: Detach and Attach
In this demonstration, you will see how to use detach and
attach with database files
Moving System Database Files
• All system databases except the resource database can
be moved to new locations
• Moving the master database:
• Startup parameters for the SQL Server services must be
changed
• Files
l must b
be moved
d manually
ll while
h l the
h instance is stopped
d

• Moving other system databases:


• Use ALTER DATABASE as with user databases

• Be cautious performing these operations


• Misconfiguration can prevent SQL Server from starting
Copying Databases

• There are different ways


to copy databases:
• Backup and Restore
• Detach and Attach
• Copy Database Wizard

• Copy Database Wizard


• Can be used to move or copy databases between instances
• Can copy additional objects the database depends on such as
logins, jobs, maintenance plans, user-defined error messages
andd shared
h d objects
b from
f the
h master database
d b
• Can be scheduled through a SQL Agent Job
Demonstration 3B: Moving and Reconfiguring tempdb
In this demonstration, you will see how to move and
reconfigure tempdb to have multiple files
Lab 4: Working with Databases
• Exercise 1: Adjust tempdb Configuration

• Exercise 2: Create the RateTracking Database

• Exercise 3: Attach the OldProspects Database

• Challenge Exercise 4: Add Multiple Files to tempdb (Only if


time permits)

Logon information
Virtual machine 10775A-MIA-SQL1

User name AdventureWorks\Administrator


Password Pa$$w0rd

Estimated time: 45 minutes


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,
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.
specifications A
client has sent you a database that needs to be installed on
the Proseware instance. Instead of sending you a backup,
theyy have sent a detached database and logg 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.
Lab Review
• What is the biggest challenge where databases on a single
server use different collations?
• Why should tempdb have multiple files on most systems?
Module Review and Takeaways
• Review Questions

• Best Practices

Das könnte Ihnen auch gefallen