網(wǎng)站模板:SQL2005CLR函數(shù)擴(kuò)展 - 關(guān)于山寨索引_MySQL教程
推薦:mysql與mysqli的區(qū)別與用法mysql與mysqli的區(qū)別與用法 mysql是非持繼連接函數(shù)而mysqli是永遠(yuǎn)連接函數(shù)。也就是說 mysql每次鏈接都會(huì)打開一個(gè)連接的進(jìn)程而mysqli多次運(yùn)行mysqli將使用同一連接進(jìn)程,從而減少了服務(wù)器的開銷 有些朋友在編程的時(shí)候,使用new mysqli('localhost', usenamer', 'password
本文只是一個(gè)山寨試驗(yàn)品,思路僅供參考.
--------------------------------------------------------------------------------
原理介紹:
索引建立
目 錄結(jié)構(gòu)劃分方案也只是很簡易的實(shí)現(xiàn)了一下,通過unicode把任意連續(xù)的兩個(gè)字符(中文或英文)分為4個(gè)字節(jié)來做四層目錄,把索引的內(nèi)容對應(yīng)的主關(guān)鍵字 (主要為了使用sql索引和唯一性)作為文件名,兩個(gè)字符在索引內(nèi)容中的位置作為文件后綴來存儲(chǔ).文件本身為0字節(jié),不保存任何信息.
比如一條數(shù)據(jù) "pk001","山寨索引"
山寨索引 四個(gè)字的unicode為
[0]: 113
[1]: 92
[2]: 232
[3]: 91
[4]: 34
[5]: 125
[6]: 21
[7]: 95
那么對應(yīng)的文件結(jié)構(gòu)為
../113/92/232/91/pk001 .0
../232/91/34/125/pk001 .1
../34/125/21/95/pk001 .2
索引使用
比如搜索"寨索引 "
則搜索 "../232/91/34/125/" 目錄下的所有文件,然后根據(jù) pk001 .1的文件后綴名1,去看 ../34/125/21/95/pk001.2文件是否存在.依次類推,最后返回一個(gè)結(jié)果集.
--------------------------------------------------------------------------------
實(shí)用性
具 體的實(shí)用性還有待驗(yàn)證.這只是實(shí)現(xiàn)了精確的like搜索,而不能做常見搜索引擎的分詞效果.另外海量數(shù)據(jù)重建索引的性能也是面臨很嚴(yán)峻的問題,比如cpu 負(fù)載和磁盤io負(fù)載.關(guān)于windows一個(gè)目錄下可以保持多少個(gè)文件而不會(huì)對文件搜索造成大的性能損失也有待評估,不過這個(gè)可以考慮根據(jù)主鍵的文件名 hash來增加文件目錄深度降低單一目錄下的文件數(shù)量.
--------------------------------------------------------------------------------
演示效果
實(shí)現(xiàn)了針對test標(biāo)的name和caption兩個(gè)字段作索引搜索.
-- 設(shè)置和獲取索引文件根目錄
--select dbo.xfn_SetMyIndexFileRoot('d:/MyIndex')
--select dbo.xfn_GetMyIndexFileRoot()
-- 建立測試環(huán)境
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
-- 查詢數(shù)據(jù)
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>
/// 設(shè)置索引目錄
/// </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"> 索引內(nèi)容 </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"> 關(guān)鍵字(空格區(qū)分) </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>
/// 獲取單個(gè)詞的索引信息
/// </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;
}
};
分享:網(wǎng)頁模板MySQL出現(xiàn)Can't create/write to file 'C:\Windows\TEMP\#sql_990_0.MYI解決辦法MySQL出現(xiàn)Cant create/write to file C:\WINDOWS\TEMP\#sql_718_0.MYD(Errcode: 17)解決辦法 Error: Cant create/write to file C:\WINDOWS\TEMP\#sql_718_0.MYD(Errcode: 17) 出現(xiàn)這樣的情況有以下的可能: 1、C:\WINDOWS\TEMP文件夾權(quán)限不夠,至少也要給出USERS組的可
- MSSQL清空日志刪除日志文件
- 關(guān)于數(shù)據(jù)庫中保留小數(shù)位的問題
- 解析mysql與Oracle update的區(qū)別
- mysql 導(dǎo)入導(dǎo)出數(shù)據(jù)庫以及函數(shù)、存儲(chǔ)過程的介紹
- MySQL——修改root密碼的4種方法(以windows為例)
- 解決MYSQL出現(xiàn)Can''t create/write to file ''#sql_5c0_0.MYD''的問題
- 深入理解SQL的四種連接-左外連接、右外連接、內(nèi)連接、全連接
- 解析:內(nèi)聯(lián),左外聯(lián),右外聯(lián),全連接,交叉連接的區(qū)別
- mysql出現(xiàn)“Incorrect key file for table”處理方法
- mysql重裝后出現(xiàn)亂碼設(shè)置為utf8可解決
- 淺析一個(gè)MYSQL語法(在查詢中使用count)的兼容性問題
- 解析MySQL中INSERT INTO SELECT的使用
MySQL教程Rss訂閱編程教程搜索
MySQL教程推薦
- MySQL 關(guān)于表復(fù)制 insert into 語法的詳細(xì)介紹
- Mysql兩種情況下更新字段中部分?jǐn)?shù)據(jù)的方法
- mysql錯(cuò)誤及解決全攻略
- 基于ubuntu中使用mysql實(shí)現(xiàn)opensips用戶認(rèn)證的解決方法
- MySQL筆記之?dāng)?shù)據(jù)備份與還原的使用詳解
- 解析如何使用Zend Framework 連接數(shù)據(jù)庫
- 解析mysql中:單表distinct、多表group by查詢?nèi)コ貜?fù)記錄
- 解析MySQL中INSERT INTO SELECT的使用
- mysql 導(dǎo)入導(dǎo)出數(shù)據(jù)庫以及函數(shù)、存儲(chǔ)過程的介紹
- 淺析一個(gè)MYSQL語法(在查詢中使用count)的兼容性問題
猜你也喜歡看這些
- SQL Server占用內(nèi)存的解決方法
- 循序漸進(jìn)講解數(shù)據(jù)表的十二個(gè)設(shè)計(jì)原則
- 怎樣從數(shù)據(jù)行入手保護(hù)SQL Server數(shù)據(jù)安全
- 解讀SQL Server數(shù)據(jù)庫備份的方法
- 解答SQL Server日志文件損壞嚴(yán)重的問題
- 如何把Excel數(shù)據(jù)導(dǎo)入到SQL2008數(shù)據(jù)庫的實(shí)例方法
- 關(guān)于PowerDesigner初體驗(yàn)的使用介紹
- 淺析SQL Server asp.net 數(shù)據(jù)提供程序連接池
- 四個(gè)語句幫你提高 SQL Server 的伸縮性
- 解析 SQL Server 2008 的精妙之處
- 相關(guān)鏈接:
復(fù)制本頁鏈接| 搜索網(wǎng)站模板:SQL2005CLR函數(shù)擴(kuò)展 - 關(guān)于山寨索引
- 教程說明:
MySQL教程-網(wǎng)站模板:SQL2005CLR函數(shù)擴(kuò)展 - 關(guān)于山寨索引
。