Table space used

Configurare noua (How To)

Situatie

Mai jos este o procedura ce returneaza pentru fiecare tabela spatiul utilizat de pe disc, mai exact retuneaza:

numarul de linii

spatiul rezervat de pe disc

spatiul efectiv utilizat de catre date

dimeniunea indexilor

spatiul neutilizat dar rezervat

Solutie

alter procedure Table_spaceused_prc
@objname nvarchar(776) = null,
@updateusage varchar(5) = false

as

declare @id int
declare @type character(2)
declare @pages int
declare @dbname sysname
declare @dbsize dec(15,0)
declare @logsize dec(15)
declare @bytesperpage dec(15,0)
declare @pagesperMB dec(15,0)

create table #spt_space
(
[id][int] IDENTITY (1, 1) NOT NULL ,
tblname varchar(200),
rows int null,
reserved dec(15) null,
data dec(15) null,
indexp dec(15) null,
unused dec(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
declare
@strTblName varchar(200),
@intID int

DECLARE cursor_ CURSOR FOR
select [name] from sysobjects where xtype=’U’ and [name] not like ‘dtprop%’
OPEN cursor_
FETCH NEXT FROM cursor_ INTO @strTblName
WHILE (@@FETCH_STATUS<>-1)
BEGIN
select @id = id, @type = xtype
from sysobjects
where id = object_id(@strTblName)

insert into #spt_space (reserved,tblname)
select sum(reserved),@strTblName
from sysindexes
where indid in (0, 1, 255)
and id = @id
select @intID = max([id]) from #spt_space

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 where [id]=@intID

update #spt_space
set indexp = (select sum(used)
from sysindexes
where indid in (0, 1, 255)
and id = @id)
– data
where [id]=@intID
update #spt_space
set unused = reserved
– (select sum(used)
from sysindexes
where indid in (0, 1, 255)
and id = @id)
where [id]=@intID
update #spt_space
set rows = i.rows
from sysindexes i
where i.indid < 2
and i.id = @id
and #spt_space.[id]=@intID

FETCH NEXT FROM cursor_ INTO @strTblName
END
CLOSE cursor_
DEALLOCATE cursor_
select tblname,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’
union
select ‘_Total_’,rows = convert(char(11), sum(rows)),
reserved = ltrim(str(sum(reserved) * d.low / 1024.,15,0) +
‘ ‘ + ‘KB’),
data = ltrim(str(sum(data) * d.low / 1024.,15,0) +
‘ ‘ + ‘KB’),
index_size = ltrim(str(sum(indexp) * d.low / 1024.,15,0) +
‘ ‘ + ‘KB’),
unused = ltrim(str(sum(unused) * d.low / 1024.,15,0) +
‘ ‘ + ‘KB’)
from #spt_space, master.dbo.spt_values d
where d.number = 1
and d.type = ‘E’
group by d.low
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

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’
end

Tip solutie

Permanent
Etichetare:

Voteaza

(18 din 38 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?