1、查询耗时较久的sql语句
SELECT TOP 20 total_worker_time/1000 AS [总消耗CPU 时间(ms)],execution_count [运行次数], qs.total_worker_time/qs.execution_count/1000 AS [平均消耗CPU 时间(ms)], last_execution_time AS [最后一次执行时间],max_worker_time /1000 AS [最大执行时间(ms)], SUBSTRING(qt.text,qs.statement_start_offset/2+1, (CASE WHEN qs.statement_end_offset = -1 THEN DATALENGTH(qt.text) ELSE qs.statement_end_offset END -qs.statement_start_offset)/2 + 1) AS [使用CPU的语法], qt.text [完整语法], qt.dbid, dbname=db_name(qt.dbid), qt.objectid,object_name(qt.objectid,qt.dbid) ObjectName FROM sys.dm_exec_query_stats qs WITH(nolock) CROSS apply sys.dm_exec_sql_text(qs.sql_handle) AS qt WHERE execution_count>1 ORDER BY total_worker_time DESC
SELECT s2.dbid, s1.sql_handle, (SELECT TOP 1 SUBSTRING(s2.text,statement_start_offset / 2+1 , ( (CASE WHEN statement_end_offset = -1 THEN (LEN(CONVERT(nvarchar(max),s2.text)) * 2) ELSE statement_end_offset END) - statement_start_offset) / 2+1)) AS sql_statement, execution_count, plan_generation_num, last_execution_time, total_worker_time, last_worker_time, min_worker_time, max_worker_time, total_physical_reads, last_physical_reads, min_physical_reads, max_physical_reads, total_logical_writes, last_logical_writes, min_logical_writes, max_logical_writes FROM sys.dm_exec_query_stats AS s1 CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2 WHERE s2.objectid is null ORDER BY last_worker_time desc,s1.sql_handle, s1.statement_start_offset, s1.statement_end_offset;
2、数据库还原
USE [master] RESTORE DATABASE ETM_Cloud FROM DISK = N'E:DB_backupETM_Cloud_FullBackup_2018_04_08_162313.bak' WITH FILE = 1, MOVE N'ETM_Cloud' TO N'D:Program FilesMicrosoft SQL ServerMSSQL11.MSSQLSERVERMSSQLDATAETM_Cloud.mdf', MOVE N'ETM_Cloud_log' TO N'D:Program FilesMicrosoft SQL ServerMSSQL11.MSSQLSERVERMSSQLDATAETM_Cloud_log.ldf', NOUNLOAD,NORECOVERY, REPLACE, STATS = 5 GO --注意一定要用NORECOVERY来还原备份 USE [master] RESTORE DATABASE [ETM_Cloud] FROM DISK = N'E:DB_backupETM_Cloud_logBackup_2018_04_08_162313.bak' WITH FILE = 1, NOUNLOAD,NORECOVERY, REPLACE, STATS = 5 GO
3、日志清理(alwayson_clean_log)
--备份事务日志,备份成NUL,就不用占用硬盘空间 BACKUP LOG [ECloud] TO DISK='NUL:' USE [ECloud] GO --收缩数据库日志文件 DBCC SHRINKFILE(ECloud_log,10) GO