創(chuàng)建存儲(chǔ)過程:
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供禹州網(wǎng)站建設(shè)、禹州做網(wǎng)站、禹州網(wǎng)站設(shè)計(jì)、禹州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、禹州企業(yè)網(wǎng)站模板建站服務(wù),10年禹州做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
CREATE PROCEDURE [dbo].[sys_viewTableSpace]
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE [dbo].#tableinfo(
表名 [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
記錄數(shù) [int] NULL,
預(yù)留空間 [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
使用空間 [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
索引占用空間 [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
未用空間 [varchar](50) COLLATE Chinese_PRC_CI_AS NULL
)
insert into #tableinfo(表名, 記錄數(shù), 預(yù)留空間, 使用空間, 索引占用空間, 未用空間)
exec sp_MSforeachtable "exec sp_spaceused '?'"
select * from #tableinfo
order by 記錄數(shù) desc
drop table #tableinfo
END
使用的時(shí)候直接 :exec sys_viewtablespace
表的大小一般指表的行數(shù),占用硬盤空間大小就比較復(fù)雜了,可以有以下幾個(gè)參數(shù):
分配空間,數(shù)據(jù)占用空間及索引占用空間。
執(zhí)行
EXEC alltablecount
即可顯示當(dāng)前數(shù)據(jù)庫所有表資源占用情況。
執(zhí)行前,請(qǐng)前建立以下表,和兩個(gè)存儲(chǔ)過程:
1、先建立表:HY_SPACE
CREATE TABLE [HY_SPACE] (
[name] [nvarchar] (128) COLLATE Chinese_PRC_CI_AS NULL ,
[rows] [char] (11) COLLATE Chinese_PRC_CI_AS NULL ,
[reserved] [varchar] (18) COLLATE Chinese_PRC_CI_AS NULL ,
[data] [varchar] (18) COLLATE Chinese_PRC_CI_AS NULL ,
[index_size] [varchar] (18) COLLATE Chinese_PRC_CI_AS NULL ,
[unused] [varchar] (18) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
2、建立統(tǒng)計(jì)存儲(chǔ)過程:
create procedure HYspaceused
@objname nvarchar(776) = null,
@updateusage varchar(5) = false
as
declare @idint
declare @typecharacter(2)
declare@pagesint
declare @dbname sysname
declare @dbsize dec(15,0)
declare @logsize dec(15)
declare @bytesperpagedec(15,0)
declare @pagesperMBdec(15,0)
create table #spt_space
(
rowsint null,
reserveddec(15) null,
datadec(15) null,
indexpdec(15) null,
unuseddec(15) null
)
if @updateusage is not null
begin
select @updateusage=lower(@updateusage)
if @updateusage not in ('true','false')
begin
raiserror(15143,-1,-1,@updateusage)
return(1)
end
end
if @objname IS NOT NULL
begin
select @dbname = parsename(@objname, 3)
if @dbname is not null and @dbname db_name()
begin
raiserror(15250,-1,-1)
return (1)
end
if @dbname is null
select @dbname = db_name()
select @id = null
select @id = id, @type = xtype
from sysobjects
where id = object_id(@objname)
if @id is null
begin
raiserror(15009,-1,-1,@objname,@dbname)
return (1)
end
if not exists (select * from sysindexes
where @id = id and indid 2)
if @type in ('P ','D ','R ','TR','C ','RF')
begin
raiserror(15234,-1,-1)
return (1)
end
else if @type = 'V '
begin
raiserror(15235,-1,-1)
return (1)
end
else if @type in ('PK','UQ')
begin
raiserror(15064,-1,-1)
return (1)
end
else if @type = 'F '
begin
raiserror(15275,-1,-1)
return (1)
end
end
if @updateusage = 'true'
begin
if @objname is null
dbcc updateusage(0) with no_infomsgs
else
dbcc updateusage(0,@objname) with no_infomsgs
print ' '
end
set nocount on
if @id is null
begin
select @dbsize = sum(convert(dec(15),size))
from dbo.sysfiles
where (status 64 = 0)
select @logsize = sum(convert(dec(15),size))
from dbo.sysfiles
where (status 64 0)
select @bytesperpage = low
from master.dbo.spt_values
where number = 1
and type = 'E'
select @pagesperMB = 1048576 / @bytesperpage
select database_name = db_name(),
database_size =
ltrim(str((@dbsize + @logsize) / @pagesperMB,15,2) + ' MB'),
'unallocated space' =
ltrim(str((@dbsize -
(select sum(convert(dec(15),reserved))
from sysindexes
where indid in (0, 1, 255)
)) / @pagesperMB,15,2)+ ' MB')
print ' '
insert into #spt_space (reserved)
select sum(convert(dec(15),reserved))
from sysindexes
where indid in (0, 1, 255)
select @pages = sum(convert(dec(15),dpages))
from sysindexes
where indid 2
select @pages = @pages + isnull(sum(convert(dec(15),used)), 0)
from sysindexes
where indid = 255
update #spt_space
set data = @pages
update #spt_space
set indexp = (select sum(convert(dec(15),used))
from sysindexes
where indid in (0, 1, 255))
- data
update #spt_space
set unused = reserved
- (select sum(convert(dec(15),used))
from sysindexes
where indid in (0, 1, 255))
select reserved = ltrim(str(reserved * d.low / 1024.,15,0) +
' ' + 'KB'),
data = ltrim(str(data * d.low / 1024.,15,0) +
' ' + 'KB'),
index_size = ltrim(str(indexp * d.low / 1024.,15,0) +
' ' + 'KB'),
unused = ltrim(str(unused * d.low / 1024.,15,0) +
' ' + 'KB')
from #spt_space, master.dbo.spt_values d
where d.number = 1
and d.type = 'E'
end
else
begin
insert into #spt_space (reserved)
select sum(reserved)
from sysindexes
where indid in (0, 1, 255)
and id = @id
select @pages = sum(dpages)
from sysindexes
where indid 2
and id = @id
select @pages = @pages + isnull(sum(used), 0)
from sysindexes
where indid = 255
and id = @id
update #spt_space
set data = @pages
update #spt_space
set indexp = (select sum(used)
from sysindexes
where indid in (0, 1, 255)
and id = @id)
- data
update #spt_space
set unused = reserved
- (select sum(used)
from sysindexes
where indid in (0, 1, 255)
and id = @id)
update #spt_space
set rows = i.rows
from sysindexes i
where i.indid 2
and i.id = @id
INSERT INTO HY_SPACE
select name = object_name(@id),
rows = convert(char(11), rows),
reserved = ltrim(str(reserved * d.low / 1024.,15,0) +
' ' + 'KB'),
data = ltrim(str(data * d.low / 1024.,15,0) +
' ' + 'KB'),
index_size = ltrim(str(indexp * d.low / 1024.,15,0) +
' ' + 'KB'),
unused = ltrim(str(unused * d.low / 1024.,15,0) +
' ' + 'KB')
from #spt_space, master.dbo.spt_values d
where d.number = 1
and d.type = 'E'
RETURN
end
return (0)
GO
3、主存儲(chǔ)過程
create procedure alltablecount as
declare @name varchar(100)
TRUNCATE TABLE HY_SPACE
declare tablecur cursor for select name from sysobjects where xtype= 'u '
create table #tablecount(tablename varchar(100),reccount int)
open tablecur
fetch next from tablecur into @name
while @@fetch_status!=-1
begin
exec ('insert into #tablecount select tablename='''+@name+''',reccount=count(1) from '+@name+' ')
EXEC HYspaceused @NAME
fetch next from tablecur into @name
end
close tablecur
deallocate tablecur
select * from #tablecount order by reccount desc
select * from HY_SPACE
GO
--讀取庫中的所有表名
select name from sysobjects where xtype='u'
--讀取指定表的所有列名
select name from syscolumns where id=(select max(id) from sysobjects where xtype='u' and name='表名')
獲取數(shù)據(jù)庫表名和字段
sqlserver中各個(gè)系統(tǒng)表的作用
sysaltfiles 主數(shù)據(jù)庫 保存數(shù)據(jù)庫的文件
syscharsets 主數(shù)據(jù)庫 字符集與排序順序
sysconfigures 主數(shù)據(jù)庫 配置選項(xiàng)
syscurconfigs 主數(shù)據(jù)庫 當(dāng)前配置選項(xiàng)
sysdatabases 主數(shù)據(jù)庫 服務(wù)器中的數(shù)據(jù)庫
syslanguages 主數(shù)據(jù)庫 語言
syslogins 主數(shù)據(jù)庫 登陸帳號(hào)信息
sysoledbusers 主數(shù)據(jù)庫 鏈接服務(wù)器登陸信息
sysprocesses 主數(shù)據(jù)庫 進(jìn)程
sysremotelogins主數(shù)據(jù)庫 遠(yuǎn)程登錄帳號(hào)
syscolumns 每個(gè)數(shù)據(jù)庫 列
sysconstrains 每個(gè)數(shù)據(jù)庫 限制
sysfilegroups 每個(gè)數(shù)據(jù)庫 文件組
sysfiles 每個(gè)數(shù)據(jù)庫 文件
sysforeignkeys 每個(gè)數(shù)據(jù)庫 外部關(guān)鍵字
sysindexs 每個(gè)數(shù)據(jù)庫 索引
sysmenbers 每個(gè)數(shù)據(jù)庫 角色成員
sysobjects 每個(gè)數(shù)據(jù)庫 所有數(shù)據(jù)庫對(duì)象
syspermissions 每個(gè)數(shù)據(jù)庫 權(quán)限
systypes 每個(gè)數(shù)據(jù)庫 用戶定義數(shù)據(jù)類型
select 列名=name from syscolumns where id=object_id(N'要查的表名')
select tabname as '表名',rowsNum as '表數(shù)據(jù)行數(shù)',reserved as '保留大小',data as '數(shù)據(jù)大小',index_size as '索引大小',unused_size as '未使用大小'
from #tabName
--where tabName not like 't%'
order by cast(rowsNum as int) desc
新聞標(biāo)題:sqlserver表大小,sql查看表的數(shù)據(jù)大小
轉(zhuǎn)載源于:http://aaarwkj.com/article38/dsspopp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、域名注冊(cè)、虛擬主機(jī)、外貿(mào)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)