Professional Documents
Culture Documents
Microsoft SQL
SQL Server
Server
使用經驗分享與交流
使用經驗分享與交流
By Richard
2007/03/26
Table of Contents
• SQL Server 操作
– 如何替 Database 瘦身
– Enterprise Manager vs. Query Analyzer
– CSV 到 SQL Server table
– SQL Server = Task Manager?
• SQL Server Function
– 善用 SQL Server date function
– 這是不是 NULL? 數字 ? 日期 ?
– 百變金剛 ? 看我 72 變…
– Truncate table and Drop table
– Drop & create index
Table of Contents
• SQL & Store Procedure Tuning
– 如何建立 Store Procedure 的 script?
– 我的極速快感與永遠的痛 : Index
– 使用 For loop update 資料
– 借我放一下…
– Store Procedure = function?
• SQL 2005 data encrypt/decrypt
– 我的萬用鑰匙 (Key and Certificate)
– Data Encrypt & Decrypt
• 建立一個可以 rollback 前一天資料的 batch
SQL Server 操作
• 如何替 Database 瘦身 :
– DB 的 mdf 與 ldf
– DB 的復原模型
– 壓縮資料庫
XXXXXX … XXXXX
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SQL & Store Procedure Tuning
• 我的極速快感與永遠的痛 : Index
– 何時該 create index?
• 大量資料存取
– 何時該 drop index?
• 大量資料寫入
– index 要如何建 ?
• SELECT, UPDATE 的條件
• Eg: select * from users where user_id=‘abc’
create index ix_users on users(‘user_id’)
SQL & Store Procedure Tuning
• 使用 For loop update 資料
DECLARE @UTYPE_ID varchar(50);
DECLARE @USER_ID varchar(50);
OPEN CURSOR_I
WHILE 1 = 1 BEGIN
FETCH NEXT FROM CURSOR_I INTO @UTYPE_ID, @USER_ID;
if @@fetch_status <> 0
break
IF @UTYPE_ID = 'AO'
UPDATE USERS SET AO_TYPE='RM' WHERE USER_ID=@USER_ID
CLOSE CURSOR_I
SQL & Store Procedure Tuning
• 借我放一下…
DECLARE @UTYPE_ID varchar(50);
DECLARE @USER_ID varchar(50);
SELECT * INTO USERS_TMP FROM USERS ( 要檢查 USERS_TMP 是否存在 )
DECLARE CURSOR_I CURSOR LOCAL
FOR SELECT UTYPE_ID, USER_ID FROM USERS_TMP
OPEN CURSOR_I
WHILE 1 = 1 BEGIN
FETCH NEXT FROM CURSOR_I INTO @UTYPE_ID, @USER_ID;
if @@fetch_status <> 0
break
IF @UTYPE_ID = 'AO'
UPDATE USERS SET AO_TYPE='RM' WHERE USER_ID=@USER_ID
USE rmadmin
OPEN SYMMETRIC KEY RMWKey DECRYPTION BY CERTIFICATE RMWCert WITH PASS
WORD = 'RMW1234'
RETURN(@dec_content)
END;
GO
建立一個可以 rollback 前一天資料
的 batch
• 將相關 batch 影響的 tables, copy 一份變成 xx
x_batch 之 tables.
• 將 download import, 針對 xxx_batch 之 tables
做 batch.
• 如果 batch 執行成功 : 將原非 xxx_batch 之 ta
bles 備份成 xxx_lastday, 將 xxx_batch 改名
成原 xxx tables.
• 如果 batch 執行失敗 : 不做任何動作 .
• 如果 batch 執行成功 , 但 data failure: 則可將 x
xx_lastday 之 tables, 還原成原 xxx 之 tables.
建立一個可以 rollback 前一天資料
的 batch
Batch Start
copy
backup succeed
copy
Yes
Data Succeed End
correct?
recover No