首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

哪位高手帮小弟我优化一下这个存储过成,模糊查询的。现在这个查询速度太慢了

2012-03-19 
谁帮我优化一下这个存储过成,模糊查询的。现在这个查询速度太慢了比如建立全文索引,如何建呢?SQL codeALTER

谁帮我优化一下这个存储过成,模糊查询的。现在这个查询速度太慢了
比如建立全文索引,如何建呢?

SQL code
ALTER PROC [dbo].[GetRelatedArticle]           @artID int,     @returnCount int       AS    declare @tags nvarchar(500),@keywords nvarchar(500),@keys nvarchar(500),@count int,@i int,@whereStr nvarchar(2000),@Sql nvarchar(2000),@Sql1 nvarchar(1000)    set @i=1    set @whereStr=''         select @tags=class_name2,@keywords=keywords,@keys=keys from article_table where id=@artID        if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#aa') and type='U')    drop table #aa    //关键字是'|关键字1|关键字2|'这样格式的       //split是自己些的分割函数    select * into #aa from split(@keys,'|') where short_str<>''    insert into #aa select short_str from split(@keywords,',') where short_str<>''    insert into #aa select short_str from split(@tags,'|') where short_str<>''         delete a from #aa a where  exists(select 1 from #aa where short_str=a.short_str and ID<a.ID)          select @count=COUNT(id) from #aa    if @count=0 and (@tags<>'' or @keywords<>'' or @keys<>'')    begin        if @keys<>''            insert into #aa values(REPLACE(@keys,'|',''))        else if @keywords<>''            insert into #aa values(REPLACE(@keywords,',',''))        else if @tags<>''            insert into #aa values(REPLACE(@tags,'|',''))                    set @count=1    end     declare @class_name nvarchar(50),@TopCount int        select @class_name=class_name1 from article_table where id=@artID    set @TopCount=@returnCount    if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#relatetable') and type='U')    drop table #relatetable    create table #relatetable(rid int identity (1,1),id int,class_name1 nvarchar(50),class_name2 nvarchar(200),head nvarchar(500),pic nvarchar(200),head_short nvarchar(50),rootpath nvarchar(50),class_root nvarchar(50))         while @i<=@count    begin                 declare @tag nvarchar(50),@id int        declare @counttemp_b int        Select TOP 1 @tag=short_str,@id=id FROM #aa order by id        delete from #aa where ID=@id                if @id>52000 --因为以前的keys和tags差不多。所以没必要查询        begin            --先判断最相关在            set @whereStr='a.keys like ''%|'+REPLACE(@tag,'''','''''')+'|%'''            --执行            if @whereStr=''            begin                set @whereStr='1<>1'            end            set @sql='insert INTO #relatetable(id,class_name1,class_name2,head,pic,head_short,rootpath,class_root) select top '+STR(@TopCount)+' a.id,a.class_name1,a.class_name2,a.head,a.pic,a.head_short,a.rootpath,b.file_name from article_table a inner join class_table b on a.class_name1=b.class_name where ('+@whereStr+') and addtime<=GETDATE() and a.[ID]<>'+STR(@artID)+' and a.status=2 and b.parent_id=''|0|''  and a.id not in (select id from #relatetable) and uptime between dateadd(mm,-4,getDate()) and getDate() order by a.uptime desc'            EXEC SP_EXECUTESQL @Sql                                     select @counttemp_b=COUNT(id) from #relatetable            --查询是否已经查出国@returnCount条,如果是,则不需要再查            if @counttemp_b=@returnCount            begin                select * from #relatetable order by rid                  return            end            else            begin                set @TopCount=@returnCount-@counttemp_b            end        end                        set @whereStr='a.keywords like ''%'+REPLACE(@tag,'''','''''')+'%'' or a.head like ''%'+REPLACE(@tag,'''','''''')+'%'''        --执行        if @whereStr=''        begin            set @whereStr='1<>1'        end        set @sql='insert INTO #relatetable(id,class_name1,class_name2,head,pic,head_short,rootpath,class_root) select top '+STR(@TopCount)+' a.id,a.class_name1,a.class_name2,a.head,a.pic,a.head_short,a.rootpath,b.file_name from article_table a inner join class_table b on a.class_name1=b.class_name where ('+@whereStr+') and addtime<=GETDATE() and a.[ID]<>'+STR(@artID)+' and a.status=2 and b.parent_id=''|0|''  and a.id not in (select id from #relatetable) and uptime between dateadd(mm,-4,getDate()) and getDate() order by a.uptime desc'        EXEC SP_EXECUTESQL @Sql                      select @counttemp_b=COUNT(id) from #relatetable        if @counttemp_b=@returnCount        begin            select * from #relatetable order by rid              return        end        else        begin            set @TopCount=@returnCount-@counttemp_b        end        set @i=@i+1    end         if @whereStr=''        begin            set @whereStr='1<>1'        end                  if @TopCount>0    begin        declare @class_root nvarchar(50)        select top 1 @class_root=FILE_NAME from class_table where class_name=@class_name and parent_id='|0|'        insert into #relatetable(id,class_name1,class_name2,head,pic,head_short,rootpath,class_root) select top (@TopCount) id,class_name1,class_name2,head,pic,head_short,rootpath,@class_root from article_table where [status]=2 and addtime<=GETDATE() and id not in (select id from #relatetable) and id<>@artID and class_name1=@class_name order by uptime desc    end//最后查询出所得的结果    select * from #relatetable order by rid     drop table #relatetable 



[解决办法]
你先把表的结构和index,数据量,一类的上来看看
[解决办法]
慢的原因在于循环

热点排行