1

好的,基本上我正在创建一个存储过程,它将为我们的冷聚变功率搜索返回数据。

我创建了一个视图,用于保存来自多个表的数据,当然返回相同的列名。

然后在我的存储过程中,我创建了一个像这样的简单临时表......

    CREATE TABLE #TempSearchResults
(
    search_id int identity,
    id integer,
    type varchar(20),
    title varchar(50),
    url varchar(250),
    rank integer
)

然后我向它添加了一个索引,以我可能有限的经验作为提高性能的一种方式。

CREATE UNIQUE INDEX idx on #TempSearchResults (search_id)

然后我选择了大规模查询

insert into #TempSearchResults
select id, type, title, url, rank + 1200 as rank
from my view
where company_id = @company_id
and title like @keyword
union all
select id, type, title, url, rank + 1100 as rank
from my view
where company_id = @company_id
and title like @keyword
and description like @keyword

并且它继续在表中找到关键字的位置具有不同的排名数学值。

最后它确实......

select id, type, title, url, rank
from #TempSearchResults
group by id, type, title, url, rank
order by rank desc, title asc;

现在,当我在 Coldfusion 中测试该存储过程时,似乎需要很长时间。

所以我在想,要么我使用错误的临时表,要么不完全地获得最佳性能。

或者我应该切换到表变量......

但我只是在阅读......临时表 VS 表变量

有趣的是,这个存储过程似乎比我直接通过冷融合运行查询要慢,我不喜欢这样做。

我希望获得最佳性能....

谢谢...

下面是我正在使用的视图的基本逻辑或代码。

select some field as id, some field as title, some field as description, 'url link' as url, 1 as rank
from table a
union
select some field as id, some field as title, some field as description, 'url link' as url, 1 as rank
from table b
union
select some field as id, some field as title, some field as description, 'url link' as url, 1 as rank
from table c

之类的。我不能透露确切的细节,因为那将是安全漏洞。我希望这能让它更清楚。

4

2 回答 2

2

我认为根本不需要使用临时表或表变量。你可以写

select id, type, title, url, rank
from (
    select id, type, title, url, rank + 1200 as rank 
    from my view 
    where company_id = @company_id and title like @keyword 

    union all 

    select id, type, title, url, rank + 1100 as rank 
    from my view 
    where company_id = @company_id and title like @keyword and description like @keyword
) as t
group by id, type, title, url, rank
order by rank desc, title asc;

编辑:

用 UNION 替换 UNION ALL,这可以简化为

select id, type, title, url, rank + 1200 as rank 
from my view 
where company_id = @company_id and title like @keyword 

union 

select id, type, title, url, rank + 1100 as rank 
from my view 
where company_id = @company_id and title like @keyword and description like @keyword

order by rank desc, title asc;
于 2011-02-18T18:06:24.770 回答
0

在您选择数据的表格上使用with (Nolock)提示;如果您的应用程序允许脏读,它可能会提高性能。

于 2012-07-18T13:10:49.960 回答