问题查询使用多个相交。TSQL优化多个相交
使用count()= 6将它更改为in('alpha','beta','gamma','delta','epsilon','phi')组不是一个选项,因为应用程序支持像通配符(如alpha%)。但count()= 6查询在少于1秒内运行。
使用通配符可以使用多个连接,这就是它的结构。在4或更少的情况下,相交表现比多重联结更好,但不幸的是没有测试5或更多。
查询对任何4个术语表现都很好 - 少于1秒。
字面上任何4 - 前4,后4或中4 4.
5或更多,然后它死了 - 我在2分钟杀死查询。
在6个术语让它运行 - 5分钟返回795行。
最多4个术语查询计划将循环与合并连接进行混合。
在5个或更多的术语中,查询计划都是循环连接。
是否有将查询提示应用于相交的语法?
尝试了两套使用()()但没有更改查询计划的3套。
(-- start term
select [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
where [ftsIndexWordOnce].[wordID] in (
select [id] from [FTSwordDef] with (nolock)
where [word] like 'alpha')
) -- end term
INTERSECT
(-- start term
select [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
where [ftsIndexWordOnce].[wordID] in (
select [id] from [FTSwordDef] with (nolock)
where [word] like 'beta')
) -- end term
INTERSECT
(-- start term
select [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
where [ftsIndexWordOnce].[wordID] in (
select [id] from [FTSwordDef] with (nolock)
where [word] like 'gamma')
) -- end term
INTERSECT
(-- start term
select [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
where [ftsIndexWordOnce].[wordID] in (
select [id] from [FTSwordDef] with (nolock)
where [word] like 'delta')
) -- end term
INTERSECT
(-- start term
select [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
where [ftsIndexWordOnce].[wordID] in (
select [id] from [FTSwordDef] with (nolock)
where [word] like 'epsilon')
) -- end term
INTERSECT
(-- start term
select [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
where [ftsIndexWordOnce].[wordID] in (
select [id] from [FTSwordDef] with (nolock)
where [word] like 'phi')
) -- end term
认为我有一个修复
select distinct [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
Inner Merge Join [FTSwordDef] with (nolock)
On [FTSwordDef].[ID] = [ftsIndexWordOnce].[wordID]
And [FTSwordDef].[word] like 'alpha'
INTERSECT
select distinct [ftsIndexWordOnce].[sID]
from [ftsIndexWordOnce] with (nolock)
Inner Merge Join [FTSwordDef] with (nolock)
On [FTSwordDef].[ID] = [ftsIndexWordOnce].[wordID]
And [FTSwordDef].[word] like 'beta'
查询优化器还是那张愚蠢的在5个以上,但是这会强制先加入是合并,并将其保存。
不错的想法,但AND返回0.很确定它一次只评估一个wordID,单个wordID不能像'alpha'和'beta'一样。你怎么看? – Paparazzi
D'oh,对吧。所以回到交叉点,但用EXISTS而不是IN。以交叉位作为子查询的连接也可以工作。 – JAQFrost
尝试用INTERSECT存在,但仍然没有解决它。在5或更多时,它会全部循环连接并死亡。但是,内部合并连接可能会诀窍。 – Paparazzi