danny tambs – senior consultant microsoft
DESCRIPTION
Session Objective(s): Overview of Partial Database Availability Demonstrate Recovery Methods Key Takeaways Partial database availability can shorten downtime when a failure occurs. Partial DB should be designed in from project start. Demos Recovery of a R/O Filegroup. Recovery of a R/W Filegroup.TRANSCRIPT
DAT410 – SQL Always OnPartial Database AvailabilityDanny Tambs – [email protected] ConsultantMicrosoft
Session Objectives And TakeawaysSession Objective(s):
Overview of Partial Database AvailabilityDemonstrate Recovery Methods
Key TakeawaysPartial database availability can shorten downtime when a failure occurs.Partial DB should be designed in from project start.
DemosRecovery of a R/O Filegroup.Recovery of a R/W Filegroup.
What is it?Enables a database to be brought online fast after a disaster when one or more Filegroups are lost.
Enables a given database to be quickly restored and brought online while other missing data can be restored later.Eg:
Large OLTP database with small amount of transaction supporting data but huge volumes of history data.
Typical UsesVLDB systemsTypically over 1 TB since the time to restore the whole database means significant downtime.Solutions using multiple filegroups where the main application functionality resides in small No.# of filegroups.
Eg: An OLTP database with a relatively small amount of working data but a proportionaly huge amount of historical data.Not meant as a substitute for a sound backup strategy.
Steps in Recovery of a Database1. After the database fails examine error log to
determine the physical filename that failed to load.Error : 17207, Unable to open the physical file "C:\Data\MyFG_Database3_1_sec.ndf". Operating system error 2: "2(error not found)".
2. Determine the logical filename that the physical filename maps to by querying the system catalog.
3. Alter Database - set the logical filename to offline4. Alter Database - set Database to online5. DATABASE NOW ONLINE !
Continue operating with Partial Availability.6. Restore Missing filegroup(s) as time permits. 7. Restore Tran Log & recover DB if read/write
filegroup.8. Database now fully Online.
ALTER DATABASE MYDB SET ONLINEGO
Primary FG1 FG2
A Timeline
File1mdf
File2ndf
File3ndf
File4ndf File5
ldf
Arrays1 2 3
Database = ONLINE
X
Database = OFFLINE
ALTER DATABASE MYDB MODIFY FILE(NAME=‘LOGICAL FILE4’, OFFLINE)GO
BACKUP LOG MYDB TO DISK=‘…FILE’GORESTORE DATABASE MYDB FILE=‘LOGICAL FILE4’ FROM DISK WITH FILE=1, NORECOVERYGO
RESTORE LOG MYDB FROM DISK =‘…FILE’ WITH RECOVERYGO
FilegroupOFFLINE
Fix HW Issue
Database = ONLINE - PARTIAL
Database cannot be opened
Command:
Status:
SQL 2000 - ReviewPartial command under SQL2000 was less flexible. Nothing to do with setting a file offline and bringing the DB Online.
Used to restore a file group to a different location to recover lost data.Needed to restore the mdf file as well as required file group.Once the database was brought online any unrestored filegroups could not be restored.Could not really be used to bring a database online quickly in order to reduce downtime.
SQL 2005 ImprovementsMany Enhancements.
SQL 2005 – we can now set a file to OFFLINE and bring database ONLINE.Unaffected file groups are still available for read/write operations.Commands against online file groups will execute.Only Commands against offline file groups will fail. Typical Errors:
945, 8653, 17207, 5120, 17204Piecemeal restore - Unrestored filegroups can now be restored at a later time.
Syntax: SET FILE OFFLINEALTER DATABASE database_name MODIFY FILE ( NAME = logical_file_name [ , OFFLINE ])
EG:
ALTER DATABASE MyFG_Database MODIFY FILE
(NAME = 'MyFG_Database3_sec' , OFFLINE)
*Set the unavailable file offline to be able to set the database online.
Syntax: SET DB ONLINE
ALTER DATABASE database_name SET ONLINE
EG:
ALTER DATABASE MyFG_Database SET ONLINE
*Once unavailable files are to OFFLINE use this command to set the database to online state.
Recovery of a Read Only Filegroup
demo
Piecemeal RestoreMust specify PARTIAL in the 1st restore of the sequence.
RESTORE DATABASE DB_name FROM DISK = ... WITH PARTIAL
Involves a series of restore sequences, starting with the primary filegroup.Can also restore one or more secondary filegroups when restoring the primary.After the restore sequence is finished recovered files can be brought online directly.
Other Files can be restored while the database is online.SQL Server 2005 Enterprise Edition only.
Syntax: PARTIAL RESTORERESTORE DATABASE database_name FROM DISK = physical_filename[WITH , PARTIAL]
EG:
RESTORE DATABASE [MyFG_Database] FILEGROUP='Primary'
FROM DISK = N'C:\...\F1_backup.bak' WITH PARTIAL
Used for piecemeal restore of MDF and NDF files. When Completed, DB is online.
Piecemeal Restore 2Can be ONLINE or OFFLINE
Offline – All EditionsOnline – SQL Enterprise Edition Only
Offline piecemeal restoreThe database is online after the partial-restore Filegroups that have not yet been restored remain offline, but can be restored as needed after taking the database offline.
Online piecemeal restoreAfter the partial-restore sequence, the database is online, and the primary filegroup and any recovered secondary filegroups are available (ONLINE). Other Filegroups to be restored are offline, but can be restored as needed while the database remains online.
Piecemeal Restore 3Works with all recovery models.Recovery Model NotesSimple Recovery -Any as yet unrestored read/write
filegroups cannot be restored. (No Tran Log)
- Only Read only Filegroups can be restored
Full and Bulk Logged
-Any as yet unrestored read/write filegroups can be restored.
-Transaction Log must be restored to make the DB consistent.
Mapping Logical to Physical filesUse sys.master_files system catalogue
view.Uses data in Master DB.
EG.SELECT name FROM sys.master_files WHERE physical_name = ‘Physical Name‘
Logical files – known within SQLPhysical names – The file on the disk
Get From Error log
Get the File Info per DBGet information about the DB files, sizes and if
Read only or / Read Write. Use to easily find file to set offline.
SELECT s.data_space_id as Id, g.name as Filegroup_name, s.name as Logical_name, physical_name, g.is_default AS [IsDefault], g.is_read_only AS [ReadOnly],
CAST(ISNULL((select sum(gs.size)*convert(float,8)
FROM sys.database_files gs WHERE gs.data_space_id = g.data_space_id), 0) AS
float) AS [Size] FROM sys.filegroups AS g INNER JOIN sys.master_files AS s ON (s.data_space_id = g.data_space_id) WHERE s.type = 0 AND s.database_id = db_id() AND (s.drop_lsn IS NULL)
Get Logical File Information Use sys.database_files Catalog View.Determine if a file is online
EG:
SELECT File_id, is_read_only, -- Is this Readonly?state_desc , -- Either Online or Offline.name -- Logical Filename
FROM sys.database_files
Recovery of a Read / Write Filegroup
demo
Designing for Partial AvailabilityQueries against an offline filegroup will fail.
REASONSJoining to tables are that not available.Tables with DRI against tables on offline filegroups.Triggers referring to unavailable tables.Partitioned Tables - where a required partition is offline.
- Group related schema Functionality in related filegroups.Note :Functions and procedures are still in PRIMARY
filegroup
Design for Partial Availability ..Detect if a table is unavailable
We know the table we want but what if the file is unavailable?Only use in filegroup to filegroup query cases. Ie boundary conditions.
Use function. IsAvailable(@Schema, @Objname)
Specify schema name, object_name to determine if the table is available.This is sample code and does not ship with SQL server 2005
Get the code from the white paperhttp://www.microsoft.com/technet/prodtechnol/sql/bestpractice/pdbavail.mspx
Tables to Filegroups mapping
SELECT sysob.object_id AS [ObjectId] ,schm.[name] AS SchemaName,sysob.[name] AS ObjectName,sysdbf.name AS [LogicalName] -- = LOGICAL FILE NAME,sysdbf.physical_name AS [PhysicalName] -- = PHYSICAL FILE NAME,sysdbf.state_desc AS [FileGroupState] -- = FILE GROUP ONLINE/OFFLINE ETC.,sysdbf.STATE
FROM sys.indexes AS iINNER JOIN sys.data_spaces AS ds ON i.data_space_id = ds.data_space_idINNER JOIN sys.objects AS sysob ON i.object_id = sysob.object_idINNER JOIN sys.database_files AS sysdbf ON (ds.data_space_id = sysdbf.data_space_id) INNER JOIN sys.schemas AS schm ON sysob.schema_id = schm.schema_id
WHERE sysob.type = 'U'AND is_ms_shipped = 0 AND sysdbf.type = 0 AND sysdbf.drop_lsn IS NULL
Understand what tables are affected if a file is offline.
Considerations for UseSetting a file to OFFLINE is a one way operation. Must restore to make online.When restoring a missing filegroup by using offline restore the database is unavailable for user interaction.Only Secondary files (*.ndf) can be set to OFFLINE state.Queries against OFFLINE filegroups will fail.Setting a file in a filegroup to OFFLINE affects the entire filegroup.
Best PracticesKeep your *.mdf files as small as possible.
Makes the first mdf restore faster.Keep your minimal functionality to be online in the MDF for fast initial restore. (critical tables, procs, Functions, reference tables.) Restore other FG later.Backup the DB transaction logs often.Record the mapping of logical to physical file names.Have a clear and well rehearsed partial DB recovery plan.
Related ContentWhitepaper – Partial Database Availability
http://www.microsoft.com/technet/prodtechnol/sql/bestpractice/pdbavail.mspx
Customer Advisory Team Blog Sitehttp://blogs.msdn.com/sqlcat/archive/2005/10/27/how-can-sql-server-2005-online-piecemeal-restore-improve-availability.aspx
MSDNhttp://msdn2.microsoft.com/en-us/library/ms177425.aspx
Questions?
© 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after
the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.