Use to query the database, table, and disks The transaction log for database 'tempdb' is full due to 'ACTIVE_TRANSACTION'

Query uses the database, table, and disks under the summary.

1, as can be seen the first column is the name of a disk, the second column is the remaining disk space, units of M

exec sys.xp_fixeddrives

2, using the system SP: exec sys.sp_spaceused

If you do not take back any object, the entire database is returned

exec sys.sp_spaceused

 

Note: database_size all database files size + size of the log file, the UNALLOCATED Space refers to the spatial database has not been used, if this space is large, you can consider compacting the database, of course, database_size- the UNALLOCATED Space = space has been used

 

Followed by the table

exec sys.sp_spaceused Table_2

the Data + = Reserved index_size + unused, if unused space is large, there may be a lot of table data is migrated away, space is not released, if you care about this part of the disk space, you might consider compressing this table

 3, for information DB queries, you can use:

EXEC SP_HELPDB 'Test' 

expand:

1, shrink (shrink) database files

use target_database_name
go

select file_id,
    type,
    type_desc,
    data_space_id,
    name,
    size*8/1024/1024 as size_gb,
    growth,
    is_percent_growth,
    physical_name,
    max_size
from sys.database_files

dbcc shrinkfile('file logcial name',0,notruncate)
dbcc shrinkfile('file logcial name',target_size_mb,truncateonly)

 

2,对数据库中的 table 和 index 压缩存储
2.1, 查看数据库中,占用存储空间非常大的table;

use target_database_name
go

select 
    t.name,
    sum(case when ps.index_id<2 then ps.row_count else 0 end) as row_count,
    sum(ps.reserved_page_count)*8/1024/1024 as reserved_gb,
    sum(ps.used_page_count)*8/1024/1024 as used_gb,
    sum( case when ps.index_id<2
                    then ps.in_row_data_page_count+ps.lob_used_page_count+ps.row_overflow_used_page_count
              else 0 end
        )*8/1024/1024 as data_used_gb,
    sum(case when ps.index_id>=2 
                then ps.in_row_data_page_count+ps.lob_used_page_count+ps.row_overflow_used_page_count
             else 0 end
        )*8/1024/1024 as index_used_gb
from sys.dm_db_partition_stats ps
inner join sys.tables t
    on ps.object_id=t.object_id
group by t.object_id, t.name
order by used_gb desc

 

2.2, 查看table及其Index是否被压缩过

select p.partition_id,object_name(p.object_id) as ObjectName,
    p.index_id,
    p.rows,
    p.data_compression,
    p.data_compression_desc,
    au.Type,
    au.Type_desc,
    au.total_pages,
    au.used_pages,
    au.data_pages
from sys.partitions p
inner join sys.allocation_units au
    on p.partition_id=au.container_id 
where p.object_id=object_id('[dbo].[table_name]',N'U')

2.3,估计压缩能够节省的存储空间

exec sys.sp_estimate_data_compression_savings 
                @schema_name='dbo',
                @object_name='table_name',
                @index_id=1,
                @partition_number=null,
                @data_compression ='page'

 

 2.4, 对table及其index进行数据压缩
对table 及其index 进行 rebuild,SQL Server将重新分配存储空间,慎重:rebuild 反而会增加数据库占用的存储空间。在数据压缩存储之后,必须shrink 数据库文件,才能释放数据库所占用的存储空间,增加Disk的Free Space。

alter table [dbo].table_name
rebuild with(data_compression=page)

alter index index_name
on [dbo].table_name
rebuild with(data_compression=page)

 

 3,增加事务日志文件

 参考:《The transaction log for database 'tempdb' is full due to 'ACTIVE_TRANSACTION'

Guess you like

Origin www.cnblogs.com/ziqiumeng/p/10967116.html