web-dev-qa-db-fra.com

Comment déterminer la taille de ma base de données SQL Server?

De base: quelle est la taille sur le disque de ma base de données MS SQL Server?
Pour en savoir plus: puis-je voir rapidement où se trouvent les données? c'est-à-dire quels tableaux, journaux, etc.

29
adambox

Vous voudrez probablement commencer par la commande sp_spaceused.

Par exemple:

sp_spaceused Renvoie des informations sur la taille totale de la base de données

sp_spaceused 'MyTable' Renvoie des informations sur la taille de MyTable

Lisez les documents pour tout ce dont vous pouvez obtenir des informations. Vous pouvez également utiliser la commande sp_msforeachtable pour exécuter sp_spaceused sur toutes les tables à la fois.

Modifier: sachez que la commande renvoie parfois plusieurs ensembles de données, chaque ensemble contenant un bloc différent de statistiques.

35
David

La manière la plus simple (pas de saisie!): Dans SQL 2005/8 de Management Studio, cliquez avec le bouton droit sur la base de données, sélectionnez Rapports, Rapports standard, Utilisation du disque (également Par tables, table et partition).

16
user3047

Ce script parcourt toutes les tables de la base de données actuelle et montre combien d'espace chacune prend pour les données, les index et l'espace inutilisé:

http://sqlserverpedia.com/wiki/Calculate_Current_Table_Sizes

4
Brent Ozar

Vous pouvez voir les fichiers physiques dans sys.database_files. Cela a le chemin d'accès au fichier et la taille (dans les blocs IIRC).

sp_spaceused vous montrera combien d'espace un objet individuel prend.

exécutez ceci pour obtenir la taille par table:

/******************************************************************************
**    File: “GetTableSpaceUsage.sql”
**    Name: Get Table Space Useage for a specific schema
**    Auth: Robert C. Cain
**    Date: 01/27/2008
**
**    Desc: Calls the sp_spaceused proc for each table in a schema and returns
**        the Table Name, Number of Rows, and space used for each table.
**
**    Called by:
**     n/a – As needed
**
**    Input Parameters:
**     In the code check the value of @schemaname, if you need it for a
**     schema other than dbo be sure to change it.
**
**    Output Parameters:
**     NA
*******************************************************************************/

/*—————————————————————————*/
/* Drop the temp table if it's there from a previous run                     */
/*—————————————————————————*/
if object_id(N'tempdb..[#TableSizes]') is not null
  drop table #TableSizes ;
go

/*—————————————————————————*/
/* Create the temp table                                                     */
/*—————————————————————————*/
create table #TableSizes
  (
    [Table Name] nvarchar(128)   /* Name of the table */
  , [Number of Rows] char(11)    /* Number of rows existing in the table. */
  , [Reserved Space] varchar(18) /* Reserved space for table. */
  , [Data Space] varchar(18)    /* Amount of space used by data in table. */
  , [Index Size] varchar(18)    /* Amount of space used by indexes in table. */
  , [Unused Space] varchar(18)   /* Amount of space reserved but not used. */
  ) ;
go

/*—————————————————————————*/
/* Load the temp table                                                        */
/*—————————————————————————*/
declare @schemaname varchar(256) ;
-- Make sure to set next line to the Schema name you want!
set @schemaname = 'dbo' ;

-- Create a cursor to cycle through the names of each table in the schema
declare curSchemaTable cursor
  for select sys.schemas.name + '.' + sys.objects.name
      from    sys.objects
            , sys.schemas
      where   object_id > 100
              and sys.schemas.name = @schemaname
              /* For a specific table uncomment next line and supply name */
              --and sys.objects.name = 'specific-table-name-here'    
              and type_desc = 'USER_TABLE'
              and sys.objects.schema_id = sys.schemas.schema_id ;

open curSchemaTable ;
declare @name varchar(256) ;  /* This holds the name of the current table*/

