如何查看SQL server中各表占用空间的大小,并排列输出?
发布网友
发布时间:2022-04-27 04:45
我来回答
共3个回答
热心网友
时间:2022-04-08 03:27
创建存储过程:
CREATE PROCEDURE [dbo].[sys_viewTableSpace]
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE [dbo].#tableinfo(
表名 [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
记录数 [int] 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,
未用空间 [varchar](50) COLLATE Chinese_PRC_CI_AS NULL
)
insert into #tableinfo(表名, 记录数, 预留空间, 使用空间, 索引占用空间, 未用空间)
exec sp_MSforeachtable "exec sp_spaceused '?'"
select * from #tableinfo
order by 记录数 desc
drop table #tableinfo
END
使用的时候直接 :exec sys_viewtablespace
热心网友
时间:2022-04-08 04:45
通过增加数据文件。
alter tablespace 表空间名称 add datafile '数据文件名称' SIZE 200m AUTOEXTEND ON MAXSIZE 1500M;
热心网友
时间:2022-04-08 06:19
use hstrades
go
if object_id('hsrun.dbo.ta') is not null
drop table hsrun.dbo.ta
create table hsrun.dbo.ta
(
name varchar(255),
rows int,
reserved varchar(255),
data varchar(255),
index_size varchar(255),
usused varchar(255)
)
truncate table hsrun.dbo.ta
declare @table_name varchar(255)
declare cur_table cursor for
select name from sysobjects where type = 'U'
open cur_table
fetch next from cur_table into @table_name
while @@fetch_status = 0
begin
fetch next from cur_table into @table_name
exec ('
insert into hsrun.dbo.ta
exec sp_spaceused ' + @table_name)
end
close cur_table
deallocate cur_table
--select 'truncate table ' + name ,* from hsrun.dbo.ta
--order by cast(reserved as int) desc
update hsrun.dbo.ta
set reserved = replace(reserved,'KB','')
select * from hsrun.dbo.ta
order by cast (reserved as int) desc
--select sum(cast (reserved as int)) from hsrun.dbo.ta
/*
backup log hsrun with no_log
dbcc shrinkfile('hsrun_log')
select max(oc_date) from hshis.dbo.hisasset
*/