sql server 2005 資料庫管理達人一日體驗營
DESCRIPTION
SQL Server 2005 資料庫管理達人一日體驗營. 錢曉明 資策會 資深講師. 議程. SQL Server 2005 新功能槪述 檢視資料庫 Metadata 災難復原 Piecemeal Restore Restore a damaged page online 如何利用資料庫快照還原被刪除的資料表 ? 資料庫監控與效能調教 效能最佳化模型 Default Trace 透過 DMV 及 DMF 檢視效能瓶頸 異地備援 Peer-to-Peer Replication 交易鎖定 Snapshot Isolation Level 高可用度 - PowerPoint PPT PresentationTRANSCRIPT
議程• SQL Server 2005 新功能槪述• 檢視資料庫 Metadata• 災難復原
Piecemeal RestoreRestore a damaged page online如何利用資料庫快照還原被刪除的資料表 ?
• 資料庫監控與效能調教效能最佳化模型Default Trace透過 DMV 及 DMF 檢視效能瓶頸
• 異地備援Peer-to-Peer Replication
• 交易鎖定Snapshot Isolation Level
• 高可用度Database Mirroring Configurations
SQL Server 2005
What Is SQL Server 2005?
Relational Database Engine
.NET CLR
Analysis Services
Native HTTP Support
Service BrokerReplication
Reporting ServicesFull-Text Search
Notification Services
Integration Services
SQL 2005 Top 30 – 管理• Database Mirroring• Online Indexing Operations• New Integrated Toolset• Snapshot Isolation• Data Partitioning• Mirrored Backups• Online Restore• Fast Recovery• Dedicated Administrator Connection• Replication Enhancements
智慧智慧開發開發管理管理
B IB I開發開發管理管理
SQL 2005 Top 30 – 開發• .NET Framework Hosting• XML Technologies• ADO.NET 2.0• Transact-SQL Enhancements• SQL Service Broker• Notification Services• Web Services• XQuery Support• Full-Text Search Enhancements• Security Enhancements
智慧智慧開發開發管理管理
B IB I開發開發管理管理
SQL 2005 Top 30 – 商業智慧• Analysis Services
• SSIS (Data Transformation Services)
• Data Mining
• Reporting Services
• Clustering Support
• Key Performance Indicators
• Scalability and Performance
• One-Click Cube
• Architecture Enhancements
• Integration with the Microsoft Office System
智慧智慧開發開發管理管理
B IB I開發開發管理管理
SQL Server 2000 Silver Bullets
FeatureSQL Server 2000Standard Edition
SQL Server 2000Enterprise Edition
Number of CPUs 4 OS Maximum
Memory 2 GB OS Maximum
Failover Clustering
64-bit Support
Log Shipping
Distributed Partitioned Views
Parallel Operations for SMP Systems
Distributed Partitioned Cubes
SQL Server 2005 Changes
FeatureSQL Server 2000Standard Edition
SQL Server 2005 Standard Edition
Number of CPUs 44+
multi-core technology
Memory 2 GB OS Maximum
Failover Clustering 2-node Only
64-bit Support
Log Shipping Database Mirroring
Distributed Partitioned Views
Parallel Operations for SMP Systems
Some
Distributed Partitioned Cubes
Feature Standard Edition Enterprise Edition
Number of CPUs 4 64
Memory OS Maximum OS Maximum
Multi-Instance Support 16 50
Dynamic AWE Memory Management
Database Snapshot
Scalable Shared Databases
Indexed Views
Parallel Index Operations
Parallel Consistency Checks (DBCC)
Enhanced Read-ahead and Scan (Super Scan)
Up to 4 CPUsUp to 4 CPUs
• Single CPU index operationsSingle CPU index operations• Manual index views usageManual index views usage
No Scale-upNo Scale-up Limited scale outLimited scale out
• Service Broker Service Broker OnlyOnly
• Multiple CPU index Multiple CPU index operationsoperations
• Automatic index view Automatic index view usageusage
Scale out includedScale out included
• Service Broker Service Broker • Peer-to-Peer Peer-to-Peer
replicationreplication
Scale-up includedScale-up included
PartitioningPartitioning
Up to 64 CPUsUp to 64 CPUs
HardwareHardware
DatabaseDatabase ScaleScale
Database Database PerformanPerformancece
Standard Standard EditionEdition
Enterprise Enterprise EditionEdition
Feature Standard EditionEnterprise
Edition
Failover Clustering 2-nodes Only Up to 8 nodes
Database Mirroring Safety FULL Only All Modes
Database Snapshot
Fast Recovery
Online Index Operations
Online Page and File Restore
Parallel Index Operations Updateable Distributed Partitioned
Views
Table and Index Partitioning
• SQL Server 2000: Database is available after Undo completes
• SQL Server 2005 Standard Edition: Database is available when Undo begins
• SQL Server 2005 Enterprise Edition: Database recovery leverages all CPU’s Faster Recovery
UndoRedo
Database is Available!
UndoRedo
Database is Available!
UndoRedo
Database is Available!
Protect from User & Logical Errors
Recover from user & logical errors
Prevent user errors
Decrease Maintenance Downtime
Online Service Pack Install & Upgrades
Online Maintenance Operations
Recovery from Failures
Disaster Recovery
Recovery from HW/SW Failure
Recovery from Database Failure
Database Snapshots
DDL Triggers
DB Mirroring, Clustering & Peer-to-Peer Replication
Online Index Operations
Database Mirroring
Mirroring, Clustering & Replication
Fast Recovery, Online Restore
議程• SQL Server 2005 新功能槪述• 檢視資料庫 Metadata• 災難復原
Piecemeal RestoreRestore a damaged page online如何利用資料庫快照還原被刪除的資料表 ?
• 資料庫監控與效能調教效能最佳化模型Default Trace透過 DMV 及 DMF 檢視效能瓶頸
• 異地備援Peer-to-Peer Replication
• 交易鎖定Snapshot Isolation Level
• 高可用度Database Mirroring Configurations
SQL Server Management Studio Reports
• Server Memory Consumption
• DBCC DropCleanBuffers• Database
Disk Usage• Service Broker
Service Broker Statistics• Logins
Login Statistics• Management
Number of Errors
議程• SQL Server 2005 新功能槪述• 檢視資料庫 Metadata• 災難復原
Piecemeal RestoreRestore a damaged page online如何利用資料庫快照還原被刪除的資料表 ?
• 資料庫監控與效能調教效能最佳化模型Default Trace透過 DMV 及 DMF 檢視效能瓶頸
• 異地備援Peer-to-Peer Replication
• 交易鎖定Snapshot Isolation Level
• 高可用度Database Mirroring Configurations
SQL Server Backup TypesBackup Type Description
Full All data files and part of the transaction log
Transaction log
Any database changes recorded in the log files
Tail-log The active portion of the log
DifferentialThe parts of the database that have changed
since the last full database backup
File / Filegroup
Specified files or filegroups
PartialThe primary filegroup, every read/write
filegroup, and any specified read-only filegroups
Copy-onlyThe database or log (without affecting the
backup sequence)
What Are Recovery Models?
Recovery Model Description
SimpleUses full or differential copies of
the database. Truncates the transaction logs.
FullIncludes both database backups
and transaction log backups.
Bulk-logged
Includes both database and transaction log backups, but uses less log space for some operations.
What Is a File or Filegroup Backup Strategy?
• Use file or filegroups if:
Database is very largeA full backup would take too long
• Back up transaction logs separately• Can be complex to manage
Sunday Monday Tuesday Wednesday
Datafile 1
Data file 2
Data file 3
How to Perform a Piecemeal Restore
• Restore a database in stages based on filegroups
Start with primary filegroupContinue with secondary filegroups
• Each filegroup comes online as it is restored
• Transactions involving filegroups not yet restored will hold locks until rollback can complete
Overview of Online Restore Operations
DatabaseDatabase
PrimaryPrimaryFilegroupFilegroup
Filegroup AFilegroup A
Filegroup BFilegroup B
File unavailable, filegroup offline
All files available, filegroup online
Primary filegroup online, database available
Restoringfile
議程• SQL Server 2005 新功能槪述• 檢視資料庫 Metadata• 災難復原
Piecemeal RestoreRestore a damaged page online如何利用資料庫快照還原被刪除的資料表 ?
• 資料庫監控與效能調教效能最佳化模型Default Trace透過 DMV 及 DMF 檢視效能瓶頸
• 異地備援Peer-to-Peer Replication
• 交易鎖定Snapshot Isolation Level
• 高可用度Database Mirroring Configurations
How to Restore a Damaged Page
Identify ID of damaged page
Restore page from a full or differential backup
Use PAGE clause to specify pages to restore
Specify NORECOVERY
Restore transaction logs
Backup transaction log tail
Restore transaction log tail
Identify ID of damaged page
Restore page from a full or differential backup
Use PAGE clause to specify pages to restore
Specify NORECOVERY
Restore transaction logs
Backup transaction log tail
Restore transaction log tail
11
33
44
55
22
RESTORE DATABASE AdventureWorksPAGE = '1:610'FROM AWBackWITH NORECOVERY
RESTORE DATABASE AdventureWorksPAGE = '1:610'FROM AWBackWITH NORECOVERY
議程• SQL Server 2005 新功能槪述• 檢視資料庫 Metadata• 災難復原
Piecemeal RestoreRestore a damaged page online如何利用資料庫快照還原被刪除的資料表 ?
• 資料庫監控與效能調教效能最佳化模型Default Trace透過 DMV 及 DMF 檢視效能瓶頸
• 異地備援Peer-to-Peer Replication
• 交易鎖定Snapshot Isolation Level
• 高可用度Database Mirroring Configurations
What Is a Database Snapshot?
12:00 Snapshot
• Read-only, consistent view of a database at a specified point in time
• Useful as test or development database, or for reporting
• Must exist on same server as source database
How Database Snapshots Work
Source DB Snapshot DB
Copy-On-Write (first time only)
SELECT …SELECT …
SELECT …SELECT …UPDATE …UPDATE …
How to Use a Database Snapshot to Recover Data
Scenario Example Code / Steps
Undeleterows
Undoingan update
Recoveringa droppedobject
11 Script the object in the database snapshot
INSERT INTO Production.WorkOrderRoutingSELECT * FROM AdventureWorks_dbsnapshot_1800.Prod.WorkOrderRouting
INSERT INTO Production.WorkOrderRoutingSELECT * FROM AdventureWorks_dbsnapshot_1800.Prod.WorkOrderRouting
UPDATE HR.DepartmentSET Name = ( SELECT Name FROM AdventureWorks_dbsnapshot_1800.HR.Department WHERE DepartmentID = 1)WHERE DepartmentID = 1
UPDATE HR.DepartmentSET Name = ( SELECT Name FROM AdventureWorks_dbsnapshot_1800.HR.Department WHERE DepartmentID = 1)WHERE DepartmentID = 1
Caution: Not a substitute for a comprehensive backup and restore strategy
Execute the script in the source database22
Repopulate the object (if appropriate)33
• Read-only, consistent copy of database• Very fast to create• Can be created for any database
Production databases Instant standby databases with
Database Mirroring• Supports scale-out and high availability
scenarios Great solution for reporting/read-only
applications Safeguarding data against application
and human error• Rollback to a snapshot to correct the error
議程• SQL Server 2005 新功能槪述• 檢視資料庫 Metadata• 災難復原
Piecemeal RestoreRestore a damaged page online如何利用資料庫快照還原被刪除的資料表 ?
• 資料庫監控與效能調教效能最佳化模型Default Trace透過 DMV 及 DMF 檢視效能瓶頸
• 異地備援Peer-to-Peer Replication
• 交易鎖定Snapshot Isolation Level
• 高可用度Database Mirroring Configurations
正規化( Normalization )
• 正規化( Normalization )的目的是減少「重複累贅的資料」。
• 一般只會實施到第三正規化。• 優點 -- 方便維護及修改。• 缺點 -- 查詢資料時需要 Join 較多的資料表才能得到
所需資料,因此回應時間加長。• 檢視標準 -- 每次存取資料時均要 Join 六、七個資料
表,回應時間加長,既表示過渡正規化。
應用程式特性(一)• 線上交易( OLTP )應用程式
「多人同時上線」 (Concurrency)「頻繁」的從事「小量交易」使用指令 Select/Insert/Update/ Delete 主要 workload
• Reads, writes可能問題
• Locking , Blocking , Disk I/O , CPU , Memory
可能的解決方案• 可以做正規化• 刪除不必要的索引• 使用 Transaction Isolation Level Snapshot• 非同步處理( Asynchronous data access)
應用程式特性(二)• Decision Support System ( DSS )應用程式
OLAPData Mining報表製作( Reporting )
• 特性讀取大量資料,然後作彙總計算 使用 Select主要 workload
• Reads可能問題
• 大量硬碟 I/O • 資料碎裂
• 可能的解決方案可以做非正規化建立索引索引重整( Index Rebuild )
索引最佳化• 建在單一欄位上的非叢集式索引,百分之九十的情況
下, Query Analyzer 會選擇使用 Table Scan ,而不會使用該索引。
• 資料經過一段時間,新增 / 修改 / 刪除之後,會產生碎裂,應執行索引重整 (類似磁碟機重整)。
• 作索引重整( Index Rebuild )對提升報表製作( Reporting )應用程式效能有幫助。
議程• SQL Server 2005 新功能槪述• 檢視資料庫 Metadata• 災難復原
Piecemeal RestoreRestore a damaged page online如何利用資料庫快照還原被刪除的資料表 ?
• 資料庫監控與效能調教效能最佳化模型Default Trace透過 DMV 及 DMF 檢視效能瓶頸
• 異地備援Peer-to-Peer Replication
• 交易鎖定Snapshot Isolation Level
• 高可用度Database Mirroring Configurations
Default Trace• 佔資源少• 預設路徑 C:\Program Files\Microsoft SQL Server\ MSSQL.1\
MSSQL\LOG• 預設追蹤事件約三十多項,如:
Audit Addlogin EventAudit Login FailedAudit Backup/Restore EventData File Auto GrowDatabase Mirroring State ChangeObject: CreatedObject: AlteredObject: DeletedServer Memory Change
Disable Default TraceSP_CONFIGURE 'show advanced options',1
go
RECONFIGURE
GO
SP_CONFIGURE 'DEFAULT TRACE ENABLED',1
GO
RECONFIGURE
GO
議程• SQL Server 2005 新功能槪述• 檢視資料庫 Metadata• 災難復原
Piecemeal RestoreRestore a damaged page online如何利用資料庫快照還原被刪除的資料表 ?
• 資料庫監控與效能調教效能最佳化模型Default Trace透過 DMV 及 DMF 檢視效能瓶頸
• 異地備援Peer-to-Peer Replication
• 交易鎖定Snapshot Isolation Level
• 高可用度Database Mirroring Configurations
透過 DMV 及 DMF 檢視效能瓶頸
• SQL Server 2000 ,必需得靠 Profiler 記錄的 trace file (追蹤記錄檔)。
• SQL Server 2005 中新增許多系統檢視及系統函數Dynamic Management View ( DMV )Dynamic Management Function ( DMF )。
Dynamic Management View ( DMV ) Dynamic
Management Function ( DMF )
• 與系統資料表不同, DMV 不會存檔。 • DMV 中的紀錄會隨著資料庫的使用狀態而改變• DMV/DMF 的命名規則
命名一定是 dm_* DMV 及 DMF 均屬於 sys 這個 schema 用時一定要寫 two-part name 或 three-part name ,如 sys.dm_*
Dynamic Management View ( DMV ) Dynamic Management Function ( DMF )• DMV/DMF 可大分為兩類,伺服器層級及元件層級 • 伺服器層級( Server Level )
sys.dm_exec_*• 記錄使用者程式碼執行狀態。
sys.dm_os_*• 記錄伺服器層級系統相關資訊,例如記憶體, locking ,及排程
等。sys.dm_tran_*
• 記錄 Transactions 及 isolation 相關資訊。sys.dm_io_*
• 記錄網路及硬碟上的 I/O 。sys.dm_db_*
• 記錄資料庫及其相關物件。
Dynamic Management View ( DMV ) Dynamic Management Function ( DMF )• 元件層級( Component Level)
sys.dm_repl_*• 記錄資料庫複寫。
sys.dm_broker_*• 記錄 Service Broker 相關資訊。
sys.dm_fts_*• 記錄全文檢索相關資訊。
sys.dm_qn_*• 記錄 Query Notifications 相關資訊。
sys.dm_clr_*• 記錄 CLR 程式碼執行相關資訊。
sys.dm_exec_requests
• 可檢查伺服器上各 Session 執行狀態
Select scheduler_id, session_id, status, command, wait_type, wait_resource, percent_complete, cpu_time
from sys.dm_exec_requests where session_id > 50
CPU Pressure (處理器瓶頸)
• Runnable Queue -- Status 欄位顯示為 runnable ,表示這些 session 正在等待執行
• 這一類的等待表示一切資源均已完備,只等待 CPU 處理,稱之為 Signal Waits 。
• 計算 Signal Waits 的時間,即可知道處理器是否產生瓶頸,此一項評量標準稱為 CPU Pressure 。
Resources Waits
• Waiter List (等待清單) -- Status 欄位顯示為suspended
• 這一類的等待稱為 Resources Waits ,表示在等待資源( Resources ),至於是哪種資源,可能是記憶體,或是資料被 Block 住,或是 Disk I/O ,…等原因,可參考wait_type (等待種類)欄位
• wait_type -- 在 SQL Server 2000 約有 76 種, SQL Server 2005 約有 192 種
sys.dm_os_wait_stats
• 計算出 signal_wait_time_ms 和 resource_wait_time_ms ,並將之轉換成百分比
Select signal_wait_time_ms=sum(signal_wait_time_ms),'%signal waits' = cast(100.0 * sum(signal_wait_time_ms) / sum (wait_time_ms) as numeric(20,2)),resource_wait_time_ms=sum(wait_time_ms - signal_wait_time_ms),'%resource waits'= cast(100.0 * sum(wait_time_ms - signal_wait_time_ms) / sum (wait_time_ms) as numeric(20,2))
From sys.dm_os_wait_stats
sys.dm_exec_sql_text(sql_handle)
• 此 DMF 傳回目前伺服器上正在執行的指令 select r.session_id, status ,substring(qt.text,r.statement_start_offset/2,
(case when r.statement_end_offset = -1 then len(convert(nvarchar(max), qt.text)) * 2 else r.statement_end_offset end -
r.statement_start_offset)/2) as query_text --- 傳回目前正在執行指令,qt.dbid,qt.objectid,r.cpu_time,r.total_elapsed_time,r.reads,r.writes,r.logical_reads,r.scheduler_id
from sys.dm_exec_requests rcross apply sys.dm_exec_sql_text(sql_handle) as qtwhere r.session_id > 50order by r.scheduler_id, r.status, r.session_id
sys.dm_exec_query_stats
• Top 50 statements by Avg CPU TimeSELECT TOP 50 qs.total_worker_time/qs.execution_count as [Avg CPU Time], SUBSTRING(qt.text,qs.statement_start_offset/2,
(case when qs.statement_end_offset = -1 then len(convert(nvarchar(max), qt.text)) * 2 else qs.statement_end_offset end -
qs.statement_start_offset)/2) as query_text,qt.dbid, dbname=db_name(qt.dbid),qt.objectid
FROM sys.dm_exec_query_stats qscross apply sys.dm_exec_sql_text(qs.sql_handle) as qtORDER BY [Avg CPU Time] DESC
議程• SQL Server 2005 新功能槪述• 檢視資料庫 Metadata• 災難復原
Piecemeal RestoreRestore a damaged page online如何利用資料庫快照還原被刪除的資料表 ?
• 資料庫監控與效能調教效能最佳化模型Default Trace透過 DMV 及 DMF 檢視效能瓶頸
• 異地備援Peer-to-Peer Replication
• 交易鎖定Snapshot Isolation Level
• 高可用度Database Mirroring Configurations
Load balancing
Applicationserver
Update and read load balancingUpdate and read load balancing
Read load balancingRead load balancing
Load balancing
Applicationserver
Implementing Distributed Load Balancing
Read request
Write request
User traffic
Replicated data
Implementing Distributed High Availability
• Synchronizing nodes
• Peer-to-peer replication topologies
• Implementing a peer-to-peer replication topology
Log Reader Agent
DistributionAgent
SydneySydney
Log Reader Agent
DistributionAgent
LondonLondon
Log Reader Agent
DistributionAgent
New YorkNew York
How to configure Peer-to-Peer Replication?
• 在三台 SQL Instance 上各建一個 Database ,均包含相同的 Table
• 在其中一台新增一個 Publication (注意不要勾選 Create a snapshot immediately and keep the snapshot available to initialize subscriptions
• 在 Publication 上按右鍵 propertysubscription options 將 Allow peer-to-peer subscriptions 改為True
• 在 Publication 上按右鍵選擇 Configure Peer-to-peer Topology 選擇要加入 Peer 的 Server 及 Database
議程• SQL Server 2005 新功能槪述• 檢視資料庫 Metadata• 災難復原
Piecemeal RestoreRestore a damaged page online如何利用資料庫快照還原被刪除的資料表 ?
• 資料庫監控與效能調教效能最佳化模型Default Trace透過 DMV 及 DMF 檢視效能瓶頸
• 異地備援Peer-to-Peer Replication
• 交易鎖定Snapshot Isolation Level
• 高可用度Database Mirroring Configurations
Transaction Isolation Levels
• Database Engine isolation Levels:
• Read Uncommitted (Dirty Read)
• Read Committed
• Repeatable Read
• Snapshot
• Serializable
Guidelines to reduce Locking and Blocking
• Keep logical transactions short
• Avoid cursors
• Use efficient and well-indexed queries
• Use the minimum transaction isolation level required
• Keep triggers to a minimum
議程• SQL Server 2005 新功能槪述• 檢視資料庫 Metadata• 災難復原
Piecemeal RestoreRestore a damaged page online如何利用資料庫快照還原被刪除的資料表 ?
• 資料庫監控與效能調教效能最佳化模型Default Trace透過 DMV 及 DMF 檢視效能瓶頸
• 異地備援Peer-to-Peer Replication
• 交易鎖定Snapshot Isolation Level
• 高可用度Database Mirroring Configurations
Database Roles and Topology for Database Mirroring
Witness Server (Optional)
Principal Server (Online database)
Mirror Server (Standby database)
Client Session
Database Mirroring Session
What Is Database Mirroring?
• Alternative to failover clustering
• Mirror ServerProvides a hot standby
• Witness ServerMonitors the primary and mirror database servers
• Client redirectionAutomatic client redirection
Options For Database Mirroring Configuration
ModeAutomatic Failover
Full Protection from Data Loss
High Availability
同步有見證伺服器High Protection同步無見證伺服器
High Performance
非同步有見證伺服器
How to Prepare Servers for Database Mirroring
Create mirroring endpoints and loginsCreate mirroring endpoints and logins11
Back up database and restore on mirror serverBack up database and restore on mirror server33
Copy server-level resourcesCopy server-level resources44
Set recovery model to FULLSet recovery model to FULL22