金蝶K3 CLOUD像许多ERP系统一下,运行一段时间后,其数据库文件会不断增大。当然,随着时间的推移,数据量的增加是一方面,更多是用户操作带来的日志文件及诸多的临时表数据。数据库文件的不断增加,不仅占据了大量的硬盘空间,同时也对系统的运行速度、效率方面带来了影响。
于是,笔者也上网搜索过一些解决方案,然而效果并不理想。后来在金蝶论坛里看到类似官方出的一个贴子,给了优化的SQL代码。后来,经过测试,确实有效,现整理一下,分享给大家,以供不时之需。
首先,查询临时表大小,决定是否需要清理临时表数据。
select cast(sum(a.total_pages)*8/1024 as varchar) + 'MB' total from sys.partitions p join sys.allocation_units a on p.partition_id = a.container_id
join sys.tables it on p.object_id = it.object_id
where it.name like 'tmp%'
select it.name,cast(sum(a.total_pages)*8 as varchar) + 'KB' total from sys.partitions p join sys.allocation_units a on p.partition_id = a.container_id
join sys.tables it on p.object_id = it.object_id
where it.name like 'tmp%'
group by it.name
order by sum(a.total_pages)*8 desc
根据临时表大小 ,决定是否需要删除临时表,删除操作如下:
delete from T_BAS_TEMPORARYTABLENAME where FPROCESSTYPE=1 or FCREATEDATE<GETDATE()-1
if object_id('temptb','table')>0 drop table temptb;
declare @sql varchar(max)
declare @icount int
declare @I int
set @sql='drop table '
set @i=1
select name,IDENTITY(int,1,1) id into temptb from sys.tables t where name like 'tmp%'
and not exists(select 1 from T_BAS_TEMPORARYTABLENAME where FTABLENAME=t.name) and create_date<=DATEADD(n,-5, GETDATE())
select @icount=@@ROWCOUNT
while @i<@icount
begin
select @sql=@sql+name+',' from temptb where id between @i and @i+49
if @@ROWCOUNT>0
set @sql=substring(@sql,1,len(@sql)-1)+';'
set @i=@i+50
exec(@sql)
set @sql='drop table '
end
if object_id('temptb','table')>0 drop table temptb;
注:以上SQL语句来源于金蝶论坛搜集,以K3CLOUD 5.0数据库实测可用!
除了清除临时表外,还可以对数据库索引进行优化,但此法慎用,数据库性能没有受到较大影响时,不建议使用。大家可以在有条件的情况下去尝试一下。以下代码来源于网络。
USE [msdb]
GO
/****** Object: Job [reindex] Script Date: 2016/9/7 23:26:04 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [Database Maintenance] Script Date: 2016/9/7 23:26:04 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Database Maintenance' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Database Maintenance'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'reindex',
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'索引优化维护计划',
@category_name=N'Database Maintenance',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [one] Script Date: 2016/9/7 23:26:05 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'one',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'declare @sql varchar(max)
set @sql=''''
select @sql=@sql+''dbcc dbreindex(''+name+'')''+char(13) from sys.tables where name not like ''z%'' and name not like ''tmp%''
if len(@sql)>0
exec(@sql)',
@database_name=N'YDZY2015',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'one',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=1,
@freq_subday_interval=0,
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20150531,
@active_end_date=99991231,
@active_start_time=30000,
@active_end_time=235959,
@schedule_uid=N'fd5038a8-5b15-43a5-af3e-ad1e7d058088'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO