改章节笔者在青岛逛街的时候突然想到的...之前就有想写几篇关于信息、数据-的博客,所以回家到之后就奋笔疾书的写出来发布了
近来做项目须要看查数据用户表的巨细,包含录记条数和占用的盘磁空间数目。在网上找了久很其中看查MSSQL数据库个每表占用的空间巨细 对相还可以。
不过它的2、3中法方返回的数据比较多,有些是我们不心关的数据,我在AdventureWorks2012数据上做的试测。其中第二种法方代码如下:
View Code if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tablespaceinfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) create table tablespaceinfo --建创结果存储表 (nameinfo varchar(50) , rowsinfo int , reserved varchar(20) , datainfo varchar(20) , index_size varchar(20) , unused varchar(20) ) delete from tablespaceinfo --清空数据表 declare @tablename varchar(255) --表称名 declare @cmdsql varchar(500) DECLARE Info_cursor CURSOR FOR select o.name from dbo.sysobjects o where OBJECTPROPERTY(o.id, N'IsTable') = 1 and o.name not like N'#%%' order by o.name OPEN Info_cursor FETCH NEXT FROM Info_cursor INTO @tablename WHILE @@FETCH_STATUS = 0 BEGIN if exists (select * from dbo.sysobjects where id = object_id(@tablename) and OBJECTPROPERTY(id, N'IsUserTable') = 1) execute sp_executesql N'insert into tablespaceinfo exec sp_spaceused @tbname', N'@tbname varchar(255)', @tbname = @tablename FETCH NEXT FROM Info_cursor INTO @tablename END CLOSE Info_cursor DEALLOCATE Info_cursor GO --itlearner注:表现数据库信息 sp_spaceused @updateusage = 'TRUE' --itlearner注:表现表信息 select * from tablespaceinfo order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc
很显然这个返回结果是错误的。但是它供给了一种思绪,修改后的SQL语句如下:
IF NOT EXISTS ( SELECT * FROM sys.tables WHERE name = 'tablespaceinfo' ) BEGIN CREATE TABLE tablespaceinfo --建创结果存储表 ( Table_Name VARCHAR(50) , Rows_Count INT , reserved INT , datainfo INT , index_size INT , unused INT ) END DELETE FROM tablespaceinfo --清空数据表 CREATE TABLE #temp --建创结果存储表 ( nameinfo VARCHAR(50) , rowsinfo INT , reserved VARCHAR(20) , datainfo VARCHAR(20) , index_size VARCHAR(20) , unused VARCHAR(20) ) DECLARE @tablename VARCHAR(255) --表称名 DECLARE @cmdsql NVARCHAR(500) DECLARE Info_cursor CURSOR FOR SELECT '[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']' AS Table_Name FROM [INFORMATION_SCHEMA].[TABLES] WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'tablespaceinfo' OPEN Info_cursor FETCH NEXT FROM Info_cursor INTO @tablename WHILE @@FETCH_STATUS = 0 BEGIN SET @cmdsql = 'insert into #temp exec sp_spaceused ''' + @tablename + '''' EXECUTE sp_executesql @cmdsql FETCH NEXT FROM Info_cursor INTO @tablename END CLOSE Info_cursor DEALLOCATE Info_cursor GO --itlearner注:表现数据库信息 --sp_spaceused @updateusage = 'TRUE' --itlearner注:表现表信息 UPDATE #temp SET reserved = REPLACE(reserved, 'KB', '') , datainfo = REPLACE(datainfo, 'KB', '') , index_size = REPLACE(index_size, 'KB', '') , unused = REPLACE(unused, 'KB', '') INSERT INTO dbo.tablespaceinfo SELECT nameinfo , CAST(rowsinfo AS INT) , CAST(reserved AS INT) , CAST(datainfo AS INT) , CAST(index_size AS INT) , CAST(unused AS INT) FROM #temp DROP TABLE #temp SELECT Table_Name , Rows_Count , CASE WHEN reserved > 1024 THEN CAST(reserved / 1024 AS VARCHAR(10)) + 'Mb' ELSE CAST(reserved AS VARCHAR(10)) + 'KB' END AS Data_And_Index_Reserved , CASE WHEN datainfo > 1024 THEN CAST(datainfo / 1024 AS VARCHAR(10)) + 'Mb' ELSE CAST(datainfo AS VARCHAR(10)) + 'KB' END AS Used , CASE WHEN Index_size > 1024 THEN CAST(index_size / 1024 AS VARCHAR(10)) + 'Mb' ELSE CAST(index_size AS VARCHAR(10)) + 'KB' END AS index_size , CASE WHEN unused > 1024 THEN CAST(unused / 1024 AS VARCHAR(10)) + 'Mb' ELSE CAST(unused AS VARCHAR(10)) + 'KB' END AS unused FROM dbo.tablespaceinfo ORDER BY reserved DESC
同时他的第三种法方返回的数据太多,很多是我们不怎么心关的,原SQL语句如下:
SELECT OBJECT_NAME(id) tablename , 8 * reserved / 1024 reserved , RTRIM(8 * dpages / 1024) + 'Mb' used , 8 * ( reserved - dpages ) / 1024 unused , 8 * dpages / 1024 - rows / 1024 * minlen / 1024 free , rows FROM sysindexes WHERE indid = 1 ORDER BY reserved DESC
这里面含包一些索引信息,其实我们只心关表占用盘磁信息,修改后的SQL语句如下:
SELECT OBJECT_NAME(id) tablename , CASE WHEN reserved * 8 > 1024 THEN RTRIM(8 * reserved / 1024) + 'MB' ELSE RTRIM(reserved * 8) + 'KB' END DataReserve , CASE WHEN dpages * 8 > 1024 THEN RTRIM(8 * dpages / 1024) + 'MB' ELSE RTRIM(dpages * 8) + 'KB' END Used , CASE WHEN 8 * ( reserved - dpages ) > 1024 THEN RTRIM(8 * ( reserved - dpages ) / 1024) + 'MB' ELSE RTRIM(8 * ( reserved - dpages )) + 'KB' END unused , CASE WHEN ( 8 * dpages / 1024 - rows / 1024 * minlen / 1024 ) > 1024 THEN RTRIM(( 8 * dpages / 1024 - rows / 1024 * minlen / 1024 ) / 1024) + 'MB' ELSE RTRIM(( 8 * dpages / 1024 - rows / 1024 * minlen / 1024 )) + 'KB' END FREE , rows AS Rows_Count FROM sys.sysindexes WHERE indid = 1 AND status = 2066 -- status='18' ORDER BY reserved DESC
有不对的地方欢送大家拍砖!
文章结束给大家分享下程序员的一些笑话语录: 话剧:程序员过沟
本剧内容纯属虚构,如有雷同……HEHE……俺也没办法了。
话说某市街道改建,某某软件公司门口横七竖八挖了几条大沟。一群程序员(SDK程序员赵某,VB程序员钱某,VC程序员孙某,DELPHI程序员李某)下班从公司里出来,看到门前的几条沟,于是各显神通……门前第一条沟也就半米来宽,SDK程序员赵某二话没说,轻轻一跃跳了过去,看到其它人纷纷把随身携带的公文包(类库)横在沟上踩着过沟,不屑地说,这么小一条沟,犯得着小题大做用那个吗?看我多么轻松多么洒脱多么……多么……(众人皆怒目横视之……)
接着第二条沟有点宽度。SDK程序员赵某还是还是一马当先,飞跃而起……不好,还差一点才到……幸好凭着多年的(跳远?编程?)经验,单手抓住沟沿,颤巍巍地爬了上来,嘴里还念念有词“高手就是高手啊,虽然差一点就……不过毕竟……HEHE……跳远是过沟的基础嘛,有基础(SDK)就有一切的说……”(众人作瞠目结舌状……)看到别人跳过去了,可自己又跳不了那么远,只好再想办法了……VB程序员钱某,DELPHI程序员李某打开手提,连上手机,开始上网找可供过沟的控件……VC程序员孙某却不慌不忙,打开公文包,把几块衬板拆了下来,然后三下五除二拼成一个简易木桥……“虽然这几个板子(类)做得不怎么样,不过先把这个项目应付过去,有时间我自己做一个好了……”于是踩着板子过了沟。
这时钱某和李某也分别找到了合适的东东。钱某找到的是“钢丝绳.ocx”,安装简单,使用方便,拉出一头,对孙某说“大虾,顺手拉兄弟一把……”,于是把绳子系在沟两边的绿化树木上,踩着钢丝就过了沟。刚刚站稳就四方作揖,“小生这里有礼了”。这时一戴着黄袖圈的老太太跳了出来,抓住钱某,“破坏绿化树木,罚款XXXX元,交钱,交钱,交钱!”(老人家作双枪老太婆怒视伪军状
……钱某被逼无奈,只好边掏钱,边对着后台叫道“导演,我这可是因公牺牲,不给个烈士称号也得报销”,后台一个臭鸡蛋飞出,“叫什么叫,我这个月的粮饷还不知哪里去领呢,都什么时代了,你不下岗都不错了……”)
李某看着刚刚好不容易从台湾拖回来的“铝条.ZIP”