View Single Post
Old 11.10.2006, 13:00   #14
Drag
Архивный участник
 
Join Date: Mar 2006
Posts: 241
Default Optimize Database

Credits to Shawnmb.

Defragment
Code:
DECLARE Tables CURSOR FOR
SELECT DISTINCT
    OBJECT_NAME([id]),
    [name]
FROM dbo.sysindexes
WHERE OBJECTPROPERTY([id], 'IsUserTable') = 1

OPEN Tables
DECLARE @database varchar(128)
DECLARE @table varchar(128)
DECLARE @index varchar(128)

SET @database = DB_NAME()

FETCH NEXT FROM Tables INTO @table, @index

WHILE @@FETCH_STATUS = 0
BEGIN
  PRINT 'Reindexing table ' +
        ISNULL(@table, '<NULL>') +
        ' on index ' + ISNULL(@index, '<NULL')

 IF @table IS NOT NULL AND @index IS NOT NULL
 BEGIN
  DBCC INDEXDEFRAG(@database, @table, @index)
 END
 FETCH NEXT FROM Tables INTO @table, @index
END

CLOSE Tables
DEALLOCATE Tables
Reindex
Code:
DECLARE Tables CURSOR FOR
SELECT DISTINCT
  QUOTENAME(TABLE_CATALOG) + '.' + 
    QUOTENAME(TABLE_SCHEMA) + '.' +
    QUOTENAME(TABLE_NAME)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'

OPEN Tables
DECLARE @table varchar(128)

FETCH NEXT FROM Tables INTO @table

WHILE @@FETCH_STATUS = 0
BEGIN
  PRINT 'Reindexing table ' + ISNULL(@table, '<NULL>')
  IF @table IS NOT NULL
    BEGIN
    DBCC DBREINDEX( @table)
  END
  FETCH NEXT FROM Tables INTO @table
END

CLOSE Tables
DEALLOCATE Tables
Next useful things:
- Tab database by right button, Shrink -> Database (lin2db, lin2world, all)
- Set in table preferences 'Simple recovery model'. Just search for it in options

Clear logs
Code:
Backup log lin2world with truncate_only
Go

Use lin2world
DBCC Shrinkfile (lin2world_log, 1)
Go

Backup log lin2db with truncate_only
Go

Use lin2db
DBCC Shrinkfile (lin2db_log, 1)
Go

Thats all about optimizing.

I just writed it in quick way, and removed ' + ' which cause error in reindex

Original Post: https://postpacific.mmorpg-game.onli...read.php?t=930
Drag is offline