Post Pacific  

Go Back   Post Pacific > Восстановленный архив > Темы без сохранившегося раздела

Notices

 
 
Thread Tools Search this Thread Display Modes
Old 15.06.2005, 20:18   #1
Shawnmb
Архивный участник
 
Join Date: Feb 2005
Posts: 307
Default SQL Optimization

I know it's unfinished, but I just realized that this post never left it's original posting location at RageZone...

First off, Check your database recovery models.
Full and Bulk-logging eat drive read/write and space like nothing else. Move to a Simple recovery model.

[Database] -> Properties -> Options



Second, Strip all unnessacary databases from the current SQL server besides the system and L2 databases (Aseren, I believe, mentioned this in a post of his own.) It helps a bit.

If possible, use a seperate 10k+ RPM drive to house the database MDF and LDF files (if you really want to go overkill seperate the LDF and MDF files onto seperate drives, but it's really not nessacary)

Every time you reboot your L2 server, I know it's tedious but it really helps. Do the following. While all other server applications are down, open CacheD's config file and tell it to clear all items on startup, then load it. Wait until it completes, shut it down, then change the clear setting to 0 in the ini.

Proceed to then run the folllowing code in the Query Analyzer:

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
Overview of the script:

Use targets a specific database.
The "DBCC Shrinkfile (lin2world_log, 1)" line is a DBCC call that serves to clear log files. Lin2world inside the brackets should be the physical file name of the log file without the extension. The 1 tells it to shrink it to 1 MB, which is the min filesize for a log. Doing this you don't have the unnessacary risks involved with detaching the database and manually deleting the logs (let alone the fact that you save a ton of time).

Run a defrag on your drives. It helps, a lot. Once a month or so I would also recommend manually shrinking the Lin2World database . [Database] -> All Tasks -> Shrink Database. This will cause a probably increase in pending write the first time you boot the thing, but don't worry about it. So far from what I've tested it helps.

Another thing you could do that solves Dead-Lock and Super-Lag problems for some people would be to change the max time CacheD will wait on an SQL query to finish. You can find this in the configuration file as
SQLExecLimit. I personally have mine at 12000.

I'll append a bit more to this later, got home and figured I'd write something useful for once.

Small Edit:
I figured I'd stick these online for anyone that finds them useful. Some are really basic but they simplify those tedious tasks that take forever to do in the Enterprise Manager.
Basic SQL Directory

Another Small Edit:
Limit the size of your transaction logs.

Newer Edit:
Another thing you can do to keep performance at peak is to reindex or defragment your database.

Defragment Database: (Online Job)
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 Database: (Offline Job)
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
Sources for DBINDEX and DBDEFRAG:
MSDN - DBCC INDEXDEFRAG
MSDN - DBCC DBREINDEX
Aaron Weiker's Blog
MSDN - INDEX Creation
Shawnmb is offline  
Old 15.06.2005, 21:05   #2
Sauron
Архивный участник
 
Join Date: Feb 2005
Posts: 357
Default

reindex cannot take more than 10min (depend on the server and db size). If than happends check hdd activity.
for example:
xp2500+ 2gb ram ata 40gb world db 1400mb - reindex -> 4-5min
amd64 3200 4gbram sata 80gb same db -> 3min

Dont set all the ram to the sql when reindexing, coz sql will use all the memory and ur os will frozen
Sauron is offline  
Old 15.06.2005, 21:55   #3
Shawnmb
Архивный участник
 
Join Date: Feb 2005
Posts: 307
Default

It really depends on the state of your database as well, if you've never run either of these I wouldn't be amazed if they took over 30 minutes a piece for a decently sized DB.
Shawnmb is offline  
Old 15.06.2005, 22:33   #4
Sauron
Архивный участник
 
Join Date: Feb 2005
Posts: 357
Default

i did my first reindex after 6 month lol :P and took me 3min like i said b4

btw there is an error in the reindex code.

where it say QUOTENAME(TABLE_NAME) +

should be

QUOTENAME(TABLE_NAME)
Sauron is offline  
Old 15.06.2005, 22:34   #5
Shawnmb
Архивный участник
 
Join Date: Feb 2005
Posts: 307
Default

Yeah me too, took quite a long time but it's well worth it
Shawnmb is offline  
Old 04.07.2006, 15:15   #6
Osito2Dancer
Архивный участник
 
Join Date: Mar 2005
Posts: 442
Default

Bump, this is useful info!.

Oso
Osito2Dancer is offline  
Old 03.11.2006, 17:42   #7
wton
Архивный участник
 
Join Date: May 2005
Posts: 463
Default

this is c4 retail db or c1 db?

thanks for your job ^^
wton is offline  
Old 03.11.2006, 19:05   #8
Drag
Архивный участник
 
Join Date: Mar 2006
Posts: 241
Default

O_O Lol... No matter... It will work on both
Drag is offline  
Old 03.11.2006, 22:24   #9
Shawnmb
Архивный участник
 
Join Date: Feb 2005
Posts: 307
Default

Works for stuff outside of L2 as well. ^^
Shawnmb is offline  
Old 04.11.2006, 04:24   #10
drugolo
Архивный участник
 
Join Date: Jun 2005
Posts: 93
Default

purge + reindexing (reindex from cypher)

strongly suggested a backup BEFORE run it...


Code:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


CREATE PROCEDURE [dbo].[lin_PurgeOldDataAndReindex]	
AS
BEGIN
SET NOCOUNT ON

-- first of all, let's put as -3 the account_id of the old unused chars (last login 1 march 2006)
UPDATE user_data SET account_id = -3
WHERE login < '2006-03-01 12:00:00' -- HERE IS THE PART WHERE YOU CAN MOD, ONLY CHANGE THE DATE.


-- clean up some variables in user_data (we will not remove the chars, anyway)
UPDATE user_data SET pledge_id=0, nickname='', subjob1_class=-1, subjob2_class=-1, subjob3_class=-1, ST_hair=0, ST_right_ear=0, ST_left_ear=0, ST_neck=0, ST_right_finger=0, ST_left_finger=0, ST_head=0, ST_right_hand=0, ST_left_hand=0, ST_gloves=0, ST_chest=0, ST_legs=0, ST_feet=0, ST_back=0, ST_both_hand=0 WHERE account_id IN (-1, -3)  

-- delete all entries in the tables about deleted chars (all of them, even old deleted ones)
DELETE FROM event_items WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM nobless_achievements WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM char_pet WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_blocklist WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3)) OR block_char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))  
DELETE FROM pledge WHERE ruler_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))  
DELETE FROM olympiad_result WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM olympiad_match WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3)) OR rival_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM quest WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM shortcut_data WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM ssq_user_data WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_ban WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_blocklist WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_deleted WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_friend WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3)) OR friend_char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))  
DELETE FROM user_henna WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_history WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_item WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3)) AND warehouse BETWEEN 0 AND 1
DELETE FROM user_journal WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_log WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_macroinfo WHERE user_macroinfo.macro_id IN (select distinct user_macro.macro_id from user_macro join user_macroinfo on (user_macro.macro_id = user_macroinfo.macro_id) where user_macro.char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3)))
DELETE FROM user_macro WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_newbie WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_nobless WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_punish WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_recipe WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_skill WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_sociality WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_subjob WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_surrender WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM bookmark WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))
DELETE FROM user_comment WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))

