随着数据库的日积月累,而且经历的开发人员是一茬又一茬,风格迥异,导致现在数据库里面的对象混乱,为了顺应IT整合的趋势,提高系统和数据库的可维护性和易扩张性,决定将系统、数据库以及服务器的整合进
行到底,现在只是个开始。
下面是刚写的一个脚本,抓取表、存储过程、触发器和视图等对象的信息并汇总,后续对该部分数据进行提取分析,把影响效能的大数据表进行定期的备份和归档,对长期不再使用的对象,如表、存储等进行备份后删
除;同时对数据库进行一些策略上的设定,以限定开发规范,让数据库焕然一新。
1 USE [msdb] 2 GO 3 /****** Object: StoredProcedure [dbo].[sp_DBMaintain_incrementData_statistic] Script Date: 09/14/2012 18:10:56 ******/ 4 SET ANSI_NULLS ON 5 GO 6 SET QUOTED_IDENTIFIER ON 7 GO 8 -- ========================================================================== 9 -- Author: xxxx 10 -- Create date: 2012/09/14 11 -- Description: 统计用户表、存储过程、函数、试图和触发器等对象变化、汇总 12 -- =========================================================================== 13 ALTER procedure [dbo].[sp_DBMaintain_incrementData_statistic] 14 as 15 SET NOCOUNT on 16 17 BEGIN TRY 18 declare @EXCEPTION VARCHAR(MAX) 19 declare @MailSubject NVARCHAR(255) 20 21 EXEC sp_MSforeachdb @command1= 'INSERT INTO msdb.dbo.Audit_Statistic 22 SELECT DISTINCT ''CQ-SDS01'' AS ServerName,''?''AS DatabaseName,o.name AS ObjectName, o.id as ObjectID, a.type_desc AS ObjectType,i.rowcnt AS [RowCount],a.create_date AS CreateDate,a.modify_date AS ModiftDate,GETDATE() AS RecordDate 23 FROM [?].dbo.sysobjects o 24 INNER JOIN [?].dbo.sysindexes i ON i.id=o.id AND i.indid IN(0,1) AND o.xtype IN (''U'') AND o.name <> ''sysdiagrams'' 25 INNER JOIN [?].sys.all_objects a ON o.id=a.OBJECT_ID AND o.name=a.name 26 ORDER BY i.rowcnt DESC' 27 28 EXEC sp_MSforeachdb @command1= 'INSERT INTO msdb.dbo.Audit_Statistic 29 SELECT DISTINCT ''CQ-SDS01'' AS ServerName,''?'' AS DatabaseName,o.name AS ObjectName, o.id as ObjectID, a.type_desc AS ObjectType,0 AS [RowCount],a.create_date AS CreateDate,a.modify_date AS ModiftDate,GETDATE() AS RecordDate 30 FROM [?].dbo.sysobjects o 31 INNER JOIN [?].sys.all_objects a ON o.id=a.OBJECT_ID AND o.name=a.name AND o.xtype IN (''p'',''fn'',''v'',''tr'') 32 ORDER BY a.modify_date DESC' 33 34 EXEC sp_MSforeachdb @command1= 'INSERT INTO msdb.dbo.Audit_Summary 35 SELECT ServerName, DatabaseName, ObjectType,COUNT(ObjectType) AS ObjectCount,SUM([RowCount]) AS TotalCount,GETDATE() AS RecordDate FROM ( 36 SELECT ''CQ-SDS01'' AS ServerName,''?'' AS DatabaseName,o.name AS ObjectName, a.type_desc AS ObjectType,ISNULL(i.rowcnt,0) AS [RowCount] 37 FROM [?].dbo.sysobjects o 38 LEFT JOIN [?].dbo.sysindexes i ON i.id=o.id AND i.indid IN(0,1) 39 INNER JOIN [?].sys.all_objects a ON o.id=a.OBJECT_ID AND o.name=a.name AND o.xtype IN (''p'',''fn'',''v'',''tr'',''u'') )A 40 GROUP BY ServerName,DatabaseName,ObjectType' 41 42 END TRY 43 BEGIN CATCH 44 SET @EXCEPTION = ERROR_MESSAGE() 45 END CATCH 46 47 IF @EXCEPTION<>'' 48 BEGIN 49 50 SET @MailSubject='DBMaintain:Database statistic failed from ' + @@SERVERNAME 51 EXEC msdb.dbo.sp_send_dbmail 52 @profile_name = 'mail', 53 @recipients = '[email protected]', 54 @body = @EXCEPTION, 55 @subject = @MailSubject 56 57 END 58