2009-03-26 3 views
4

SQL Server 버전 :/SP3 w SQL Server 2005 및 2008어떻게 테이블 저장 정보를 얻을 수

거기에 내장 된 다음과 같은 정보를 검색합니다 SQL Server 저장 프로 시저? 또는 DMV (동적 관리 뷰)도 좋을 것입니다.

나는 주로 FILEGROUP 테이블의 데이터를 찾는 방법에 관심이 있습니다. 하지만 다음 결과를 모두 반환하는 sproc가 있으면 더 좋을 것입니다. 그런데

alt text

는 데이터 SQL 서버 UI 표시를 검색하는 방법의 일대일 매칭을 표시하는 문서가 있는가?

답변

5

시스템 저장 프로 시저 sp_help가 좋은 시작 지점이 될 수 있습니다. 예를 들어

:이 트릭 할 수

exec sp_help 'schema.TableName' 
+0

그게 전부 야! 필자는 테이블에서 "sp_help"를 실행하는 것에 대해 생각해 본 적이 없습니다 ... FILEGROUP을 표시합니다. "Data_located_on_FileGroup"이라고 이름이 지정되었습니다. – Sung

+0

멋진 정보, 기꺼이 도와 드리겠습니다. –

1

파일 그룹에 대해 DBCC showfilestats 또는 sp_spaceused를 살펴보십시오.

blog에 스크립트가 있습니다. 테이블과 크기를 나열합니다. 보다 사용자 친화적 인 (관리보기에서는 DB에서 오른쪽 마우스를 사용하여 보고서를 생성 할 수 있습니다).

+0

미안, 그에만 DB를 보여줍니다. – Sascha

+0

스크립트를 찾았습니다 @ http://www.keyboardface.com/archives/2007/06/12/mssql-table-size-for-all-tables/ – Sascha

+0

@Sascha : 저는 FILEGROUP을 찾는데 더 많은 관심이 있습니다. 파일 크기 ... – Sung

2

이렇게하면 선 (善)의 모든 종류를 표시합니다 :

-- Script to analyze table space usage using the 
-- output from the sp_spaceused stored procedure 
-- Works with SQL 7.0, 2000, and 2005 

set nocount on 

print 'Show Size, Space Used, Unused Space, Type, and Name of all database files' 

select 
    [FileSizeMB] = 
     convert(numeric(10,2),sum(round(a.size/128.,2))), 
     [UsedSpaceMB] = 
     convert(numeric(10,2),sum(round(fileproperty(a.name,'SpaceUsed')/128.,2))) , 
     [UnusedSpaceMB] = 
     convert(numeric(10,2),sum(round((a.size-fileproperty(a.name,'SpaceUsed'))/128.,2))) , 
    [Type] = 
     case when a.groupid is null then '' when a.groupid = 0 then 'Log' else 'Data' end, 
    [DBFileName] = isnull(a.name,'*** Total for all files ***') 
from 
    sysfiles a 
group by 
    groupid, 
    a.name 
    with rollup 
having 
    a.groupid is null or 
    a.name is not null 
order by 
    case when a.groupid is null then 99 when a.groupid = 0 then 0 else 1 end, 
    a.groupid, 
    case when a.name is null then 99 else 0 end, 
    a.name 




create table #TABLE_SPACE_WORK 
(
    TABLE_NAME sysname  not null , 
    TABLE_ROWS numeric(18,0) not null , 
    RESERVED varchar(50)  not null , 
    DATA  varchar(50)  not null , 
    INDEX_SIZE varchar(50)  not null , 
    UNUSED  varchar(50)  not null , 
) 

create table #TABLE_SPACE_USED 
(
    Seq  int  not null  
    identity(1,1) primary key clustered, 
    TABLE_NAME sysname  not null , 
    TABLE_ROWS numeric(18,0) not null , 
    RESERVED varchar(50)  not null , 
    DATA  varchar(50)  not null , 
    INDEX_SIZE varchar(50)  not null , 
    UNUSED  varchar(50)  not null , 
) 

