--查看死锁进程spid sp_who2 active --进程持续排行cpu: SELECT * FROM sys.sysprocesses order by cpu desc --查询造成阻塞或死锁的Sql语句: dbcc inputbuffer(520) --查看数据库列表 select* from master..sysdatabases 查看锁表: select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran_locks where resource_type='OBJECT' --杀死进程: kill 520 --找出执行最慢的SQL语句(适用于SQL SERVER 2005及其以上版本) --https://blog.csdn.net/CSDN515/article/details/120428652 SELECT (total_elapsed_time / execution_count)/1000 N'平均时间ms' ,total_elapsed_time/1000 N'总花费时间ms' ,total_worker_time/1000 N'所用的CPU总时间ms' ,total_physical_reads N'物理读取总次数' ,total_logical_reads/execution_count N'每次逻辑读次数' ,total_logical_reads N'逻辑读取总次数' ,total_logical_writes N'逻辑写入总次数' ,execution_count N'执行次数' ,SUBSTRING(st.text, (qs.statement_start_offset/2) + 1 ,((CASE statement_end_offset WHEN -1 THEN DATALENGTH(st.text) ELSE qs.statement_end_offset END - qs.statement_start_offset)/2) + 1) N'执行语句' ,creation_time N'语句编译时间' ,last_execution_time N'上次执行时间' FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st WHERE SUBSTRING(st.text, (qs.statement_start_offset/2) + 1, ((CASE statement_end_offset WHEN -1 THEN DATALENGTH(st.text) ELSE qs.statement_end_offset END - qs.statement_start_offset)/2) + 1) not like 'fetch%' ORDER BY total_elapsed_time / execution_count DESC; set statistics profile on set statistics io on set statistics time on go --sql go set statistics profile off set statistics io off set statistics time off --查询数据库中表数据条数排序 SELECT B.NAME,A.ROW_COUNT FROM SYS.DM_DB_PARTITION_STATS A, SYS.OBJECTS B WHERE A.OBJECT_ID=B.OBJECT_ID AND A.INDEX_ID<=1 AND B.TYPE='U'AND A.ROW_COUNT>0 ORDER BY ROW_COUNT DESC --查询数据库表占用空间和记录情况 SELECT t.NAME AS TableName, s.Name AS SchemaName, p.rows AS RowCounts, SUM(a.total_pages) * 8 AS TotalSpaceKB, CAST(ROUND(((SUM(a.total_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS TotalSpaceMB, SUM(a.used_pages) * 8 AS UsedSpaceKB, CAST(ROUND(((SUM(a.used_pages) * 8) / 1024.00), 2) AS NUMERIC(36, 2)) AS UsedSpaceMB, (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB, CAST(ROUND(((SUM(a.total_pages) - SUM(a.used_pages)) * 8) / 1024.00, 2) AS NUMERIC(36, 2)) AS UnusedSpaceMB FROM sys.tables t INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE 1=1 AND t.is_ms_shipped = 0 AND i.OBJECT_ID > 255 GROUP BY t.Name, s.Name, p.Rows ORDER BY p.rows desc
标签:qs,--,SUM,SqlServer,sys,排查,total,优化,id From: https://www.cnblogs.com/wangdongying/p/17503439.html