Sie sind auf Seite 1von 3

select top 5 database_name,convert(varchar(20),backup_finish_date) as Date, (bac

kup_size/1024/1024) as Size from msdb..backupset where type='l'


order by backup_finish_date desc
--------------------------------------------------------------------------------
--------------------------------
SELECT * FROM fn_trace_getinfo(default);-- to check the traces running on the in
stance.
GO
--------------------------------------------------------------------------------
--------------------------------
select compatibility_level from sys.databases where name in (select name from ma
ster..sysdatabases where name not in ('master','model','tempdb','msdb'))..to fin
d the compatibility level of user db..
--------------------------------------------------------------------------------
----------------------------------
select name,recovery_model from sys.databases where name in (select name from m
aster..sysdatabases where name not in ('master','model','tempdb','msdb')) --to f
ind the recovery model of all the user db...
--------------------------------------------------------------------------------
----------------------------------
select name, is_auto_close_on from sys.databases where name in (select name fro
m master..sysdatabases where name not in ('master','model','tempdb','msdb'))
--------------------------------------------------------------------------------
--------------------------------
select name, is_auto_shirnk_on from sys.databases where name in (select name fr
om master..sysdatabases where name not in ('master','model','tempdb','msdb'))
--------------------------------------------------------------------------------
---------------------------------
Find Long running queries in SQL2005
Select dmText.text as 'Executed Query',dmStats.last_execution_time as 'Last Exec
uted Time', dmstats.*
from
sys.dm_exec_query_stats as dmStats
Cross apply
sys.dm_exec_sql_text(dmStats.sql_handle) as dmText
Order By
total_elapsed_time desc,
dmStats.last_execution_time desc
--------------------------------------------------------------------------------
---------------------------------
see the physical memory allocation
SELECT
(Physical_memory_in_bytes/1024.0)/1024.0 AS Physical_memory_in_Mb
FROM sys.dm_os_sys_info
how much memory SQL Server has allocated through AWE.
SELECT
SUM(awe_allocated_kb) / 1024 as [AWE allocated, Mb]
FROM sys.dm_os_memory_clerks
get the memory consumption by internal components of SQL Server 2005
SELECT TOP 10 type,
SUM(single_pages_kb) as [SPA Mem, Kb]
FROM sys.dm_os_memory_clerks
GROUP BY type
ORDER BY SUM(single_pages_kb) DESC
Finally, using dbcc memorystatus can get the snapshot for the system memory alloca
tion, ms s kb artiacle:
http://support.microsoft.com/kb/907877
--------------------------------------------------------------------------------
-----------------------------------
how to check the user s server permission
select a.* from sys.server_permissions a,sys.server_principals b
where a.grantee_principal_id=b.principal_id
and b.name= %username% ;
--------------------------------------------------------------------------------
----------------------------------
fixed orphaned SQL Server users
EXEC sp_change_users_login 'Report'
EXEC sp_change_users_login 'Auto_Fix', 'user'
EXEC sp_change_users_login 'Auto_Fix', 'user', 'login', 'password'
--------------------------------------------------------------------------------
-----------------------------------
Identifying db locks
performance monitor -> locks objects
profiler
might be caused by I/O bottleneck
try to use index for update and delete
SP_INDEXOPTION : page lock, row lock, table lock ...etc
use nolock for select (dirty read)
sp_lock
sp_who/sp_who2
--shwo long running process--
SELECT spid, cmd, status, loginame, open_tran, datediff(s, last_batch, getdate (
)) AS [WaitTime(s)]
FROM master..sysprocesses p
WHERE open_tran > 0
AND spid > 50
AND datediff (s, last_batch, getdate ()) > 30
ANd EXISTS (SELECT * FROM master..syslockinfo l
WHERE req_spid = p.spid AND rsc_type <> 2)
SELECT spid, waittime, lastwaittype, waitresource
FROM master..sysprocesses
WHERE waittime > 10000 --The wait time is measured in milliseconds
AND spid > 50
--------------------------------------------------------------------------------
----------------------------------
Find Objects in SQL Server
*********************************************************
** Find The Names And Types Of The T-SQL Objects
** Used In A Stored Procedure
**
** By: Dave Vroman
*********************************************************/
DECLARE @ProcName VARCHAR(50)
SET @ProcName = 'MyTestProc'
SELECT [Name],
CASE WHEN xType = 'C' THEN 'CHECK Constraint'
WHEN xType = 'D' THEN 'Default Or DEFAULT Constraint'
WHEN xType = 'F' THEN 'FOREIGN KEY Constraint'
WHEN xType = 'L' THEN 'Log'
WHEN xType = 'FN' THEN 'Scalar Function'
WHEN xType = 'IF' THEN 'Inlined Table-Function'
WHEN xType = 'P' THEN 'Stored Procedure'
WHEN xType = 'PK' THEN 'PRIMARY KEY Constraint (Type Is K)'
WHEN xType = 'RF' THEN 'Replication Filter Stored Procedure'
WHEN xType = 'S' THEN 'System Table'
WHEN xType = 'TF' THEN 'Table Function'
WHEN xType = 'TR' THEN 'Trigger'
WHEN xType = 'U' THEN 'User Table'
WHEN xType = 'UQ' THEN 'UNIQUE Constraint (Type Is K)'
WHEN xType = 'V' THEN 'View'
WHEN xType = 'X' THEN 'Extended Stored Procedure'
ELSE xType END AS xType
FROM sysobjects
WHERE id IN
(SELECT sd.depid FROM sysobjects so, sysdepends sd
WHERE so.name = @ProcName AND sd.id = so.id )
--------------------------------------------------
SELECT Distinct SO.Name, SO.Type
FROM sysobjects SO (NOLOCK)
INNER JOIN syscomments SC (NOLOCK) on SO.Id = SC.ID
--AND SO.Type = 'P' --U,P,V
AND SC.Text LIKE '%ObjectsName%' --need to be replicated
ORDER BY SO.Name