大家可能都会用
select * from tb where field like '%关键字%'
但是一旦数据量大用户多,就会造成查询过慢,因此MS提供一种牺牲空间来换取时间的解决方案。
全文检索。原文请看
http://www.microsoft.com/china/msdn/archives/library/dnsql2k/html/sql_fulltextsearch.asp
我对自己的实现加了些注释:
use PopAsk
--使用这个库
exec sp_fulltext_database 'enable'
--使这个库允许全文检索
--生成测试数据
create table t(id int,AA1 varchar(1000),AA2 varchar(1000))
insert into t select 1,'',''
insert into t select 2,'K1,K2,K3,Kn',''
insert into t select 3,'K1,K5,K3,K8',''
insert into t select 4,'K1,K5,K3',''
insert into t select 5,'K1,K2,K3,K4',''
insert into t select 7,'K1,K2,K9,Kn',''
go
--创建用户定义函数
create function f_getmaxid(@str varchar(8000))
returns int
as
begin
declare @tab table(keywords varchar(100))
declare @id int
while charindex(',',@str)>0
begin
insert into @tab select left(@str,charindex(',',@str)-1)
set @str=stuff(@str,1,charindex(',',@str),'')
end
insert into @tab select @str
select
top 1 @id=a.id
from
t a,
@tab b
where
charindex(b.keywords,a.AA1)>1
group by
a.id
order by
count(1) desc
return @id
end
go
--调用用户定义函数输出结果
select id=dbo.f_getmaxid('K1,K2,K3,Kn')
go
/*
id
--
2
*/
--生成测试数据
create table t(id int,AA1 varchar(1000),AA2 varchar(1000))
insert into t select 1,'',''
insert into t select 2,'K1,K2,K3,Kn',''
insert into t select 3,'K1,K5,K3,K8',''
insert into t select 4,'K1,K5,K3',''
insert into t select 5,'K1,K2,K3,K4',''
insert into t select 7,'K1,K2,K9,Kn',''
go
--创建用户定义函数
create function f_getmaxid(@str varchar(8000))
returns int
as
begin
declare @tab table(keywords varchar(100))
declare @id int
set @str='K1,K2,K3,Kn'
while charindex(',',@str)>0
begin
insert into @tab select left(@str,charindex(',',@str)-1)
set @str=stuff(@str,1,charindex(',',@str),'')
end
insert into @tab select @str
select
top 1 @id=a.id
from
t a,
@tab b
where
charindex(b.keywords,a.AA1)>1
group by
a.id
order by
count(1) desc
return @id
end
go
--调用用户定义函数输出结果
select id=dbo.f_getmaxid('K1,K2,K3,Kn')
go
/*
id
--
2
*/
--生成测试数据表(用表变量替代)
declare @t table(id int,AA1 varchar(1000),AA2 varchar(1000))
insert into @t select 1,'K1,K2,K3,Kn',''
insert into @t select 2,'K1,K5,K3,K8',''
--执行处理过程
declare @str varchar(8000)
declare @tab table(keywords varchar(100))
set @str='K1,K2,K3,Kn'
while charindex(',',@str)>0
begin
insert into @tab select left(@str,charindex(',',@str)-1)
set @str=stuff(@str,1,charindex(',',@str),'')
end
insert into @tab select @str
select
top 1 a.id
from
@T a,
@tab b
where
charindex(b.keywords,a.AA1)>1
group by
a.id
order by
count(b.keywords) desc
用程序实现
<%
for i=0 to ubound(关键字数组)
temp=temp & " union all " & "select id from where aaa1 like '%"&k1&"%'"
temp1=temp1 & " union " & "select id from where aaa1 like '%"&k1&"%'"
next
temp=right(temp,len(temp)-len(" union all "))
set temp1_rs=conn.execute(right(temp1,len(temp1)-len(" union "))
if not temp1_rs.eof then
maxid=""
max=0
do while not temp1_rs.eof
set maxid_rs=conn.execute(select count(id) from ("&temp&")aaa where aaa.id='"&temp1_rs("id")&"')
'''''''''判断count(id) 值最多
temp1_rs.movenext
loop
end if
%>
中间可能有些错误,具体思路差不多。不过我想能能够用数据存储过程会快些