SQL Server DBA 일상 검사 상용 SQL

1.데이터베이스
데이터 파일

--  
exec sp_helpdb 

 
--  
select name, 
       user_access_desc,           --  
       state_desc,                 --  
       recovery_model_desc,        --  
       page_verify_option_desc,    --  
       log_reuse_wait_desc         --  
from sys.databases 

 
-- : , , ,  
use test 
go 

exec sp_spaceused   
go 

 

-- @updateusage = 'true', dbcc updateusage 
exec sp_spaceused  @updateusage = 'true' 

 
-- ,  
DBCC UPDATEUSAGE('test') 
3,로그 파일

--  
sp_helpfile  

 

-- 、 、 、  
select db_name(database_id) dbname, 
       type_desc,      --  
       name,           --  
       physical_name,  --  
       state_desc,     --  
       size * 8.0/1024 as ' (MB)'         
from sys.master_files 

 

-- extent , , , TotalExtents*64/1024, MB 
-- tempdb ,  
dbcc showfilestats 
4,데이터 파일,로그 파일 의 I/O 통계 정보

-- 、 、 、  
select db_name(database_id) dbname, 
       type_desc,      --  
       name,           --  
       physical_name,  --  
       state_desc,     --  
       size * 8.0/1024 as ' (MB)'         
from sys.master_files 
where type_desc = 'LOG' 

 

-- ,  
dbcc sqlperf(logspace) 
5,대상,표,색인,색인 보기 등

-- I/O ,  
    select database_id, 
           file_id, 
           file_handle,           --windows  
           sample_ms,             --  

           num_of_reads, 
           num_of_bytes_read, 
           io_stall_read_ms,      --  

           num_of_writes, 
           num_of_bytes_written, 
           io_stall_write_ms, 

           io_stall,              -- I/O  
           size_on_disk_bytes     --          

    from sys.dm_io_virtual_file_stats(db_id('test'),   -- id 
                                       1 )  -- id                                        
    union all 

    select database_id, 
           file_id, 
           file_handle,           --windows  
           sample_ms,             --  

           num_of_reads, 
           num_of_bytes_read, 
           io_stall_read_ms,      --  

           num_of_writes, 
           num_of_bytes_written, 
           io_stall_write_ms, 

           io_stall,              -- I/O  
           size_on_disk_bytes     --   
    from sys.dm_io_virtual_file_stats( db_id('test'),   -- id 
                                       2 )  -- id 
6,tempdb 데이터베이스

-- : , , , ,  
exec sp_spaceused @objname ='temp_lock' 

 
-- :  
exec sp_spaceused @objname ='temp_lock', 
                  @updateusage ='true' 

 

-- , ,  
/*====================================================== 
 

sys.dm_db_partition_stats ( )  
========================================================*/    
select o.name, 
       sum(p.reserved_page_count) as reserved_page_count, -- ,  

       sum(p.used_page_count) as used_page_count,         -- ,  

       sum(case when p.index_id <2  
                     then p.in_row_data_page_count +  
                          p.lob_used_page_count +  
                          p.row_overflow_used_page_count 
                else p.lob_used_page_count +  
                     p.row_overflow_used_page_count 
           end) as data_pages,  -- , 、 lob 、  

       sum(case when p.index_id < 2  
                     then p.row_count 
                else 0 
           end) as row_counts   -- , ,  

from sys.dm_db_partition_stats p 
inner join sys.objects o 
        on p.object_id = o.object_id  
where p.object_id= object_id(' ') 
group by o.name    

  

-- , ,           
--  
-- : 、 ( )、                 
dbcc showcontig(temp_lock) 

 

--SQL Server ,  
select * 
from sys.dm_db_index_physical_stats( 
        db_id('test'),                      -- id 
        object_id('test.dbo.temp_lock'),    -- id 
        null,                               -- id 
        null,                               --  

        'limited'   --default,null,'limited','sampled','detailed', 'limited' 
                    --'limited' , , ,  
                    --'sampled' 、 1% , 1000 , 'detailed' 'sampled' 
                    --'detailed' ,  
    ) 

 

--  
use test 
go 

if OBJECT_ID('extentinfo') is not null 
    drop table extentinfo 
go 

create table extentinfo  
(   [file_id] smallint,  
    page_id int,  
    pg_alloc int,                
    ext_size int,                 
    obj_id int,                   

    index_id int,                 
    partition_number int, 
    partition_id bigint, 
    iam_chain_type varchar(50),   
    pfs_bytes varbinary(10)  
)  
go  

 
/*==================================================================== 
,  

:  DBCC EXTENTINFO(dbname,tablename,indexid) 

DBCC EXTENTINFO('[test]','extentinfo',0) 
======================================================================*/ 
insert extentinfo  
exec('dbcc extentinfo(''test'') ') 
go 

 
--  
select  file_id,  
        obj_id,               -- ID 
        index_id,             -- id 

        page_id,              -- ,  
        pg_alloc,             --  

        ext_size,             --  

        partition_number, 
        partition_id, 
        iam_chain_type,       --IAM : , ,  
        pfs_bytes  
from extentinfo 
order by file_id, 
         OBJ_ID, 
         index_id, 
         partition_id, 
         ext_size 

 
/*===================================================================================================== 
, , , 


1.  

2. pg_alloc ext_size , , 
  , , 
  ( ), 2 ( 2 ), 
  2 1. 
========================================================================================================*/ 
select file_id, 
       obj_id,  
       index_id,  
       partition_id,  
       ext_size,  

       count(*) as ' ',  
       sum(pg_alloc) as ' ',  

       ceiling(sum(pg_alloc) * 1.0 / ext_size) as ' ',  
       ceiling(sum(pg_alloc) * 1.0 / ext_size) / count(*) * 100.00 as ' / '  

from extentinfo  
group by file_id, 
         obj_id,  
         index_id, 
         partition_id,  
         ext_size  
having ceiling(sum(pg_alloc)*1.0/ext_size) < count(*)    
-- : < , 100%  
order by partition_id, obj_id, index_id, [file_id]      

좋은 웹페이지 즐겨찾기