--查询当前的事务 select t2.session_id as SPID, t2.transaction_id, transaction_begin_time, N‘已执行‘+ltrim(datediff(mi,transaction_begin_time,getdate()))+N‘分钟‘ as mi, case transaction_type when 1 then N‘读/写事务‘ when 2 then N‘只读事务‘ when 3 then N‘系统事务‘ when 4 then N‘分布式事务‘ end tran_Type, case transaction_state when 0 then N‘事务尚未完全初始化‘ when 1 then N‘事务已初始化但尚未启动‘ when 2 then N‘事务处于活动状态‘ when 3 then N‘事务已结束。该状态用于只读事务‘ when 4 then N‘已对分布式事务启动提交进程‘ when 5 then N‘事务处于准备就绪状态且等待解析‘ when 6 then N‘事务已提交‘ when 7 then N‘事务正在被回滚‘ when 0 then N‘事务已回滚‘ end transaction_state, client_net_address, client_tcp_port, program_name, t2.text from sys.dm_tran_active_transactions t1 join ( select a.session_id, transaction_id, client_net_address, client_tcp_port, text,c.program_name from sys.dm_tran_session_transactions a join ( select session_id,a2.text,client_net_address,client_tcp_port from sys.dm_exec_connections a1 cross apply sys.dm_exec_sql_text(a1.most_recent_sql_Handle) a2 ) b on a.session_id=b.session_id left join sys.dm_exec_sessions c on a.session_id=c.session_id where is_user_transaction=1 )t2 on t1.transaction_ID=t2.transaction_ID ORDER BY t2.transaction_id ----索引执行情况 SELECT objects.name , databases.name , indexes.name , user_seeks , user_scans , user_lookups , partition_stats.row_count FROM sys.dm_db_index_usage_stats stats LEFT JOIN sys.objects objects ON stats.object_id = objects.object_id LEFT JOIN sys.databases databases ON databases.database_id = stats.database_id LEFT JOIN sys.indexes indexes ON indexes.index_id = stats.index_id AND stats.object_id = indexes.object_id LEFT JOIN sys.dm_db_partition_stats partition_stats ON stats.object_id = partition_stats.object_id AND indexes.index_id = partition_stats.index_id WHERE 1 = 1 --AND databases.database_id = 7 AND objects.name IS NOT NULL AND indexes.name IS NOT NULL AND user_scans>0 ORDER BY user_scans DESC ,stats.object_id ,indexes.index_id ----最占用CPU SELECT TOP 100 execution_count, total_logical_reads /execution_count AS [Avg Logical Reads], total_elapsed_time /execution_count AS [Avg Elapsed Time], db_name(st.dbid) as [database name], object_name(st.dbid) as [object name], object_name(st.objectid) as [object name 1], 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) AS statement_text FROM sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st WHERE execution_count > 100 AND qs.creation_time > dateadd(day,-3,getdate()) ORDER BY 1 DESC; ---执行时间最长的命令 SELECT TOP 10 COALESCE(DB_NAME(st.dbid), DB_NAME(CAST(pa.value as int))+‘*‘,‘Resource‘) AS DBNAME, SUBSTRING(text, -- starting value for substring CASE WHEN statement_start_offset = 0 OR statement_start_offset IS NULL THEN 1 ELSE statement_start_offset/2 + 1 END, -- ending value for substring CASE WHEN statement_end_offset = 0 OR statement_end_offset = -1 OR statement_end_offset IS NULL THEN LEN(text) ELSE statement_end_offset/2 END - CASE WHEN statement_start_offset = 0 OR statement_start_offset IS NULL THEN 1 ELSE statement_start_offset/2 END + 1 ) AS TSQL, total_logical_reads/execution_count AS AVG_LOGICAL_READS FROM sys.dm_exec_query_stats CROSS APPLY sys.dm_exec_sql_text(sql_handle) st OUTER APPLY sys.dm_exec_plan_attributes(plan_handle) pa WHERE attribute = ‘dbid‘ ORDER BY AVG_LOGICAL_READS DESC ; ----缺索引 SELECT TOP 30 [Total Cost] = ROUND(avg_total_user_cost * avg_user_impact * (user_seeks + user_scans),0) , avg_user_impact, TableName = statement, [EqualityUsage] = equality_columns , [InequalityUsage] = inequality_columns, [Include Cloumns] = included_columns FROM sys.dm_db_missing_index_groups g INNER JOIN sys.dm_db_missing_index_group_stats s ON s.group_handle = g.index_group_handle INNER JOIN sys.dm_db_missing_index_details d ON d.index_handle = g.index_handle ORDER BY [Total Cost] DESC; --经常更新却很少使用的索引 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED SELECT DB_NAME() AS DatabaseName , SCHEMA_NAME(o.Schema_ID) AS SchemaName , OBJECT_NAME(s.[object_id]) AS TableName , i.name AS IndexName , s.user_updates , s.system_seeks + s.system_scans + s.system_lookups AS [System usage] INTO #TempUnusedIndexes FROM sys.dm_db_index_usage_stats s INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id INNER JOIN sys.objects o ON i.object_id = O.object_id WHERE 1=2 EXEC sp_MSForEachDB ‘USE [?]; INSERT INTO #TempUnusedIndexes SELECT TOP 20 DB_NAME() AS DatabaseName , SCHEMA_NAME(o.Schema_ID) AS SchemaName , OBJECT_NAME(s.[object_id]) AS TableName , i.name AS IndexName , s.user_updates , s.system_seeks + s.system_scans + s.system_lookups AS [System usage] FROM sys.dm_db_index_usage_stats s INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id INNER JOIN sys.objects o ON i.object_id = O.object_id WHERE s.database_id = DB_ID() AND OBJECTPROPERTY(s.[object_id], ‘‘IsMsShipped‘‘) = 0 AND s.user_seeks = 0 AND s.user_scans = 0 AND s.user_lookups = 0 AND i.name IS NOT NULL ORDER BY s.user_updates DESC‘ SELECT TOP 20 * FROM #TempUnusedIndexes WHERE DatabaseName = ‘agilepoint50‘ ORDER BY [user_updates] DESC DROP TABLE #TempUnusedIndexes ---维护代价最高的索引 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED SELECT DB_NAME() AS DatabaseName , SCHEMA_NAME(o.Schema_ID) AS SchemaName , OBJECT_NAME(s.[object_id]) AS TableName , i.name AS IndexName , (s.user_updates ) AS [update usage] , (s.user_seeks + s.user_scans + s.user_lookups) AS [Retrieval usage] , (s.user_updates) - (s.user_seeks + s.user_scans + s.user_lookups) AS [Maintenance cost] , s.system_seeks + s.system_scans + s.system_lookups AS [System usage] , s.last_user_seek , s.last_user_scan , s.last_user_lookup INTO #TempMaintenanceCost FROM sys.dm_db_index_usage_stats s INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id INNER JOIN sys.objects o ON i.object_id = O.object_id WHERE 1=2 EXEC sp_MSForEachDB ‘USE [?]; INSERT INTO #TempMaintenanceCost SELECT TOP 20 DB_NAME() AS DatabaseName , SCHEMA_NAME(o.Schema_ID) AS SchemaName , OBJECT_NAME(s.[object_id]) AS TableName , i.name AS IndexName , (s.user_updates ) AS [update usage] , (s.user_seeks + s.user_scans + s.user_lookups) AS [Retrieval usage] , (s.user_updates) - (s.user_seeks + user_scans + s.user_lookups) AS [Maintenance cost] , s.system_seeks + s.system_scans + s.system_lookups AS [System usage] , s.last_user_seek , s.last_user_scan , s.last_user_lookup FROM sys.dm_db_index_usage_stats s INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id INNER JOIN sys.objects o ON i.object_id = O.object_id WHERE s.database_id = DB_ID() AND i.name IS NOT NULL AND OBJECTPROPERTY(s.[object_id], ‘‘IsMsShipped‘‘) = 0 AND (s.user_seeks + s.user_scans + s.user_lookups) > 0 ORDER BY [Maintenance cost] DESC‘ SELECT top 20 * FROM #TempMaintenanceCost WHERE DatabaseName =‘agilepoint50‘ ORDER BY [Maintenance cost] DESC DROP TABLE #TempMaintenanceCost ---表及数据库的空间占用 set nocount on declare @db varchar(20) set @db = db_name() dbcc updateusage(@db) with no_infomsgs go create table #tblspace ( 数据表名称 varchar(50) null, 记录笔数 int null, 保留空间 varchar(15) null, 数据使用空间 varchar(15) null, 索引使用空间 varchar(15) null, 未使用空间 varchar(15) null, ) declare @tblname varchar(50) declare curtbls cursor for select table_name from information_schema.tables where table_type = ‘base table‘ open curtbls Fetch next from curtbls into @tblname while @@fetch_status = 0 begin insert #tblspace exec sp_spaceused @tblname fetch next from curtbls into @tblname end close curtbls deallocate curtbls select * from #tblspace order by convert(int,left(保留空间,len(保留空间)-2)) desc drop table #tblspace
时间: 2024-10-10 22:58:12