sql commands

1,DBCC SQLPERF(logspace)

https://msdn.microsoft.com/en-us/library/ms189768.aspx

2,

  1, LOGINFO(''HAHA3'')

  2,

CREATE TABLE #tracestatus (   RecoceryUntitID INT,     FileID INT,    FileSize INT,    StartOffset INT,    FSeqNo INT,    Status INT,    Parity INT,    CreateLSN VARCHAR(100)    )

-- Execute the command, putting the results in the table INSERT INTO #tracestatus EXEC ('DBCC LOGINFO(''HAHA3'')')

-- Display the results SELECT  sum(FileSize) FROM #tracestatus   GO

 

3,

 

CREATE DATABASE [DBMaint2008];
GO
USE [DBMaint2008];
GO
CREATE TABLE [TestTable] ([C1] INT IDENTITY, [C2] CHAR (100));
GO
-- Take a full backup
BACKUP DATABASE [DBMaint2008] TO DISK = N'D:\SQLskills\DemoBackups\DBMaint_Full.bck' WITH INIT;
GO
-- Insert some rows
INSERT INTO [TestTable] VALUES ('Transaction 1');
INSERT INTO [TestTable] VALUES ('Transaction 2');
GO
-- Take a log backup

BACKUP LOG [DBMaint2008] TO DISK = N'D:\SQLskills\DemoBackups\DBMaint_Log1.bck' WITH INIT;
GO
-- Insert some more rows
INSERT INTO [TestTable] VALUES ('Transaction 3');
INSERT INTO [TestTable] VALUES ('Transaction 4');
RESTORE HEADERONLY FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\CVS.bak' WITH NOUNLOAD; GO
SELECT name, physical_name AS CurrentLocation, state_desc FROM sys.master_files WHERE database_id = DB_ID(N'CVS');
RESTORE DATABASE [CVS] FROM  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\CVS.bak' WITH  FILE = 1, NORECOVERY, MOVE N'CVS' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\S\CVS.MDF',  MOVE N'CVS_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\S\CVS_log.LOG',  NOUNLOAD,  STATS = 5  
RESTORE LOG [CVS] FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\CVS.bak' WITH NORECOVERY  , FILE = 2; GO
RESTORE DATABASE [CVS] WITH RECOVERY; GO

 

 

4,HIGH CPU

use tempdb

drop table query_snapshot1

drop table query_snapshot2

 

select * into   query_snapshot1 from sys.dm_exec_query_stats

waitfor delay '00:01:00'

select * into   query_snapshot2 from sys.dm_exec_query_stats

select top 20   SUBSTRING(text, (c.statement_start_offset/2)+1,

          ((CASE c.statement_end_offset

            WHEN -1 THEN DATALENGTH(text)

           ELSE c.statement_end_offset

           END - c.statement_start_offset)/2) + 1) AS statement_text, c.diff_cpu,   objectid, plan_handle from  (

select a.sql_handle,   b.total_worker_time- a.total_worker_time diff_cpu, b.plan_handle,   b.statement_start_offset, b.statement_end_offset from tempdb..query_snapshot1   a, tempdb..query_snapshot2 b

where a.sql_handle =   b.sql_handle

and   a.statement_start_offset = b.statement_start_offset

and a.statement_end_offset   = b.statement_end_offset) c

cross apply   sys.dm_exec_sql_text(c.sql_handle)

order by c.diff_cpu desc

Query 1

select * from sys.dm_exec_query_plan(0x060001001C70C614506F946B0400000001000000000000000000000000000000000000000000000000000000)

Query 2

 select TOP 10 t.text,mg.cpu_time,mg.session_id, qp.query_plan,mg.last_wait_type,mg.percent_complete,
  mg.command,mg.start_time,mg.reads,mg.writes ,mg.wait_time,mg.wait_type,mg.wait_resource
  from sys.dm_exec_requests mg
  CROSS APPLY sys.dm_exec_query_plan(mg.plan_handle) AS qp
  CROSS APPLY sys.dm_exec_sql_text(mg.sql_handle) AS t
  Order by mg.cpu_time desc

 

5,Repair Database

ALTER DATABASE [CVS] SET EMERGENCY; 
GO 
ALTER DATABASE [CVS] SET SINGLE_USER; 
GO 
DBCC CHECKDB (N'CVS', REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS, NO_INFOMSGS; 
GO 
ALTER DATABASE [CVS]  
SET Multi_USER; 
GO

other reference 
http://www.sqlskills.com/blogs/paul/disaster-recovery-101-backing-up-the-tail-of-the-log/

 

你可能感兴趣的:(command)