-- Now loop thru the cursor, calling the sp_spaceused for each table
fetch curSchemaTable into @name ;
while ( @@FETCH_STATUS = 0 )
  begin    
    insert into #TableSizes
            exec sp_spaceused @objname = @name ;       
    fetch curSchemaTable into @name ;   
  end

/* Important to both close and deallocate! */
close curSchemaTable ;     
deallocate curSchemaTable ;


/*—————————————————————————*/
/* Feed the results back                                                     */
/*—————————————————————————*/
select [Table Name]
      , [Number of Rows]
      , [Reserved Space]
      , [Data Space]
      , [Index Size]
      , [Unused Space]
from    [#TableSizes]
order by [Table Name] ;

/*—————————————————————————*/
/* Remove the temp table                                                     */
/*—————————————————————————*/
drop table #TableSizes ;

pris de Robert Caine blog

Ce code est pour Microsoft SQL 2005+

1
balexandre

Il s'agit d'une requête/vue qui obtient toutes ces informations, et bien plus, sans curseurs ou boucles "maléfiques". ;-)

    /*
    vwTableInfo - Table Information View

 This view display space and storage information for every table in a
SQL Server 2005 database.
Columns are:
    Schema
    Name
    Owner       may be different from Schema)
    Columns     count of the max number of columns ever used)
    HasClusIdx  1 if table has a clustered index, 0 otherwise
    RowCount
    IndexKB     space used by the table's indexes
    DataKB      space used by the table's data

 16-March-2008, [email protected]
 31-January-2009, Edited for better formatting
*/
--CREATE VIEW vwTableInfo
-- AS

    SELECT SCHEMA_NAME(tbl.schema_id) as [Schema]
    , tbl.Name
    , Coalesce((Select pr.name 
            From sys.database_principals pr 
            Where pr.principal_id = tbl.principal_id)
        , SCHEMA_NAME(tbl.schema_id)) as [Owner]
    , tbl.max_column_id_used as [Columns]
    , CAST(CASE idx.index_id WHEN 1 THEN 1 ELSE 0 END AS bit) AS [HasClusIdx]
    , Coalesce( (Select sum (spart.rows) from sys.partitions spart 
        Where spart.object_id = tbl.object_id and spart.index_id < 2), 0) AS [RowCount]

    , Coalesce( (Select Cast(v.low/1024.0 as float) 
        * SUM(a.used_pages - CASE WHEN a.type <> 1 THEN a.used_pages WHEN p.index_id < 2 THEN a.data_pages ELSE 0 END) 
            FROM sys.indexes as i
             JOIN sys.partitions as p ON p.object_id = i.object_id and p.index_id = i.index_id
             JOIN sys.allocation_units as a ON a.container_id = p.partition_id
            Where i.object_id = tbl.object_id  )
        , 0.0) AS [IndexKB]

    , Coalesce( (Select Cast(v.low/1024.0 as float)
        * SUM(CASE WHEN a.type <> 1 THEN a.used_pages WHEN p.index_id < 2 THEN a.data_pages ELSE 0 END) 
            FROM sys.indexes as i
             JOIN sys.partitions as p ON p.object_id = i.object_id and p.index_id = i.index_id
             JOIN sys.allocation_units as a ON a.container_id = p.partition_id
            Where i.object_id = tbl.object_id)
        , 0.0) AS [DataKB]
    , tbl.create_date, tbl.modify_date

     FROM sys.tables AS tbl
      INNER JOIN sys.indexes AS idx ON (idx.object_id = tbl.object_id and idx.index_id < 2)
      INNER JOIN master.dbo.spt_values v ON (v.number=1 and v.type='E')

Prendre plaisir.

0
RBarryYoung

Exécutez Démarrer\Programmes\Microsoft SQL Server\Enterprise Manager. Ouvrez la feuille de base de données, dans la propriété% databasename%, vous pouvez voir l'emplacement des fichiers de données et des fichiers de transaction.

0
nerf