-- fix tables for missing data
UPDATE user_data SET pledge_id = 0 WHERE pledge_id <> 0 AND pledge_id NOT IN (SELECT pledge_id FROM pledge)  
UPDATE user_data SET nickname = '' WHERE pledge_id = 0
UPDATE agit SET pledge_id = 0 WHERE pledge_id <> 0 AND pledge_id NOT IN (SELECT pledge_id FROM pledge)  
UPDATE castle SET pledge_id = 0 WHERE pledge_id <> 0 AND pledge_id NOT IN (SELECT pledge_id FROM pledge)  
DELETE FROM pledge_crest WHERE crest_id NOT IN (SELECT crest_id FROM pledge)  
DELETE FROM war_declare WHERE (challengee <> 0 AND challengee NOT IN (SELECT pledge_id FROM pledge)) OR (challenger <> 0 AND challenger NOT IN (SELECT pledge_id FROM pledge))  
DELETE FROM siege_agit_pledge WHERE pledge_id <> 0 AND pledge_id NOT IN (SELECT pledge_id FROM pledge)  
DELETE FROM team_battle_agit_pledge WHERE pledge_id <> 0 AND pledge_id NOT IN (SELECT pledge_id FROM pledge)  
DELETE FROM alliance WHERE master_pledge_id NOT IN (SELECT pledge_id FROM pledge)  
UPDATE pledge SET alliance_id = 0 WHERE alliance_id <> 0 AND alliance_id NOT IN (SELECT id FROM alliance)  

-- this one is commented out because is optional (for me i prefer keep the charnames/accountnames stored)
-- DELETE FROM user_data WHERE char_id IN (SELECT char_id FROM user_data WHERE account_id IN (-1, -3))

DECLARE @TableName varchar(255)

DECLARE TableCursor CURSOR FOR
SELECT table_name FROM information_schema.tables
WHERE table_type = 'base table'

OPEN TableCursor

FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN 
DBCC DBREINDEX(@TableName,' ',90)
FETCH NEXT FROM TableCursor INTO @TableName
END

CLOSE TableCursor

DEALLOCATE TableCursor
END
drugolo is offline  
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 21:09.