6231B_04

advertisement
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
Log file: .ldf
Secondary data file: .ndf
Extent: 8 contiguous 8KB pages
Page: 8KB
Table and Index data is
stored in Pages that are
grouped in Extents. Special
allocation pages keep track
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 data over more spindles 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 database vendor
• Set the size to a reasonable size:

Leave enough place 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
msdb
Holds SQL Server Agent configuration
including job, backup and restore history
model
Is the template for new databases
tempdb
Holds temporary data like temporary tables,
table variables, hash tables and the row
version store
resource
Hidden read-only database that contains
system objects that are mapped to the sys
schema of databases
Overview of tempdb
• tempdb contains temporary data for internal objects, row
versioning, and user objects

Is truncated or rebuilt with every restart of the instance

Occupies space depending upon workload and usage patterns
on the SQL Server instance

Should be tested with 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-SQL command
• Collation can be specified
CREATE DATABASE Branch
ON
( NAME = Branch_dat,
FILENAME = 'D:\Data\Branch.mdf', SIZE = 100MB,
MAXSIZE = 500MB, FILEGROWTH = 20% )
LOG ON
( NAME = Branch_log,
FILENAME = 'L:\Logs\Branch.ldf', SIZE = 20MB,
MAXSIZE = UNLIMITED, FILEGROWTH = 10MB );
Configuring Database Options
Database-level options are unique to each database
Important database options:
Option
Description
Auto Options
Define whether some operations should occur
automatically within the database
Page Verify
Define how the page should be verified when
read from disk. Should be set to CHECKSUM
Recovery Model
Defines the recovery model of the database
State options
Sets the state of the database, such as
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 permission
• Applies only to data files

Does not apply to log 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 place 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, 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

How to expand a database file

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.
Primary
Filegroup
Data File(s)
Other
Filegroups
Data File(s)
*.mdf , *.ndf
Database
*.ndf
Log File(s)
*.ldf
Demonstration 2C: Filegroups
• In this demonstration you will see:

How to create a database with several filegroups

How to create objects in filegroups
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 drops 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

Preference is to use the ALTER DATABASE statement

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 must be moved manually while the instance is stopped
• 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
and shared objects from the master database

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
623XB-MIA-SQL
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, 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.
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
Download