程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> SQL2005CLR函數擴展 - 關於山寨索引

SQL2005CLR函數擴展 - 關於山寨索引

編輯:關於SqlServer
本文只是一個山寨試驗品,思路僅供參考.
--------------------------------------------------------------------------------
原理介紹:
索引建立
目錄結構劃分方案也只是很簡易的實現了一下,通過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
{
    /// <summary>
    /// 設置索引目錄
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    [Microsoft.SqlServer.Server.SqlFunction ]
    public static SqlBoolean SetRoot(SqlString value)
    {
        if (value.IsNull) return false ;
        if (System.IO.Directory .Exists(value.Value))
        {
            root = value.Value;
            return true ;
        }
        else
        {
            return false ;
        }
    }
    /// <summary>
    /// 獲取索引目錄
    /// </summary>
    /// <returns></returns>
    [Microsoft.SqlServer.Server.SqlFunction ]
    public static SqlString GetRoot()
    {
        return new SqlString (root);
    }
    /// <summary>
    /// 建立索引
    /// </summary>
    /// <param name="key"> 主鍵 </param>
    /// <param name="indexName"> 索引名稱 </param>
    /// <param name="content"> 索引內容 </param>
    /// <returns></returns>
    [Microsoft.SqlServer.Server.SqlFunction ]
    public static SqlInt32 SetIndex(SqlString key,SqlString indexName,SqlString content)
    {
        if (key.IsNull || content.IsNull||indexName.IsNull) return 0;
        return _setIndex(key.Value,indexName.Value, content.Value);
    }

    /// <summary>
    /// 查詢索引
    /// </summary>
    /// <param name="word"> 關鍵字(空格區分) </param>
    /// <param name="indexName"> 索引名稱 </param>
    /// <returns></returns>
    [SqlFunction (TableDefinition = "pk nvarchar(900)" , Name = "GetIndex" , FillRowMethodName = "FillRow" )]
    public static IEnumerable GetIndex(SqlString word,SqlString indexName)
    {

        System.Collections.Generic.List <string > ret = new List <string >();
        if (word.IsNull || indexName.IsNull) return ret;
        return _getIndex2(word.Value, indexName.Value);
    }

    public static void FillRow(Object obj, out SqlString pk)
    {
        string key = obj.ToString();
        pk = key;
    }
    static string root = @"d:/index" ;

    /// <summary>
    /// 獲取有空格分隔的索引信息
    /// </summary>
    /// <param name="word"></param>
    /// <param name="indexName"></param>
    /// <returns></returns>
    static System.Collections.Generic.List <string > _getIndex2(string word, string indexName)
    {
        string [] arrWord = word.Split(new char [] { ' ' }, StringSplitOptions .RemoveEmptyEntries);

        System.Collections.Generic.List <string > key_0 = _getIndex(arrWord[0], indexName);

        if (arrWord.Length == 0) return key_0;
        System.Collections.Generic.List <string > [] key_list=new List <string >[arrWord.Length-1];
        for (int i = 0; i < arrWord.Length-1; i++)
        {
            System.Collections.Generic.List <string > key_i = _getIndex(arrWord[i+1],indexName);
            key_list[i] = key_i;
        }

        for (int i=key_0.Count-1;i>=0;i--)
        {
            foreach (System.Collections.Generic.List <string > key_i in key_list)
            {
                if (key_i.Contains(key_0[i]) == false )
                {
                    key_0.RemoveAt(i);
                    continue ;
                }
            }
        }
        return key_0;
    }
    /// <summary>
    /// 獲取單個詞的索引信息
    /// </summary>
    /// <param name="word"></param>
    /// <param name="indexName"></param>
    /// <returns></returns>
    static System.Collections.Generic.List <string > _getIndex(string word, string indexName)
    {
        System.Collections.Generic.List <string > ret = new List <string >();
        byte [] bWord = System.Text.Encoding .Unicode.GetBytes(word);
        if (bWord.Length < 4) return ret;

        string path = string .Format(@"{0}/{1}/{2}/{3}/{4}/{5}/" , root,indexName, bWord[0], bWord[1], bWord[2], bWord[3]);
        if (System.IO.Directory .Exists(path) == false )
        {
            return ret;
        }
        string [] arrFiles = System.IO.Directory .GetFiles(path);

        foreach (string file in arrFiles)
        {
            string key = System.IO.Path .GetFileNameWithoutExtension(file);
            string index = System.IO.Path .GetExtension(file).TrimStart(new char [] { '.' });
            int cIndex = int .Parse(index);
            bool bHas = true ;
            for (int i = 2; i < bWord.Length - 3; i = i + 2)
            {
                string nextFile = string .Format(@"{0}/{1}/{2}/{3}/{4}/{5}/{6}.{7}" ,
                    root, indexName, bWord[i + 0], bWord[i + 1], bWord[i + 2], bWord[i + 3], key, ++cIndex);

                if (System.IO.File .Exists(nextFile) == false )
                {
                    bHas = false ;
                    break ;
                }
            }
            if (bHas == true &&ret.Contains(key)==false )
                ret.Add(key);

        }
        return ret;
    }

    /// <summary>
    /// 建立索引文件
    /// </summary>
    /// <param name="key"></param>
    /// <param name="indexName"></param>
    /// <param name="content"></param>
    /// <returns></returns>
    static int _setIndex(string key,string indexName, string content)
    {
        byte [] bContent = System.Text.Encoding .Unicode.GetBytes(content);
        if (bContent.Length <= 4) return 0;
        for (int i = 0; i < bContent.Length - 3; i = i + 2)
        {
            string path = string .Format(@"{0}/{1}/{2}/{3}/{4}/{5}/" , root,indexName, bContent[i + 0], bContent[i + 1], bContent[i + 2], bContent[i + 3]);
            if (System.IO.Directory .Exists(path) == false )
            {
                System.IO.Directory .CreateDirectory(path);
            }
            string file = string .Format(@"{0}/{1}.{2}" , path, key, i / 2);

            if (System.IO.File .Exists(file) == false )
            {
                System.IO.File .Create(file).Close();
            }
        }
        return content.Length;
    }
};

