日期:2014-05-18  浏览次数:20570 次

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