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

Posted 名扬博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQLServer数据库,表内存,实例名分析SQL语句相关的知识,希望对你有一定的参考价值。

--数据库内存分析
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语句的主要内容,如果未能解决你的问题,请参考以下文章

如何设置SQLServer数据库内存

sQL server 啥是实例

SQL操作语句

SQLServer 2014 内存优化表

sql server 怎样将字符串转化为时间格式

sql 2008 R2 怎样 释放内存