Module 4 Working with Databases

30
Module 4 Working with Databases

description

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 - PowerPoint PPT Presentation

Transcript of Module 4 Working with Databases

Page 1: Module 4 Working with Databases

Module 4

Working with Databases

Page 2: Module 4 Working with Databases

Module Overview

• Overview of SQL Server Databases

• Working with Files and Filegroups

• Moving Database Files

Page 3: Module 4 Working with Databases

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

Page 4: Module 4 Working with Databases

How Data is Stored in SQL Server

Transaction Log file: .ldfTransaction Log file: .ldf

Extent: 8 contiguous 8KB pagesExtent: 8 contiguous 8KB pages

Page: 8KBPage: 8KB

Primary Data file: .mdf

Secondary data file: .ndf

Primary Data file: .mdf

Secondary data file: .ndf

Table and Index data is stored in Pages that are

grouped in Extents. Special allocation pages keep track

of page usage.

Table and Index data is stored in Pages that are

grouped in Extents. Special allocation pages keep track

of page usage.

Page 5: Module 4 Working with Databases

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

Page 6: Module 4 Working with Databases

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

Page 7: Module 4 Working with Databases

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

tempdbHolds temporary data like temporary tables, table variables, hash tables and the row version store

resourceHidden read-only database that contains system objects that are mapped to the sys schema of databases

Page 8: Module 4 Working with 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

Page 9: Module 4 Working with Databases

Demonstration 1A: Working with tempdb

• In this demonstration, you will see how the size of the tempdb database files is managed.

Page 10: Module 4 Working with Databases

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

Page 11: Module 4 Working with Databases

Creating User Databases

• Can be created in GUI

• Can be created with CREATE DATABASE T-SQL command

• Collation can be specified

CREATE DATABASE BranchON ( 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 );

CREATE DATABASE BranchON ( 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 );

Page 12: Module 4 Working with Databases

Configuring Database Options

Database-level options are unique to each database

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

Important database options:

Page 13: Module 4 Working with Databases

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

Page 14: Module 4 Working with Databases

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.

Page 15: Module 4 Working with Databases

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

Page 16: Module 4 Working with Databases

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

Page 17: Module 4 Working with 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

Page 18: Module 4 Working with Databases

Working with Filegroups

OtherFilegroups

OtherFilegroups

Primary FilegroupPrimary Filegroup

Database

Data File(s)

*.mdf , *.ndf

Log File(s)

*.ldf

Data File(s)

*.ndf

Filegroups are named groups of database files that are used to control placement of database objects. Filegroups can be backed up or restored separately.

Page 19: Module 4 Working with Databases

Demonstration 2C: Filegroups

• In this demonstration you will see: How to create a database with several filegroups

How to create objects in filegroups

Page 20: Module 4 Working with Databases

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

Page 21: Module 4 Working with Databases

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

Page 22: Module 4 Working with Databases

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

Page 23: Module 4 Working with Databases

Demonstration 3A: Detach and Attach

• In this demonstration, you will see how to use detach and attach with database files.

Page 24: Module 4 Working with Databases

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

Page 25: Module 4 Working with Databases

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

Page 26: Module 4 Working with Databases

Demonstration 3B: Moving and Reconfiguring tempdb

• In this demonstration, you will see how to move and reconfigure tempdb to have multiple files.

Page 27: Module 4 Working with Databases

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

Estimated time: 45 minutes

Virtual machine 623XB-MIA-SQL

User name AdventureWorks\Administrator

Password Pa$$w0rd

Page 28: Module 4 Working with Databases

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.

Page 29: Module 4 Working with Databases

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?

Page 30: Module 4 Working with Databases

Module Review and Takeaways

• Review Questions

• Best Practices