create table #TABLE_SPACE 
(
    Seq  int  not null 
    identity(1,1) primary key clustered, 
    TABLE_NAME SYSNAME  not null , 
    TABLE_ROWS int  not null , 
    RESERVED int  not null , 
    DATA  int  not null , 
    INDEX_SIZE int  not null , 
    UNUSED  int  not null , 
    USED_MB    numeric(18,4) not null, 
    USED_GB    numeric(18,4) not null, 
    AVERAGE_BYTES_PER_ROW  numeric(18,5) null, 
    AVERAGE_DATA_BYTES_PER_ROW numeric(18,5) null, 
    AVERAGE_INDEX_BYTES_PER_ROW numeric(18,5) null, 
    AVERAGE_UNUSED_BYTES_PER_ROW numeric(18,5) null, 
) 

declare @fetch_status int 

declare @proc varchar(200) 
select @proc = rtrim(db_name())+'.dbo.sp_spaceused' 

declare Cur_Cursor cursor local 
for 
select 
    TABLE_NAME = 
    rtrim(TABLE_SCHEMA)+'.'+rtrim(TABLE_NAME) 
from 
    INFORMATION_SCHEMA.TABLES 
where 
    TABLE_TYPE = 'BASE TABLE' 
order by 
    1 

open Cur_Cursor 

declare @TABLE_NAME  varchar(200) 

select @fetch_status = 0 

while @fetch_status = 0 
    begin 

    fetch next from Cur_Cursor 
    into 
     @TABLE_NAME 

    select @fetch_status = @@fetch_status 

    if @fetch_status <> 0 
     begin 
     continue 
     end 

    truncate table #TABLE_SPACE_WORK 

    insert into #TABLE_SPACE_WORK 
     (
     TABLE_NAME, 
     TABLE_ROWS, 
     RESERVED, 
     DATA, 
     INDEX_SIZE, 
     UNUSED 
     ) 
    exec @proc @objname = 
     @TABLE_NAME ,@updateusage = 'true' 


    -- Needed to work with SQL 7 
    update #TABLE_SPACE_WORK 
    set 
     TABLE_NAME = @TABLE_NAME 

    insert into #TABLE_SPACE_USED 
     (
     TABLE_NAME, 
     TABLE_ROWS, 
     RESERVED, 
     DATA, 
     INDEX_SIZE, 
     UNUSED 
     ) 
    select 
     TABLE_NAME, 
     TABLE_ROWS, 
     RESERVED, 
     DATA, 
     INDEX_SIZE, 
     UNUSED 
    from 
     #TABLE_SPACE_WORK 

    end  --While end 

close Cur_Cursor 

deallocate Cur_Cursor 

insert into #TABLE_SPACE 
    (
    TABLE_NAME, 
    TABLE_ROWS, 
    RESERVED, 
    DATA, 
    INDEX_SIZE, 
    UNUSED, 
    USED_MB, 
    USED_GB, 
    AVERAGE_BYTES_PER_ROW, 
    AVERAGE_DATA_BYTES_PER_ROW, 
    AVERAGE_INDEX_BYTES_PER_ROW, 
    AVERAGE_UNUSED_BYTES_PER_ROW 

    ) 
