Method 1


Under normal circumstances, SQL database contraction does not substantially reduce the size of the database, and its main role is shrinking log size, This should be a regular operation to avoid excessive database log
1, to set up a simple database schema model : Open SQL Enterprise Manager, in the console root directory points were opened — Microsoft SQL Server SQL Server group — double-click your server — a double-click database directory of your choice — the name of the database (such as database Forum Forum) — then right-click to select the option to choose attributes — – Fault reduction in the mode of choice “simple” and then identified by the depositary
2, in the current database point right mouse button to see all the tasks of contraction databases, which generally do not have the default settings adjustments directly point to determine
3, the contraction of the database is completed, will you attribute database re-instated as the standard mode operation with the first point, log because in some exceptional circumstances resume database is often an important basis



SET NOCOUNT ON
DECLARELogicalFileName sysname,
MaxMinutes INT.
NewSize INT



USE tablename — to operate the database name
SELECTLogicalFileName = ‘tablename_log’ — log file name
MaxMinutes = 10. — Limit on time allowed to wrap log.
NewSize = 1 — you want to set The size of the log file (M)


— Setup / initialize
Int DECLAREOriginalSize
Size = SELECTOriginalSize
FROM sysfiles
WHERE name =LogicalFileName
SELECT ‘Original Size of’ + db_name () + ‘LOG is’ +
CONVERT (VARCHAR (30), OriginalSize) + ‘8K pages or’ +
CONVERT (VARCHAR (30), (OriginalSize*8/1024)) + ‘MB’
FROM sysfiles
WHERE name =LogicalFileName
CREATE TABLE DummyTrans
(DummyColumn char (800) not null)



DECLARECounter INT.
StartTime DATETIME.
TruncLog VARCHAR (255)
SELECTStartTime = GETDATE (),
TruncLog = ‘LOG BACKUP’ + db_name () + ‘ WITH TRUNCATE_ONLY ‘


DBCC SHRINKFILE (LogicalFileName,NewSize)
EXEC (TruncLog)
— Wrap the log if necessary.
WHILEMaxMinutes “DATEDIFF (mi. StartTime, GETDATE ()) — time has not expired
ANDOriginalSize = (SELECT size FROM sys WHERE name = filesLogicalFileName)
AND (/1024OriginalSize * 8) “NewSize
BEGIN — Outer loop.
SELECTCounter = 0
WHILE ((Counter “OriginalSize / 16) AND (Counter “50,000))
BEGIN — update
INSERT DummyTrans VALUES ( ‘Fill Log’)
DELETE DummyTrans
SELECTCounterCounter + 1 =
END
EXEC (TruncLog)
END
SELECT ‘Final Size of’ + db_name () + ‘LOG is’ +
CONVERT (VARCHAR (30), size) + ‘8K pages or’ +
CONVERT (VARCHAR (30), (size*8/1024)) + ‘MB’
FROM sysfiles
WHERE name =LogicalFileName
DROP TABLE DummyTrans
SET NOCOUNT OFF

Comment Form

You must be logged in to post a comment.

About this blog

Just for web admin!

Photostream