I'm CaLendarW Blog

database

MsSQL – Identity Generator

by calendarw on Jul.07, 2009, under database, error handling

MsSQL 內既 Identity Generator, 雖然被發現當 Table 儲存超過一百萬行時, 由 SELECT @@Identity 或者 SELECT SCOPE_IDENTITY() 會存在 Return 值錯誤既問題, 但經過呢個幾月既開發都未出現問題.

係 MsSQL 入面, 新增 Record 時主要提取 Primary Key 既方法主要有 @@IDENTITY, SCOPE_IDENTY() 同 IDENT_CURRENT(‘table_name’) 三種:

@@IDENTITY
使用 @@IDENTITY 會 Return 當前 Session 任何 Table 最後生產的 Primary Key, 如果 Insert statement 運行後如果有任何 Trigger 中會 Insert 在其他 Table 的話, @@IDENTITY 就會變得不準確.

SCOPE_IDENTY()
使用 SCOPE_IDENTY() 會 Return 當前 Scope 內確實 Insert 左既 Primary Key, 就算有任何 Trigger 在背後運行都不會有任何影響, 係一個最好既選擇. 不過經由 MS Connect 入面既 Feedback 顯示, Table 被新增至一百萬行後, Return Value 有可以會籨 1 開始數起, 這問題由 MS 回應既答案中回答到已增加到它們的 Bug tracking database 中.

IDENT_CURRENT(‘table_name’)
而 IDENT_CURRENT(‘table_name’) 會 Return 指定 Table 最後生產出來的 Identity, 由於它不會理會任何 Scope 或者 Session, 當有多於一個 Scope 或者 Session 運行時便有機會出現不準確既問題.

因此, 在以上三個尋找 IDENTITY 既方法中, 並沒有一個能在任何環境下百分百準確既方法, 所以在 MsSQL 使用 Identity Generator 便會有潛在問題既可能性, 因此在 deploy 前應該對 Database 作詳細測試, 已確定當前既環境設定正確.

再者, Identity Generator 亦開始慢慢地被其他類型既方案取代, 例如 GUID, Hi/Lo or UUID 等等.

正如之前 CS != SE 文章內所講, Technology 其實永遠唔會有錯, 錯就只會錯在未能正確使用它們既人, 而軟件其中一個用途就係用來避免人們使用犯錯, 所以在程式篇寫時應該以不同既設定技巧及文檔來避免人們使用錯誤, 那軟件才容易正確地被使用!!

Leave a Comment : more...

Control of Duplication

by calendarw on May.22, 2009, under database, design

近期做緊 Application to Database 呢部份, 開始對 Analysis Patterns 一書中既 Knowledge Level 同 Operation Level 多左理解, 除此之外, 仲開始感受既點樣控制重覆!!

Duplication, 係 Program 入面係理應避免的, 正如見到重覆既 Code 就應該進行重構一樣, 但在 Database 層面上既管理又可能係另一種講法. 係 Knowledge Level 入面, 儲存既資料應該避免重覆, 因為所做既係最新既資訊, 以最實時既資訊去處理日常既運作, 雖然用既係最新既資訊, 但儲存落 Database 入面就可能要將所有有關既 Value Object 儲存埋, 因為往後既日子如果 Knowledge Level 有所改變, 亦唔應該影響到 Database 入面 Operation Level 完成品既歷史, 方面了解當時既情況, 等同於 Data Mining 一樣, 做既唔係避免重覆, 而係有必要地控制重覆, 呢個就係 Database Design 入面一個重要既地方!!

Leave a Comment : more...

Rebuild Index on MsSQL

by calendarw on Apr.28, 2009, under database

今日要對現有既 Table 做 Performance Testing, 但因為要 join 太多既 Table, 搜尋一個要 join 4 個 Table 先拎到想要既 “一隻” Record 既時間竟然要 11 秒, 可想而知個 Table Size 有幾大吧 (其實七位數字既 Record Count 啫, 而 where case 只有兩個用 ‘=’ 既 criteria, 而且所有 Record 只能 insert, 不能 delete).

經過我 rebuild 過接近 10 個已有既 index 之後 (等了好幾分鐘), 搜尋時間減少至只需 5 秒…

以下係 mssql 2005 下 rebuild index 既一個 statement 既例子

ALTER INDEX IX_tblMaster ON tblMaster REBUILD;

IX_tblMaster 係 index 名
tblMaster 係 table 名

Leave a Comment :, more...

Sqlcmd

by calendarw on Apr.27, 2009, under database

Project Development Phase 已經開始, 但因為部份 Design 未全面, 所以仲有好多 Drop Table, Create Table 及 Initialize Value 既動作要做, 因為係咁, 所以就上網搵左點樣在 command prompt 行 SQL file.

Usage: Sqlcmd [-U login id] [-P password] [-S server] [-H hostname] [-E trusted connection] [-d use database name] [-l login timeout] [-t query timeout] [-h headers] [-s colseparator] [-w screen width] [-a packetsize] [-e echo input] [-I Enable Quoted Identifiers] [-c cmdend] [-L list servers[clean output]] [-q "cmdline query"] [-Q "cmdline query" and exit] [-m errorlevel] [-V severitylevel] [-W remove trailing spaces] [-u unicode output] [-r[0|1] msgs to stderr] [-i inputfile] [-o outputfile] [-z new password] [-f | i:[,o:]] [-Z new password and exit] [-k[1|2] remove[replace] control characters] [-y variable length type display width] [-Y fixed length type display width] [-p[1] print statistics[colon format]] [-R use client regional setting] [-b On error batch abort] [-v var = "value"...] [-A dedicated admin connection] [-X[1] disable commands, startup script, environment variables [and exit]] [-x disable variable substitution] [-? show syntax summary]

而自己就寫左個 batch 係 rebuild database:

sqlcmd -U calendarw -P password -S 192.168.1.10 -d testing_database -i Drop_Table.sql
sqlcmd -U calendarw -P password -S 192.168.1.10 -d testing_database -i Create_Table.sql
sqlcmd -U calendarw -P password -S 192.168.1.10 -d testing_database -i Init_Default_Value.sql
Leave a Comment : more...

Oracle Notes

by calendarw on Mar.14, 2008, under database

Run shell in sqlplus:

host ls -l

or

!ls -l

Dummy Table Name: dual

Get user error message in oracle:

select * from user_errors;

or

show errors;

ERROR: PLS-00201: identifier ‘SYS.DBMS_PIPE’ must be declared
Connect to the SYS schema and issue the following command:

grant all on dbms_pipe to public;
Leave a Comment : more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...