我有一个慢查询,其中包含10个以上的左连接和10个类似的运算符。见吼叫。
SystemContact约有。 10K的数据,另外两个有超过100k的数据。 查询看起来没有优化,这是因为它是由用于搜索SystemContact的程序生成的。
我遇到的问题是,查询在我们的系统上运行速度非常慢,至少需要一分钟才能运行。我们已经完成了更新统计信息和对表进行碎片整理,但它似乎并没有提高性能。
最初我认为它可能与当前使用nvarchar的字符串DataType有关,但是当我将所有那些带有数据的表加载到同一服务器上的tempdb时,查询运行大约2秒。
我想知道造成这种缓慢的原因是什么原因?
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
select
Id, Context1, Context2, Context3, LastModifiedBy
from SystemContact
left join SystemField as SF1 on SystemContact.Id = SF1.ContactId and SF1.FieldId = 1111
left join SystemField as SF2 on SystemContact.Id = SF2.ContactId and SF2.FieldId = 5445
left join SystemField as SF3 on SystemContact.Id = SF3.ContactId and SF3.FieldId = 3423
left join SystemField as SF4 on SystemContact.Id = SF4.ContactId and SF4.FieldId = 6545
left join SystemField as SF5 on SystemContact.Id = SF5.ContactId and SF5.FieldId = 5464
left join SystemFieldText as SFT1 on SystemContact.Id = SFT1.ContactId and SFT1.FieldId = 546
left join SystemFieldText as SFT2 on SystemContact.Id = SFT2.ContactId and SFT2.FieldId = 7565
left join SystemFieldText as SFT3 on SystemContact.Id = SFT3.ContactId and SFT3.FieldId = 456
left join SystemFieldText as SFT4 on SystemContact.Id = SFT4.ContactId and SFT4.FieldId = 457
left join SystemFieldText as SFT5 on SystemContact.Id = SFT5.ContactId and SFT5.FieldId = 56
left join SystemFieldText as SFT6 on SystemContact.Id = SFT6.ContactId and SFT6.FieldId = 85
where
(SF1.Value like '%john.smith@example.com%') or (SF2.Value like '%john.smith@example.com%') or (SF3.Value like '%john.smith@example.com%')
or (SF4.Value like '%john.smith@example.com%') or (SF5.Value like '%john.smith@example.com%')
or (SFT2.Value like '%john.smith@example.com%') or (SFT3.Value like '%john.smith@example.com%') or (SFT4.Value like '%john.smith@example.com%')
or (SFT5.Value like '%john.smith@example.com%') or (SFT6.Value like '%john.smith@example.com%')
编辑:
以下是执行计划的链接:https://goo.gl/gICWir
答案 0 :(得分:0)
CREATE NONCLUSTERED INDEX ix
ON dbo.SystemFieldText (FieldId, ContactId) INCLUDE (value)
GO
SELECT id,
Context1,
Context2,
Context3,
LastModifiedBy
FROM dbo.SystemContact
WHERE EXISTS(
SELECT *
FROM dbo.SystemField SF
WHERE SystemContact.id = SF.ContactId
AND SF.value LIKE '%john.smith@example.com%'
AND SF.FieldId IN (1111, 5445, 3423, 6545, 5464)
)
OR EXISTS(
SELECT *
FROM dbo.SystemFieldText
WHERE SystemContact.id = SFT.ContactId
AND SFT.value LIKE '%john.smith@example.com%'
AND SFT.FieldId IN (546, 7565, 7565, 456, 457, 56, 85)
)