Author: Sean Chang
最近幾年雲端運算及Big Data漸漸地從專有名詞轉變為管理者必須面對的問題了。越來越多的資訊被放到企業網路中,網路、資料庫的效能管理日趨重要。最近我們常被問到要怎麼做好資料庫的日常管理,以一般學校及中小企業常用的MS-SQL Server來說,資料庫的管理主要分為幾個部份:
- 資料庫運行的作業系統環境
- 資料庫運行效能
- 資料庫維護
以下我們就針對各部份須注意的事項做一個簡單分享。
作業系統環境
作業系統作為資料庫運行的平台,本身的效能也會對資料庫造成影響。其中最重要的影響因素莫過於磁碟空間不足了,因此針對作業系統我們有下列幾項基本檢查:
- Windows「事件檢視器」中是否有MS-SQL Server相關錯誤訊息
- Data file所在的磁碟機已用及可用空間
- Log file所在的磁碟機已用及可用空間
- 資料庫備份檔所在的磁碟機已用及可用空間
資料庫運行效能
資料庫的運行效能主要受硬體主機的效能影響,可以就下列幾個效能指標進行檢測:
記憶體使用情形檢查
使用SSMS工具執行下列指令,取得MS-SQL Server記憶體使用情形:
dbcc memorystatus;
主要觀察兩個指標:
- Memory Manager\Target committed
此為SQL Server有效運行所需的記憶體
- Memory Manager\Current committed
此為SQL Server目前使用的記憶體
- 兩者差異過大則表示記憶體不足情形嚴重
使用SSMS執行這個指令觀察所有資料庫效能指標(適合進階使用者)
select * from sys.dm_os_performance_counters;
硬碟使用情形檢查
可透過MS-Windows內建的「效能監視器」觀察重要的效能指標
開始 > 執行 >
perfmon
PhysicalDisk: AVG. Disk Queue Length
磁碟駐列長度,若持續大於2則可能影響效能
- PhysicalDisk: AVG. Disk sec/Read
平均讀取時間,理想狀況下應小於10 msec
- PhysicalDisk: AVG. Disk sec/Write
平均寫入時間,理想狀況下應小於10 msec
CPU使用情形檢查
可透過MS-Windows內建的「效能監視器」觀察重要的效能指標
開始 > 執行 >
perfmon
System: Processor Queue Length
系統駐列長度,若持續大於3則表示CPU等級不足以應付系統需求
- Processor: Privilege Time
處理器花費在執行Microsoft Windows核心命令的時間百分比,若大於20%則須關注是否有I/O bottleneck或Memory leak問題
資料庫維運維護
確認完執行環境及效能指標後,資料庫本身的為運主要為index、備份及使用趨勢為主,具體的執行項目如下:
- 資料庫檔案增長趨勢
- Log檔案增長趨勢
- Log檔案過大可考慮備份後進行清理
- Index檔重整
- 查看維護計畫執行狀況,可透過下列指令查詢:
select
category = jc.name,
category_id = jc.category_id,
job_name = j.name,
job_enabled = j.enabled,
last_run_time = cast(js.last_run_date as varchar(10))
+ '-' + cast(js.last_run_time as varchar(10)),
last_run_duration = js.last_run_duration,
last_run_status = js.last_run_outcome,
last_run_msg = js.last_outcome_message
+ cast(nullif(js.last_run_outcome,1) as varchar(2)),
job_created = j.date_created,
job_modified = j.date_modified
from msdb.dbo.sysjobs j
inner join msdb.dbo.sysjobservers js
on j.job_id = js.job_id
inner join msdb.dbo.syscategories jc
on j.category_id = jc.category_id
where j.enabled = 1
and js.last_run_outcome in (0,1,3,5);
- 查看並驗證備份是否成功