• 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
    

      

  • 相关阅读:
    一道华为笔试题--内存块排序
    正则表达式
    Paxos算法的一个简单小故事
    Zookeeper中的Leader选取机制
    CCF201604-2俄罗斯方块
    Mybatis----Mapper.xml中的输入映射
    Mybatis----mybatis的全局配置文件SqlMapConfig.xml的配置
    Mybatis----开发dao
    Mybatis----入门程序
    Spring----面向切面编程和通知类型
  • 原文地址:https://www.cnblogs.com/wangboke/p/6840457.html
Copyright © 2020-2023  润新知