SQL Server處理Unicode字串時,前置詞"N"之使用
作者:Ahan 日期:2007-09-12 21:47
前一陣子在寫一個中文姓名利用標準拚音翻成英文姓名的元件時,為了處理SQL使用Unicode文字來進行查詢時,遇到一個問題,就是當資料庫的查詢關鍵字是像堃、峯等Unicode字集時,SQL Server會無法辨認而無法查到應有的資料,查了一下微軟的支援服務後,發現在SQL Server中處理Unicode字串常數時,必須在該字串前加上大寫字母 N 做為前置詞(prefix),這樣子才能讓SQL Server在處理該輸入字串時,先行轉為目前資料庫的非Unicode字碼集。
其實這個問題是SQL Server當初在設計時,在6.5版的時候不支援Unicode資料,因此為了後能與未來的版本相容,在日後修正時,為了在不改變系統核心的情況下,將Unicode字碼的轉換工作做在SQL Command底下,但前題是所使用的資料庫版本必須支援Unicode字碼集,也就是SQL Server 7.0以後的版本。
Example:
SELECT * FROM TABLE WHERE NAME LIKE N"%KeyWord%";
在 SQL Server 中處理 Unicode 字串常數時,必需為所有的 Unicode 字串加上前置詞 N
http://support.microsoft.com/kb/239530/zh-tw
其實這個問題是SQL Server當初在設計時,在6.5版的時候不支援Unicode資料,因此為了後能與未來的版本相容,在日後修正時,為了在不改變系統核心的情況下,將Unicode字碼的轉換工作做在SQL Command底下,但前題是所使用的資料庫版本必須支援Unicode字碼集,也就是SQL Server 7.0以後的版本。
Example:
SELECT * FROM TABLE WHERE NAME LIKE N"%KeyWord%";
在 SQL Server 中處理 Unicode 字串常數時,必需為所有的 Unicode 字串加上前置詞 N
http://support.microsoft.com/kb/239530/zh-tw
使用SQL Server 2005 取得雜湊值
作者:Ahan 日期:2007-01-17 11:36
HashBytes (Transact-SQL)
Returns the MD2, MD4, MD5, SHA, or SHA1 hash of its input.
HashBytes ( '<algorithm>', { @input | 'input' } ) <algorithm>::= MD2 | MD4 | MD5 | SHA | SHA1
Example:
DECLARE @HashThis nvarchar(max);
SELECT @HashThis = CONVERT(nvarchar,'dslfdkjLK85kldhnv$n000#knf');
SELECT HashBytes('SHA1', @HashThis);
GO
取得雜湊值是個二進位varbinary欄位,如要直接取其雜湊碼可利用SQL 2005 master 資料庫內建之 master.dbo.fn_varbintohexstr 預儲程序來進行轉換,使用方法以SHA1為例是
RIGHT(master.dbo.fn_varbintohexstr(HashBytes('SHA1', CONVERT(varchar,欲轉換的資料表欄位名稱))),40)
SQL Server常用的日期格式轉換
作者:Ahan 日期:2006-03-29 10:35
SQL Server中文版的預設的日期字串datetime格式是yyyy-mm-dd Thh:mm:ss.mmm
例如:
例如:
select getdate()
2004-09-12 11:06:08.177
找相關資料時發現有以下範例,節錄下來以備日後參考用。2004-09-12 11:06:08.177
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
20040912110608
20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
2004.09.12
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
12 09 2004
select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004
09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 )
11:06:08
11:06:08
select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1
09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004
09-12-2004
select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1
12 09 2004 1
select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177
11:06:08.177
MySQL performance testing:Storage Engine
作者:Ahan 日期:2005-11-28 00:51
Storage Engine: MyISAM:BTREE
| Storage Engine: MyISAM:BTREE
|
Storage Engine: InnoDB:BTREE
| Storage Engine: InnoDB:BTREE
|
Storage Engine: HEAP:BTREE
| Storage Engine: HEAP:BTREE
|
Storage Engine: MEMORY:HASH
| Storage Engine: MEMORY:HASH
|
| Table1: 效能測試比較表 | ||||
| Command / Consuming / Storage Engine | MyISAM:BTREE | InnoDB:BTREE | HEAP:BTREE | MEMORY:HASH |
| > Command | 1.954 | 2.144 | 1.1 | 1.501 |
| = Command | 0.0010 | 0.0010 | 0.0010 | 0.0010 |
| Difference | 1.953 | 2.143 | 1.099 | 1.500 |
資料庫索引效能測試
作者:Ahan 日期:2005-11-22 06:00
測試環境
中央處理器:Pentium III 800
實體記憶體:768 MB
作業系統:Fedora Core 4
資料庫系統:MySQL 5, PostgreSQL 8.0
資料庫位址:localhost
程式語言:JSP
網站伺服器:Apache Tomcat + JDBC Drivers(MySQL, PostgreSQL)
背景資料
MySQL
Storage Engine Allowable Index Types
MyISAM BTREE
InnoDB BTREE
MEMORY/HEAP HASH, BTREE
Example:
CREATE TABLE lookup (id INT) ENGINE = MEMORY;
CREATE INDEX id_index USING BTREE ON lookup (id);
PostgreSQL
Example
CREATE [ UNIQUE ] INDEX index_name ON table
[ USING acc_name ] ( column [ ops_name ] [, ...] )
CREATE [ UNIQUE ] INDEX index_name ON table
[ USING acc_name ] ( func_name( column [, ... ]) [ ops_name ] )
acc_name
The name of the access method to be used for the index. The default access method is BTREE. Postgres provides three access methods for indexes:
BTREE - an implementation of Lehman-Yao high-concurrency btrees.
RTREE - implements standard rtrees using Guttman's quadratic split algorithm.
HASH - an implementation of Litwin's linear hashing
說明
系統以預先建立好之資料表進行Insert單一欄位資料新增測試,測試前均先將資料表內容清空並移除索引,測試中欲進行加入索引後之測試時同樣清空原先Insert的資料再由程式中直接新增索引,所使用的索引型態均為預設值(BTREE),此步驟均不列入測試時間,所得結果如下:
查看...
中央處理器:Pentium III 800
實體記憶體:768 MB
作業系統:Fedora Core 4
資料庫系統:MySQL 5, PostgreSQL 8.0
資料庫位址:localhost
程式語言:JSP
網站伺服器:Apache Tomcat + JDBC Drivers(MySQL, PostgreSQL)
背景資料
MySQL
Storage Engine Allowable Index Types
MyISAM BTREE
InnoDB BTREE
MEMORY/HEAP HASH, BTREE
Example:
CREATE TABLE lookup (id INT) ENGINE = MEMORY;
CREATE INDEX id_index USING BTREE ON lookup (id);
PostgreSQL
Example
CREATE [ UNIQUE ] INDEX index_name ON table
[ USING acc_name ] ( column [ ops_name ] [, ...] )
CREATE [ UNIQUE ] INDEX index_name ON table
[ USING acc_name ] ( func_name( column [, ... ]) [ ops_name ] )
acc_name
The name of the access method to be used for the index. The default access method is BTREE. Postgres provides three access methods for indexes:
BTREE - an implementation of Lehman-Yao high-concurrency btrees.
RTREE - implements standard rtrees using Guttman's quadratic split algorithm.
HASH - an implementation of Litwin's linear hashing
說明
系統以預先建立好之資料表進行Insert單一欄位資料新增測試,測試前均先將資料表內容清空並移除索引,測試中欲進行加入索引後之測試時同樣清空原先Insert的資料再由程式中直接新增索引,所使用的索引型態均為預設值(BTREE),此步驟均不列入測試時間,所得結果如下:
查看...

