-- 查询SqlServer总体的内存使用情况 select type , sum(virtual_memory_reserved_kb) VM_Reserved , sum(virtual_memory_committed_kb) VM_Commited , sum(awe_allocated_kb) AWE_Allocated , sum(shared_memory_reserved_kb) Shared_Reserved , sum(shared_memory_committed_kb) Shared_Commited --, sum(single_pages_kb) --SQL2005、2008 --, sum(multi_pages_kb) --SQL2005、2008 from sys.dm_os_memory_clerks group by type order by type --查看每个数据库对内存的占用 SELECT ISNULL(DB_NAME(DATABASE_ID),'RESOURCEDB') AS DATABASENAME, CAST(COUNT(ROW_COUNT) * 8.0 /(1024.0) AS DECIMAL(28,2)) AS 'SIZE(MB)' FROM SYS.dm_os_buffer_descriptors GROUP BY database_id ORDER BY DATABASENAME -- 查询当前数据库缓存的所有数据页面,哪些数据表,缓存的数据页面数量 -- 从这些信息可以看出,系统经常要访问的都是哪些表,有多大? select p.object_id, object_name=object_name(p.object_id), p.index_id, buffer_pages=count(*) from sys.allocation_units a, sys.dm_os_buffer_descriptors b, sys.partitions p where a.allocation_unit_id=b.allocation_unit_id and a.container_id=p.hobt_id and b.database_id=db_id() group by p.object_id,p.index_id order by buffer_pages desc -- 查询缓存的各类执行计划,及分别占了多少内存 -- 可以对比动态查询与参数化SQL(预定义语句)的缓存量 select cacheobjtype , objtype , sum(cast(size_in_bytes as bigint))/1024 as size_in_kb , count(bucketid) as cache_count from sys.dm_exec_cached_plans group by cacheobjtype, objtype order by cacheobjtype, objtype -- 查询缓存中具体的执行计划,及对应的SQL -- 将此结果按照数据表或SQL进行统计,可以作为基线,调整索引时考虑 -- 查询结果会很大,注意将结果集输出到表或文件中 SELECT usecounts , refcounts , size_in_bytes , cacheobjtype , objtype , TEXT FROM sys.dm_exec_cached_plans cp CROSS APPLY sys.dm_exec_sql_text(plan_handle) ORDER BY objtype DESC ; GO -----------查询缓存的表---------------------- SELECT count(*)*8 AS cached_pages_kb,obj.name ,obj.index_id,b.type_desc,b.name FROM sys.dm_os_buffer_descriptors AS bd INNER JOIN(SELECT object_name(object_id) AS name,index_id ,allocation_unit_id,object_id FROM sys.allocation_units AS au INNER JOIN sys.partitions AS p ON au.container_id = p.hobt_id AND (au.type = 1 OR au.type = 3) UNION ALL SELECT object_name(object_id) AS name,index_id, allocation_unit_id,object_id FROM sys.allocation_units AS au INNER JOIN sys.partitions AS p ON au.container_id = p.partition_id AND au.type = 2) AS obj ON bd.allocation_unit_id = obj.allocation_unit_id LEFT JOIN sys.indexes b on b.object_id = obj.object_id AND b.index_id = obj.index_id WHERE database_id = db_id() --and obj.name='StoreOut' GROUP BY obj.name, obj.index_id ,b.name,b.type_desc ORDER BY cached_pages_kb DESC; -------------如果要查询是否连接没有释放引起的, 你可以用查询分析器连接到你的数据库服务器, 执行下面的代码:----------------------------- select * from master.dbo.sysprocesses where spid>50 and waittype = 0x0000 and waittime = 0 and status = 'sleeping' and last_batch < dateadd(minute, -10, getdate()) and login_time < dateadd(minute, -10, getdate()) ----mssql 判断sql语句的执行效率语句 SET STATISTICS io ON SET STATISTICS time ON go --========此处为sql代码段=============== select count(1) from ProductInfo mb where (1=1 and charindex('53875-027',mb.SeachKey)>=1 And mb.FactName in ('丰田','品牌') And mb.Status=1) select count(1) from ProductInfo mb where (1=1 and exists(select * from f_split('品牌,丰田',',') t where t.col=mb.FactName)) go SET STATISTICS profile OFF SET STATISTICS io OFF SET STATISTICS time OFF ------------------------查询索引------------------------------------------------ SELECT CASE WHEN t.[type] = 'U' THEN '表' WHEN t.[type] = 'V' THEN '视图' END AS '类型', SCHEMA_NAME(t.schema_id) + '.' + t.[name] AS '(表/视图)名称', 'drop index [' + i.name + '] ON [' + t.name + ']' AS DropIndexCommand, i.[name] AS 索引名称, SUBSTRING(column_names, 1, LEN(column_names) - 1) AS '列名', CASE WHEN i.[type] = 1 THEN '聚集索引' WHEN i.[type] = 2 THEN '非聚集索引' WHEN i.[type] = 3 THEN 'XML索引' WHEN i.[type] = 4 THEN '空间索引' WHEN i.[type] = 5 THEN '聚簇列存储索引' WHEN i.[type] = 6 THEN '非聚集列存储索引' WHEN i.[type] = 7 THEN '非聚集哈希索引' END AS '索引类型', CASE WHEN i.is_unique = 1 THEN '唯一' ELSE '不唯一' END AS '索引是否唯一' FROM sys.objects t INNER JOIN sys.indexes i ON t.object_id = i.object_id CROSS APPLY ( SELECT col.[name] + ', ' FROM sys.index_columns ic INNER JOIN sys.columns col ON ic.object_id = col.object_id AND ic.column_id = col.column_id WHERE ic.object_id = t.object_id AND ic.index_id = i.index_id ORDER BY col.column_id FOR XML PATH('') ) D(column_names) WHERE t.is_ms_shipped <> 1 AND index_id > 0 ORDER BY i.[name]; ----------------查询索引碎片---------------------------------------- SELECT DB_NAME(ips.database_id) DBName, OBJECT_NAME(ips.object_id) ObjName, i.name IndexName, ips.avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats(DB_ID('AppsDB'), DEFAULT,DEFAULT,DEFAULT,default) ips INNER JOIN sys.indexes i ON ips.index_id = i.index_id AND ips.object_id = i.object_id WHERE ips.object_id>99 AND ips.avg_fragmentation_in_percent>=10 AND ips.index_id>0 ------------------------------------------------------------------ ----------------查询表的索引信息----------------------- DBCC ShowContig(StoreIn) 重建表的索引 DBCC DBREINDEX(表,'索引名称',填充因子) DBCC DBREINDEX(StoreIn,'',80) ------------------------------------
如果您也喜欢它,动动您的小指点个赞吧