select 
    TABLE_NAME, 
    TABLE_ROWS, 
    RESERVED, 
    DATA, 
    INDEX_SIZE, 
    UNUSED, 
    USED_MB   = 
     round(convert(numeric(25,10),RESERVED)/ 
     convert(numeric(25,10),1024),4), 
    USED_GB   = 
     round(convert(numeric(25,10),RESERVED)/ 
     convert(numeric(25,10),1024*1024),4), 
    AVERAGE_BYTES_PER_ROW = 
     case 
     when TABLE_ROWS <> 0 
     then round(
     (1024.000000*convert(numeric(25,10),RESERVED))/ 
     convert(numeric(25,10),TABLE_ROWS),5) 
     else null 
     end, 
    AVERAGE_DATA_BYTES_PER_ROW = 
     case 
     when TABLE_ROWS <> 0 
     then round(
     (1024.000000*convert(numeric(25,10),DATA))/ 
     convert(numeric(25,10),TABLE_ROWS),5) 
     else null 
     end, 
    AVERAGE_INDEX_BYTES_PER_ROW = 
     case 
     when TABLE_ROWS <> 0 
     then round(
     (1024.000000*convert(numeric(25,10),INDEX_SIZE))/ 
     convert(numeric(25,10),TABLE_ROWS),5) 
     else null 
     end, 
    AVERAGE_UNUSED_BYTES_PER_ROW = 
     case 
     when TABLE_ROWS <> 0 
     then round(
     (1024.000000*convert(numeric(25,10),UNUSED))/ 
     convert(numeric(25,10),TABLE_ROWS),5) 
     else null 
     end 
from 
    (
    select 
     TABLE_NAME, 
     TABLE_ROWS, 
     RESERVED = 
     convert(int,rtrim(replace(RESERVED,'KB',''))), 
     DATA  = 
     convert(int,rtrim(replace(DATA,'KB',''))), 
     INDEX_SIZE = 
     convert(int,rtrim(replace(INDEX_SIZE,'KB',''))), 
     UNUSED  = 
     convert(int,rtrim(replace(UNUSED,'KB',''))) 
    from 
     #TABLE_SPACE_USED aa 
    ) a 
order by 
    TABLE_NAME 

print 'Show results in descending order by size in MB' 

select * from #TABLE_SPACE order by USED_MB desc 
go 

drop table #TABLE_SPACE_WORK 
drop table #TABLE_SPACE_USED 
drop table #TABLE_SPACE 
+0

@ck : 죄송합니다.이 스크립트는 FILEGROUP 정보를 제공하지 않습니다. – Sung

2

해결책을 발견.

테이블 FILEGROUP 정보를 찾기 위해 UI를 사용하는 것보다이 시간을 길게 입력하는 것처럼 보입니다.

List tables in filegroups을 찾았

declare @objectid bigint 
set @objectid = object_id('table_name') 
exec sp_objectfilegroup @objectid 

내가 대신 테이블 이름을 사용 다른 저장 프로 시저를 만들기 때문에 결국 그 세 줄을 입력 너무 게으른되었다.

create procedure spTableFileGroup 
    @TableName sysname 
as 
begin 
    if exists( select 1 
       from INFORMATION_SCHEMA.TABLES T 
       where T.TABLE_NAME = @TableName) begin 
     declare @objectid bigint 
     set @objectid = object_id(@TableName) 
     exec sp_objectfilegroup @objectid 
    end 
    else begin 
     print 'There is no table named "' + @TableName + '"' 
    end 
end 
GO 

사용

exec spTableFileGroup 'table_name' 
GO 
1

는 인덱스를 클러스터 년대 FILEGROUP가 결정된다 테이블. 당신은 파일 그룹을 찾으려면이 쿼리를 사용할 수 있습니다

SELECT * 
FROM 
sys.tables AS tbl 
INNER JOIN sys.indexes AS idx ON idx.object_id = tbl.object_id and idx.index_id < 2 
LEFT OUTER JOIN sys.data_spaces AS dsidx ON dsidx.data_space_id = idx.data_space_id 

을 관련 두 번째 질문에, 내가 어떤 문서가 있다고 생각하지 않습니다, 당신은 SSMS의 세부 정보를 볼 때, 당신은 SQL 프로파일 러를 사용할 수 있습니다. 그러면 정확한 검색어가 표시됩니다.

1

->

use Your_database_name 
GO 

SELECT FG.*, MF.* FROM sys.filegroups FG INNER JOIN sys.master_files 
MF on MF.data_space_id = FG.data_space_id WHERE database_id = db_id() 
관련 문제