the script i have written to verify the space utilization and give necessary action to bring it down to
85%. i have considered auto growth, no drive space .
The script will give the necessary information about the space usage and the recommendation to bring the space utilization less than 85%, if the drive is full then , the recommendation is to increase the size of file to (drive space)-100 mb. Note if the auto growth is enabled , the recommendation is it increase the max size to 5% of the file increase(you change it if you want)
the link is
******************************************************************************************************************************************************************
if OBJECT_ID('#freesapce') is not null
begin
drop table #freesapce
end
/*creating table to check free space in the drive*/
create table #freesapce( drive char(10),val int)
insert into #freesapce exec xp_fixeddrives;
go
declare @dbname varchar(50)=(select DB_NAME())
select DB_NAME(mf.database_id) 'db_name',fp.drive,fp.val 'mb on drive'
, (size*8/1024) [occupied size],FILEPROPERTY(name,'spaceused')*8*1.0/1024 'free space',
FILEPROPERTY(name,'spaceused')*1.0*100/size [percent free],
left(round((FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/85,0),
CHARINDEX('.',(FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/85)-1) 'req',
mf.type_desc as 'file',
isnull(
/*condition to check if the space utilisation is more than 85%*/
case when
(FILEPROPERTY(name,'spaceused')*1.0*100)/size >85 then
/*condition to check if the space utilisation is more than 85% and if drive is not having sufficient space
to bring the space utiliszation to less than 85 %*/
case
when (fp.val)-(left(round((FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/85,0),
CHARINDEX('.',(FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/85)-1)-
FILEPROPERTY(name,'spaceused')*8*1.0/1024)<0
then
case when mf.growth=0 and mf.type_desc not like '%LOG%' and mf.type_desc like '%ROW%'
then
CHAR(13)+'/*contact windows*/ use master ; '+CHAR(13)+'alter database ['+DB_NAME(database_id)+'] modify file
( NAME = N'''+name+''',size='+ convert (nvarchar(20),
size*8/1024+fp.val-100)+' mb )'
when max_size<>-1 or growth=0
and type_desc not like '%LOG%' and type_desc like '%ROW%'
then
CHAR(13)+'/*contact windows*/ use master ; '+CHAR(13)+'alter database ['+DB_NAME(database_id)+'] modify file
( NAME = N'''+name+''',size='+ convert (nvarchar(20),
size*8/1024+fp.val-100)+' mb,
MAXSIZE ='+convert (nvarchar(20),
size*8/1024+fp.val-100)+' mb )'
end
else
/*condition to check if the space utilisation is more than 85% and bring it down to 85%*/
case when mf.growth=0 and mf.type_desc not like '%LOG%' and mf.type_desc like '%ROW%'
then
'/*action required*/ '+CHAR(13)+'use master ; '+CHAR(13)+'alter database ['+DB_NAME(database_id)+'] modify file
( NAME = N'''+name+''',size='+
convert(nvarchar(20),left(round((FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/84,0),
CHARINDEX('.',(FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/84)-1))+' mb )'
when max_size<>-1 or growth=0
and type_desc not like '%LOG%' and type_desc like '%ROW%'
then
/*condition to check if the space utilisation is more than 85% and bring it down to 85% and checks auto
growth is enabled*/
'/*action required*/ '+CHAR(13)+'use master ; '+CHAR(13)+'alter database ['+DB_NAME(database_id)+'] modify file
( NAME = N'''+name+''',size='+
convert(nvarchar(20),left(round((FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/84,0),
CHARINDEX('.',(FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/84)-1))+' mb,
MAXSIZE ='+
convert(nvarchar(20),left(round((FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/84,0),
CHARINDEX('.',(FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/84)-1)+
left(round((FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/84,0),
CHARINDEX('.',(FILEPROPERTY(name,'spaceused')*8*1.0/1024)*100/84)-1)*5/100)+'mb )'
end
end
else
'no action required' end,'auto growth unlimited') action
/*when sapce utilisation is more 85% unlimited file growth is enabled- you will
see in the action -'auto growth unlimited'*/
from #freesapce fp left join
sys.master_files mf on fp.drive=SUBSTRING(mf.physical_name,1,1)
where database_id=DB_ID(@dbname)
go
drop table #freesapce
go