求效率最高的sql-server查询实现方法(续,另加分贴)

yangyanli 2005-10-19 10:16:55
http://community.csdn.net/Expert/topic/4335/4335758.xml?temp=.1848413

表Table 字段 id AA1,AA2 …,记录100000条。
一组关键词数量不定,n个,分别是 K1 K2 K3 Kn
如何用最快的方法把表Table中字段AA1中包含关键词最多的记录id查出来。
各位给个思路。

例如AA1是“中国的首都是北京”,关键词是“中国 北京”
再例如AA1是“中国是伟大的地方”,关键词是“中国 河南”

AA1,varchar 420

...全文
132 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
子陌红尘 2005-10-19
  • 打赏
  • 举报
回复
create table tab(id int identity(1,1),AA1 varchar(420))
insert into tab(AA1) select '中国的首都是北京'
insert into tab(AA1) select '中国是伟大的地方'
go

create procedure sp_test
@v varchar(1000),
@id int output
as
begin
--执行动态查询
declare @s nvarchar(4000),@r nvarchar(1000)

while charindex(' ',@v)>0
begin
set @r = left(@v,charindex(' ',@v)-1)
set @v = stuff(@v,1,charindex(' ',@v),'')
if len(@r)>0
set @s = @s+N'+(case when charindex('''+@r+''',AA1)>0 then 1 else 0 end)'
end
set @s = isnull(@s,N'')+N'+(case when charindex('''+@v+''',AA1)>0 then 1 else 0 end)'
set @s = N'select top 1 @id=id from Tab order by '+stuff(@s,1,1,'')+N' desc'

exec sp_executesql @s,N'@id int out',@id out
return
end
go

declare @id int
exec sp_test '中国 北京',@id output
select @id
/*
1
*/

drop table tab
drop procedure sp_test
子陌红尘 2005-10-19
  • 打赏
  • 举报
回复
create table tab(id int identity(1,1),AA1 varchar(420))
insert into tab(AA1) select '中国的首都是北京'
insert into tab(AA1) select '中国是伟大的地方'
go

create procedure sp_test(@v varchar(1000))
as
begin
--执行动态查询
declare @s nvarchar(4000),@r nvarchar(1000),@id int

while charindex(' ',@v)>0
begin
set @r = left(@v,charindex(' ',@v)-1)
set @v = stuff(@v,1,charindex(' ',@v),'')
if len(@r)>0
set @s = @s+N'+(case when charindex('''+@r+''',AA1)>0 then 1 else 0 end)'
end
set @s = isnull(@s,N'')+N'+(case when charindex('''+@v+''',AA1)>0 then 1 else 0 end)'
set @s = N'select top 1 @id=id from Tab order by '+stuff(@s,1,1,'')+N' desc'

exec sp_executesql @s,N'@id int out',@id out
select @id
end
go

exec sp_test '中国 北京'

/*
1
*/

drop table tab
drop procedure sp_test
zjcxc 2005-10-19
  • 打赏
  • 举报
回复
直接建立关键字表, 每个关键字一条记录, 然后用全文检索来检索数据.
zlp321002 2005-10-19
  • 打赏
  • 举报
回复
--存储过程

CREATE proc P_PTEST
@s nvarchar(4000),
@v nvarchar(1000),
@id int output
as
begin
--执行动态查询
declare @r nvarchar(1000)
while charindex(' ',@v)>0
begin
set @r = left(@v,charindex(' ',@v)-1)
set @v = stuff(@v,1,charindex(' ',@v),'')
if len(@r)>0
set @s = @s+N'+(case when charindex('''+@r+''',AA1)>0 then 1 else 0 end)'
end
set @s = @s+N'+(case when charindex('''+@v+''',AA1)>0 then 1 else 0 end)'
set @s = N'select top 1 @id=id from Tab order by '+stuff(@s,1,1,'')+N' desc'

exec sp_executesql @s,N'@id int out',@id out

end

--调用
declare @t int
exec P_PTEST '', N'中国 北京',@t output
select @t
yangyanli 2005-10-19
  • 打赏
  • 举报
回复
libin_ftsafe(子陌红尘) 的怎么弄成存储过程呢
churchatp1 2005-10-19
  • 打赏
  • 举报
回复
方法之一:
1。你把关键字放到一个表b里面,结构可以col1 nvarcahr(40)
2。然后用select a.aa1,count(*) as [num1] from [table] a,b where patindex('%'+b.col1+'%',a.aa1)>0
group by a.aa1这个语句就可以得到每一行aa1可以里面包含的关键字的总数。
3。找最大的就是了
如:select max(num1) from (
select a.aa1,count(*) as [num1] from [table] a,b where patindex('%'+b.col1+'%',a.aa1)>0
group by a.aa1)
效率应该还可以的
这样就不用游标循环了,一个sql语句就可以了
子陌红尘 2005-10-19
  • 打赏
  • 举报
回复
--生成测试数据
create table tab(id int identity(1,1),AA1 varchar(420))
insert into tab(AA1) select '中国的首都是北京'
insert into tab(AA1) select '中国是伟大的地方'

--执行动态查询
declare @s nvarchar(4000),@id int
declare @v nvarchar(1000),@r nvarchar(1000)
set @v = N'中国 北京'
set @s = N''
while charindex(' ',@v)>0
begin
set @r = left(@v,charindex(' ',@v)-1)
set @v = stuff(@v,1,charindex(' ',@v),'')
if len(@r)>0
set @s = @s+N'+(case when charindex('''+@r+''',AA1)>0 then 1 else 0 end)'
end
set @s = @s+N'+(case when charindex('''+@v+''',AA1)>0 then 1 else 0 end)'
set @s = N'select top 1 @id=id from Tab order by '+stuff(@s,1,1,'')+N' desc'

exec sp_executesql @s,N'@id int out',@id out

--输出结果
select @id
/*
1
*/

--删除测试数据
drop table tab
zxbyhcsdn 2005-10-19
  • 打赏
  • 举报
回复
AA1是“中国是伟大的地方”,关键词是“中国 河南”
“中国是伟大的地方”中并没有"河南" 啦?

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