代码改变世界

SSRS 2008 ReportServerTempDB增长异常分析

2016-04-20 23:59  潇湘隐者  阅读(2559)  评论(0编辑  收藏  举报

    这两天收到一SQL 2008 R2数据库服务器的磁盘空间告警,在检查过程中发现ReportServerTempDB已经暴增到60多GB,其中数据文件接近60G,日志文件9G大小左右。如下截图所示

clipboard

我们知道ReportServerTempDB是SSRS使用的临时数据库。这个数据库负责存储中间处理结果,例如报表服务器生成的会话和执行数据、缓存报表以及工作表。正常情况下,Report Server能够周期性地清除ReportServerTempDB中的到期的和孤立的数据。后台进程定期清理时间间隔由参数CleanupCycleMinutes控制,这个参数位于

<Installation Drive>\<Program Files or Program Files(x86)>\Microsoft SQL Server\<SSRS Instance>\Reporting Services\ReportServer 下的rsreportserver.config配置文件中。 例如C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\rsreportserver.config ,它指定多少分钟后从报表服务器数据库删除旧会话和过期快照。有效值的范围为 0 到最大整数之间。默认值为 10。如果将值设置为 0,将禁止数据库清除进程。如下所示,此参数值为10分钟

clipboard[1]

也就是说,如果正常清理ReportServerTempDB的话,ReportServerTempDB应该不会有这么大。检查数据库ReportServerTempDB,发现最大的表是SessionData,有50多G大小。

CREATE TABLE #tablespaceinfo
    (
      nameinfo VARCHAR(500) ,
      rowsinfo BIGINT ,
      reserved VARCHAR(20) ,
      datainfo VARCHAR(20) ,
      index_size VARCHAR(20) ,
      unused VARCHAR(20)
    )  
 
DECLARE @tablename VARCHAR(255);  
 
DECLARE Info_cursor CURSOR
FOR
    SELECT  '[' + [name] + ']'
    FROM    sys.tables
    WHERE   type = 'U'
 
OPEN Info_cursor  
FETCH NEXT FROM Info_cursor INTO @tablename  
 
WHILE @@FETCH_STATUS = 0
    BEGIN 
        INSERT  INTO #tablespaceinfo
                EXEC sp_spaceused @tablename  
        FETCH NEXT FROM Info_cursor  
    INTO @tablename  
    END 
 
CLOSE Info_cursor  
DEALLOCATE Info_cursor  
 
--创建临时表
CREATE TABLE [#tmptb]
    (
      TableName VARCHAR(50) ,
      DataInfo BIGINT ,
      RowsInfo BIGINT ,
      Spaceperrow  AS ( CASE RowsInfo
                         WHEN 0 THEN 0
                         ELSE CAST(DataInfo AS decimal(18,2))/CAST(RowsInfo AS decimal(18,2))
                       END ) PERSISTED
    )
 
--插入数据到临时表
INSERT  INTO [#tmptb]
        ( [TableName] ,
          [DataInfo] ,
          [RowsInfo]
        )
        SELECT  [nameinfo] ,
                CAST(REPLACE([datainfo], 'KB', '') AS BIGINT) AS 'datainfo' ,
                [rowsinfo]
        FROM    #tablespaceinfo
        ORDER BY CAST(REPLACE(reserved, 'KB', '') AS INT) DESC  
 
 
--汇总记录
SELECT  [tbspinfo].* ,
        [tmptb].[Spaceperrow] AS '每行记录大概占用空间(KB)'
FROM    [#tablespaceinfo] AS tbspinfo ,
        [#tmptb] AS tmptb
WHERE   [tbspinfo].[nameinfo] = [tmptb].[TableName]
ORDER BY CAST(REPLACE([tbspinfo].[reserved], 'KB', '') AS INT) DESC  
 
DROP TABLE [#tablespaceinfo]
DROP TABLE [#tmptb]

clipboard[2]

检查C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\LogFiles 下的日志文件,搜索“Call to CleanBatch”,会看到clean session都是0,部分如下所示

library!WindowsService_0!df8!04/20/2016-12:38:44:: i INFO: Call to CleanBatch()

library!WindowsService_0!df8!04/20/2016-12:38:45:: i INFO: Cleaned 0 batch records, 0 policies, 0 sessions, 0 cache entries, 49 snapshots, 289 chunks, 0 running jobs, 0 persisted streams, 244 segments, 244 segment mappings, 0 edit sessions.

library!WindowsService_0!df8!04/20/2016-12:38:45:: i INFO: Call to CleanBatch() ends

不清楚为什么出现这种情况,在网上也能看到很多关于reportservertempdb 不能清理历史数据或快照的帖子,如下所示,

http://www.sqlservercentral.com/Forums/Topic1183933-1550-1.aspx

ReportServerTempDB not cleaning itself up in SSRS 2008

ReportserverTempDB Grows unexpected

可以判断SSRS清理历史数据或快照的后台进程出现异常或存在bug(The ReportServerTempDB sessiondata table is not being purged according to the 10 minute default setting),但是具体情况,没有相关文档或资料佐证。所以仅仅从上面日志,我们还不能分析出具体原因。我倒是很想知道这个数据库ReportServerTempDB是什么时候出现暴增的,幸亏我在这台服务器部署了一个作业监控数据库文件增长情况,如下所示

可以看出这个数据库在2016-1-1号,只有22G大小(已经运行了一两年了),此后的几个月,几乎每个月增长了10G左右。

clipboard[3]

clipboard[4]

查看表SessionData的记录,发现居然还有2015年就已经过期的会话数据,更加深信这个是SSRS的一些bug造成的。

USE ReportServerTempDB;
 
GO
 
SELECT MIN(Expiration) FROM SessionData WITH(NOLOCK)

clipboard[5]


那么如何处理这个案例呢,我们可以在业务非常少的时间段,按照下面步骤进行操作

1: 首先停止SSRS服务

2: 删除SessionDate表的数据

USE ReportServerTempDB;
GO
 
TRUNCATE TABLE dbo.SessionData;

3: 然后启动SSRS服务

4: 收缩ReportServerTempDB数据库

    如果磁盘空间足够的情况下,就不要收缩ReportServerTempDB数据库了。如果磁盘空间实在紧张,那么收缩也是必须的。clipboard[6]

 

 

如果还存在dbo.SessionData不断增长的情况,最好创建做一个作业,每天定期清理那些过期的会话信息。另外还有一个问题,很多人会有疑惑:ReportServerTempDB数据库里面的表能否清理? 答案是可以,具体参考官方文档https://technet.microsoft.com/en-us/library/ms156016.aspx

If you back up the temporary database and subsequently restore it, you should delete the contents. Generally, it is safe to delete the contents of the temporary database at any time. However, you must restart the Report Server Windows service after you delete the contents.