码迷,mamicode.com
首页 > 数据库 > 详细

SQLServer数据库,表内存,实例名分析SQL语句

时间:2017-05-11 13:27:23      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:files   sel   char   server   sed   arc   reserve   bst   内存   

--数据库内存分析
USE master
go
DECLARE @insSize TABLE(dbName sysname,checkTime VARCHAR(19),dbSize VARCHAR(50),logSize VARCHAR(50))
INSERT INTO @insSize ( dbName, checkTime, dbSize, logSize )
EXEC sp_msforeachdb ‘select ‘‘?‘‘ dbName,CONVERT(VARCHAR(19),GETDATE(),120) checkTime,LTRIM(STR(SUM(CASE WHEN RIGHT(FILENAME,3)<>‘‘ldf‘‘ THEN convert (dec (15,2),size) * 8 / 1024 ELSE 0 END),15,2)+‘‘ MB‘‘) dbSize,  
                 LTRIM(STR(SUM(CASE WHEN RIGHT(FILENAME,3)=‘‘ldf‘‘  THEN convert (dec (15,2),size) * 8 / 1024 ELSE 0 END),15,2)+‘‘ MB‘‘) logSize from ?.dbo.sysfiles‘
--SELECT * FROM @insSize ORDER BY CONVERT(DECIMAL,LTRIM(RTRIM(SUBSTRING(dbSize,1,LEN(dbSize)-2)))) DESC
SELECT checkTime AS ‘检查日期‘, dbName AS ‘数据库‘,dbSize AS ‘数据容量‘,logSize AS ‘日志容量‘,CAST(CAST(LEFT(dbSize,LEN(dbSize)-2) as decimal(10,2))+CAST(LEFT(logSize,LEN(logSize)-2) as decimal(10,2)) AS VARCHAR(20))+‘MB‘ AS ‘占用空间‘ FROM @insSize 
WHERE dbName NOT IN(‘master‘,‘model‘,‘tempdb‘,‘msdb‘,‘distribution‘)
ORDER BY CONVERT(DECIMAL,LTRIM(RTRIM(SUBSTRING(dbSize,1,LEN(dbSize)-2)))) DESC

--表空间分析
BEGIN
USE[EMPI]
--select count(1) from sysobjects where xtype=‘U‘数据库中表的数量
create table #t(name varchar(255), rows bigint, reserved varchar(20), data varchar(20), index_size varchar(20), unused varchar(20))
exec sp_MSforeachtable "insert into #t exec sp_spaceused ‘?‘" 
select name AS ‘表名‘,rows AS ‘数据行数‘,CAST(CAST(CAST(LEFT(data,LEN(data)-2) AS FLOAT)/1024 AS DECIMAL(10,3)) AS VARCHAR(20))+‘MB‘ AS ‘占用空间‘,
CAST(CAST(CAST(LEFT(reserved,LEN(reserved)-2) AS FLOAT)/1024 AS DECIMAL(10,3)) AS VARCHAR(20))+‘MB‘ AS ‘保留的总空间‘,index_size AS ‘索引的空间‘,unused AS ‘未用空间‘ FROM #t 
ORDER BY CONVERT(DECIMAL,LTRIM(RTRIM(SUBSTRING(data,1,LEN(data)-2)))) DESC
drop table #t
end



--用户名以及权限(不全)
use master
go
SELECT  u.name AS ‘用户名‘,g.name AS ‘权限‘, u.default_database_name AS ‘默认数据库‘
  from sys.server_principals u, sys.server_principals g, sys.server_role_members m
 where g.principal_id = m.role_principal_id
   and u.principal_id = m.member_principal_id
  order by 1, 2
go
 
--数据库实例名
select @@SERVICENAME

  

SQLServer数据库,表内存,实例名分析SQL语句

标签:files   sel   char   server   sed   arc   reserve   bst   内存   

原文地址:http://www.cnblogs.com/wangboke/p/6840457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!