SQL2005CLR函式擴充套件 山寨索引

2021-08-23 15:10:51 字數 3319 閱讀 5671

本文只是乙個山寨試驗品,思路僅供參考.

對於檔案索引lucene才是權威,這裡只是自己實現了乙個可以實現簡單檔案索引的半成品.所謂檔案索引就是把sql字串按位元組分詞儲存到磁碟檔案目錄結構中用來快速定位.

原理介紹:

索引建立

目錄結構劃分方案也只是很簡易的實現了一下,通過unicode把任意連續的兩個字元(中文或英文)分為4個位元組來做四層目錄,把索引的內容對應的主關鍵字(主要為了使用sql索引和唯一性)作為檔名,兩個字元在索引內容中的位置作為檔案字尾來儲存.檔案本身為0位元組,不儲存任何資訊.

比如一條資料 "pk001","山寨索引"

山寨索引 四個字的unicode為

[0]: 113

[1]: 92

[2]: 232

[3]: 91

[4]: 34

[5]: 125

[6]: 21

[7]: 95

那麼對應的檔案結構為

..\113\92\232\91\pk001 .0

..\232\91\34\125\pk001 .1

..\34\125\21\95\pk001 .2

索引使用

比如搜尋"寨索引"

則搜尋 "..\232\91\34\125\"

目錄下的所有檔案,然後根據

pk001 .1的檔案字尾名1,去看

..\34\125\21\95\pk001.2檔案是否存在.依次類推,最後返回乙個結果集.

實用性具體的實用性還有待驗證.這只是實現了精確的like搜尋,而不能做常見搜尋引擎的分詞效果.另外海量資料重建索引的效能也是面臨很嚴峻的問題,比如cpu負載和磁碟io負載.關於windows乙個目錄下可以保持多少個檔案而不會對檔案搜尋造成大的效能損失也有待評估,不過這個可以考慮根據主鍵的檔名hash來增加檔案目錄深度降低單一目錄下的檔案數量.

演示效果

實現了針對test標的name和caption兩個欄位作索引搜尋.

-- 設定和獲取索引檔案根目錄

--select dbo.xfn_setmyindexfileroot('d:\myindex')

--select dbo.xfn_getmyindexfileroot()

-- 建立測試環境 go

create

table test( id uniqueidentifier

,name

nvarchar

( 100), caption nvarchar

( 100))

insert

into test select

top 3 newid

(),'

我的索引 '

,' 測試 '

from sysobjects

insert

into test select

top 3 newid

(),'

我的測試 '

,' 索引 '

from sysobjects

insert

into test select

top 3 newid

(),'

測試索引 '

,' 測試索引 '

from sysobjects

insert

into test select

top 3 newid

(),'

我的索引 '

,' 索引 '

from sysobjects

create

index i_testid on test( id)

-- 建立索引檔案

declare

@t int

select

@t=

dbo. xfn_setkeyformyindex( id,

'testindex'

,name

+' '

+ caption)

from

test

-- 查詢資料

select a.* from test a, dbo. xfn_getkeyfrommyindex( '測試 索引 我的' , 'testindex' ) b

where a. id= b. pk/*

0c4634ea-df94-419a-a8e5-793bd5f54eed 我的索引 測試

2dd87b38-cd3f-4f14-bb4a-00678463898f 我的索引 測試

8c67a6c3-753f-474c-97ba-ce85a2455e3e 我的索引 測試

c9706bf1-fb1f-42fb-8a48-69ec37ead3e5 我的測試 索引

8bbf25cc-9dbb-4fcb-b2eb-d318e587dd5f 我的測試 索引

8b45322d-8e46-4691-961a-cd0078f1fa0a 我的測試 索引 */

--drop table test

clr**如下:編譯為myfullindex.dll

using

system;

using

system.data.sqltypes;

using

microsoft.sqlserver.server;

using

system.collections;

using

system.collections.generic;

public

partial

class

userdefinedfunctions

else

} ///

/// 獲取索引目錄

///

///

[microsoft.sqlserver.server.sqlfunction ]

public

static

sqlstring getroot()

///

/// 建立索引

///

///主鍵

///索引名稱

///索引內容

///

[microsoft.sqlserver.server.sqlfunction ]

public

static

sqlint32 setindex(sqlstring key,sqlstring indexname,sqlstring content)

//////

查詢索引

///

///關鍵字(空格區分)

///索引名稱 <

SQL2005CLR函式擴充套件 天氣服務

我們可以用clr獲取網路服務 來顯示到資料庫自定函式的結果集中,比如163的天氣預報 他的這個xml結果的日期是不正確的,但這個我們暫不討論。從這個xml獲取天氣的clr 如下,用webclient訪問一下就可以了。然後通過dom物件遍歷節點屬性返回給結果集。using system using s...

SQL2005CLR函式擴充套件 天氣服務

我們可以用clr獲取網路服務 來顯示到資料庫自定函式的結果集中,比如163的天氣預報 他的這個xml結果的日期是不正確的,但這個我們暫不討論。從這個xml獲取天氣的clr 如下,用webclient訪問一下就可以了。然後通過dom物件遍歷節點屬性返回給結果集。using system using s...

SQL2005CLR函式擴充套件 山寨索引

本文只是乙個山寨試驗品,思路僅供參考.對於檔案索引lucene才是權威,這裡只是自己實現了乙個可以實現簡單檔案索引的半成品.所謂檔案索引就是把sql字串按位元組分詞儲存到磁碟檔案目錄結構中用來快速定位.原理介紹 索引建立 目錄結構劃分方案也只是很簡易的實現了一下,通過unicode把任意連續的兩個字...