--------------------------------------------------------------------------------
部署的sql腳本如下
--drop function dbo.xfn_SetMyIndexFileRoot
--drop function dbo.xfn_GetMyIndexFileRoot
--drop function dbo.xfn_GetKeyFromMyIndex
--drop function dbo.xfn_SetKeyForMyIndex
--drop assembly MyFullIndex
--go
CREATE ASSEMBLY MyFullIndex FROM 'd:/SQLCLR/MyFullIndex.dll' WITH PERMISSION_SET = UnSAFE;
--
go
-- 索引搜索
CREATE FUNCTION dbo. xfn_GetKeyFromMyIndex ( @word nvarchar ( max ), @indexName  nvarchar ( 900))   
RETURNS table ( pk nvarchar ( 100))
AS EXTERNAL NAME MyFullIndex. UserDefinedFunctions. GetIndex
go
-- 索引建立
CREATE FUNCTION dbo. xfn_SetKeyForMyIndex ( @pk nvarchar ( 900), @indexName  nvarchar ( 900), @word nvarchar ( max ))   
RETURNS int
AS EXTERNAL NAME MyFullIndex. UserDefinedFunctions. SetIndex
go
-- 獲取索引文件根目錄
CREATE FUNCTION dbo. xfn_GetMyIndexFileRoot ()   
RETURNS nvarchar ( max )
AS EXTERNAL NAME MyFullIndex. UserDefinedFunctions. GetRoot
go
-- 設置索引文件根目錄(默認目錄為 d:/myindex )
CREATE FUNCTION dbo. xfn_SetMyIndexFileRoot ( @FileRoot nvarchar ( max ))   
RETURNS bit
AS EXTERNAL NAME MyFullIndex. UserDefinedFunctions. SetRoot
go
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved