Wyświetlanie wielkości bazy danych wraz z informacją o wolnym miejscu w bazie w MS SQL

Wyświetlanie wielkości bazy danych wraz z informacją o wolnym miejscu w bazie w MS SQL

Trafił mi się task, aby zrobić audyt wielkości plików baz MS SQL-owych oraz ilością wolnego miejsca w każdym z nich. Niby temat łatwy przy ilości 5-10 baz i jednym serwerze, ale co w przypadku gdy:

  • baz jest 100
  • serwerów jest np. 20?

Posiedziałem i podrapałem się w głowę i okazało się to bardzo proste do osiągnięcia za pomocą wbudowanej procedury sp_spaceused, która została dodana w w MS SQL 2012.

sp_spaceused is a system stored procedure that reveals the amount of space used. This procedure can take many different parameters as input in order to show the amount of space on disk that is used. If your system can afford it, it is best to update the usage stats before finding the space used. This ensures accurate results from sp_spaceused.

Displays the number of rows, disk space reserved, and disk space used by a table, indexed view, or Service Broker queue in the current database, or displays the disk space reserved and used by the whole database. >>więcej informacji<<

Permission to execute sp_spaceused is granted to the Public role. Only members of the db_owner fixed database role can specify the @updateusage parameter.

In SQL Server 2016, the @openresultset parameter made our life much simpler. By specifying the parameter, the result can be shown in one or more result sets.

 

Całość polecenia wygląda tak…

DECLARE @command varchar(8000)

SELECT @command = 'IF ”?” NOT IN(”master”, ”model”, ”msdb”, ”tempdb”) BEGIN USE ?

EXEC sp_spaceused @oneresultset = 1

END’

EXEC sp_MSforeachdb @command

 

Zawartość można wrzucać do wybranej tabeli i analizować pod względem rozrostu baz.

 

Wyświetlanie wielkości bazy danych wraz z informacją o wolnym miejscu w bazie w MS SQL

About the Author: admin

Leave A Reply

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *

Witryna wykorzystuje Akismet, aby ograniczyć spam. Dowiedz się więcej jak przetwarzane są dane komentarzy.