У меня есть таблица с полнотекстовым индексированным столбцом MiddlePart
. Таблица имеет около 600 000 строк. Следующий запрос очень быстрый (30 результатов, <1 секунда): </p>
select * from DomainName
where contains (MiddlePart, '"antiques*"')
OR freetext(MiddlePart, 'antiques')
Этот запрос также очень быстрый (5 результатов, <1 секунда): </p>
select * from DomainName
where (contains (MiddlePart, '"dog*"') OR freetext(MiddlePart, 'dog'))
AND (contains (MiddlePart, '"training*"') OR freetext(MiddlePart, 'training'))
Так почему ОБА эти запросы ЧРЕЗВЫЧАЙНО медлительны? (90 секунд + и я отменил запрос):
Запрос A:
select * from DomainName
where contains (MiddlePart, '"antiques*"')
OR freetext(MiddlePart, 'antiques')
union
select * from DomainName
where (contains (MiddlePart, '"dog*"') OR freetext(MiddlePart, 'dog'))
AND (contains (MiddlePart, '"training*"') OR freetext(MiddlePart, 'training'))
Запрос B:
select * from DomainName
where (contains (MiddlePart, '"antiques*"')
OR freetext(MiddlePart, 'antiques'))
OR
((contains (MiddlePart, '"dog*"') OR freetext(MiddlePart, 'dog'))
AND (contains (MiddlePart, '"training*"') OR freetext(MiddlePart, 'training')))
EDIT
Полный текст плана для QUERY A:
|--Merge Join(Union)
|--Nested Loops(Inner Join, OUTER REFERENCES:(FulltextMatch.[docid], [Expr1055]) WITH ORDERED PREFETCH)
| |--Stream Aggregate(GROUP BY:(FulltextMatch.[docid]))
| | |--Merge Join(Concatenation)
| | |--Table-valued function
| | |--Table-valued function
| |--Clustered Index Seek(OBJECT:([domaining].[dbo].[DomainName].[PK__DomainNa__3214EC2708EA5793]), SEEK:([domaining].[dbo].[DomainName].[ID]=FulltextMatch.[docid]) ORDERED FORWARD)
|--Merge Join(Left Semi Join, MERGE:([domaining].[dbo].[DomainName].[ID])=(FulltextMatch.[docid]), RESIDUAL:([domaining].[dbo].[DomainName].[ID]=FulltextMatch.[docid]))
|--Nested Loops(Inner Join, OUTER REFERENCES:(FulltextMatch.[docid], [Expr1056]) WITH ORDERED PREFETCH)
| |--Stream Aggregate(GROUP BY:(FulltextMatch.[docid]))
| | |--Merge Join(Concatenation)
| | |--Table-valued function
| | |--Table-valued function
| |--Clustered Index Seek(OBJECT:([domaining].[dbo].[DomainName].[PK__DomainNa__3214EC2708EA5793]), SEEK:([domaining].[dbo].[DomainName].[ID]=FulltextMatch.[docid]) ORDERED FORWARD)
|--Merge Join(Concatenation)
|--Table-valued function
|--Table-valued function
Полный текст плана запроса B:
|--Nested Loops(Left Semi Join, OUTER REFERENCES:([domaining].[dbo].[DomainName].[ID]))
|--Clustered Index Scan(OBJECT:([domaining].[dbo].[DomainName].[PK__DomainNa__3214EC2708EA5793]))
|--Concatenation
|--Table-valued function
|--Nested Loops(Left Semi Join)
| |--Concatenation
| | |--Table-valued function
| | |--Table-valued function
| |--Row Count Spool
| |--Concatenation
| |--Table-valued function
| |--Table-valued function
|--Table-valued function