0

有人可以告诉我区别吗...我不知道是什么问题,也不知道如何用几句话来描述它,以便在上面搜索stackoverflow,)........

我的 SQL Server 2008 R2 x64 上的这个选择大约需要1 分钟和 16 秒

select T_MESSAGE.MSG_GUID from T_MESSAGE
where 
T_MESSAGE.MSG_BODY like @searchpattern
or T_MESSAGE.MSG_COMMENT like @searchpattern
or T_MESSAGE.MSG_SEDERDISPLAYINFORMATION like @searchpattern 
or (select COUNT(*) from T_RECIPIENT 
          join T_RECIPIENT_ANDROID on T_RECIPIENT_ANDROID.RAND_REC_GUID = T_RECIPIENT.REC_GUID 
    where T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID 
    and T_RECIPIENT_ANDROID.RAND_DEVICETOKEN like @searchpattern) > 0   
or (select COUNT(*) from T_RECIPIENT 
    join T_RECIPIENT_IOS on T_RECIPIENT_IOS.RIOS_REC_GUID = T_RECIPIENT.REC_GUID 
    where T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID 
    and T_RECIPIENT_IOS.RIOS_DEVICETOKEN like @searchpattern) > 0
or (select COUNT(*) from T_RECIPIENT 
    join T_RECIPIENT_SMS on T_RECIPIENT_SMS.RSMS_REC_GUID = T_RECIPIENT.REC_GUID 
    where T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID 
    and T_RECIPIENT_SMS.RSMS_PHONENUMBER like @searchpattern) > 0
or (select COUNT(*) from T_RECIPIENT 
    join T_RECIPIENT_SMTP on T_RECIPIENT_SMTP.RSMTP_REC_GUID = T_RECIPIENT.REC_GUID 
    where T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID 
    and T_RECIPIENT_SMTP.RSMTP_ADDRESS like @searchpattern) > 0

这段代码只需要几毫秒..

select T_MESSAGE.MSG_GUID from T_MESSAGE
where 
T_MESSAGE.MSG_BODY like @searchpattern
or T_MESSAGE.MSG_COMMENT like @searchpattern
or T_MESSAGE.MSG_SEDERDISPLAYINFORMATION like @searchpattern 
or (select COUNT(*) from T_RECIPIENT 
          join T_RECIPIENT_ANDROID on T_RECIPIENT_ANDROID.RAND_REC_GUID = T_RECIPIENT.REC_GUID 
    where T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID 
    and T_RECIPIENT_ANDROID.RAND_DEVICETOKEN like @searchpattern) > 0   
or (select COUNT(*) from T_RECIPIENT 
    join T_RECIPIENT_IOS on T_RECIPIENT_IOS.RIOS_REC_GUID = T_RECIPIENT.REC_GUID 
    where T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID 
    and T_RECIPIENT_IOS.RIOS_DEVICETOKEN like @searchpattern) > 0
or (select COUNT(*) from T_RECIPIENT 
    join T_RECIPIENT_SMS on T_RECIPIENT_SMS.RSMS_REC_GUID = T_RECIPIENT.REC_GUID 
    where T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID 
    and T_RECIPIENT_SMS.RSMS_PHONENUMBER like @searchpattern) > 0

....是的!你说得对,我刚刚删除了我的“或块”之一......但我删除了哪一个并不重要

如果我有超过 3 个(!)查询需要很长时间(顺便说一句:如果一个块的结果是 0 并且不仅结果而且表也是空的)

而现在来了……

这一个也非常快(不到一秒),我已经包含了所有的“或块”

declare @searchpattern as varchar(MAX)
set @searchpattern = 'mysearchstring'

declare @guidTable table
( 
    MSG_GUID UniqueIdentifier
)

insert into @guidTable 
select T_MESSAGE.MSG_GUID from T_MESSAGE
where 
T_MESSAGE.MSG_BODY like @searchpattern
or T_MESSAGE.MSG_COMMENT like @searchpattern
or T_MESSAGE.MSG_SENDERDISPLAYINFORMATION like @searchpattern 
or (select COUNT(*) from T_RECIPIENT 
    join T_RECIPIENT_ANDROID on T_RECIPIENT_ANDROID.RAND_REC_GUID = T_RECIPIENT.REC_GUID 
    where T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID 
    and T_RECIPIENT_ANDROID.RAND_DEVICETOKEN like @searchpattern) > 0   

insert into @guidTable 
select T_MESSAGE.MSG_GUID from T_MESSAGE 
where
(select COUNT(*) from T_RECIPIENT 
    join T_RECIPIENT_IOS on T_RECIPIENT_IOS.RIOS_REC_GUID = T_RECIPIENT.REC_GUID 
    where T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID 
    and T_RECIPIENT_IOS.RIOS_DEVICETOKEN like @searchpattern) > 0 

insert into @guidTable 
select T_MESSAGE.MSG_GUID from T_MESSAGE 
where
(select COUNT(*) from T_RECIPIENT 
    join T_RECIPIENT_SMS on T_RECIPIENT_SMS.RSMS_REC_GUID = T_RECIPIENT.REC_GUID 
    where T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID 
    and T_RECIPIENT_SMS.RSMS_PHONENUMBER like @searchpattern) > 0

insert into @guidTable 
select T_MESSAGE.MSG_GUID from T_MESSAGE 
where 
(select COUNT(*) from T_RECIPIENT 
    join T_RECIPIENT_SMTP on T_RECIPIENT_SMTP.RSMTP_REC_GUID = T_RECIPIENT.REC_GUID 
    where T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID 
    and T_RECIPIENT_SMTP.RSMTP_ADDRESS like @searchpattern) > 0


select * from @guidTable 

...我只是拆分选择并将结果查询逐个查询写入一个新的声明表...

目前我通过在 sql 中创建一个函数来解决这个任务 - 但谁能告诉我可能是什么问题?

(顺便说一句:数据库目前只有大约 5000 个条目)

4

1 回答 1

1

这应该更快使用exists而不是获取您不需要的计数:

select T_MESSAGE.MSG_GUID from T_MESSAGE 
where  
T_MESSAGE.MSG_BODY like @searchpattern 
or T_MESSAGE.MSG_COMMENT like @searchpattern 
or T_MESSAGE.MSG_SEDERDISPLAYINFORMATION like @searchpattern  
or exists (select 42 from T_RECIPIENT  
          join T_RECIPIENT_ANDROID on T_RECIPIENT_ANDROID.RAND_REC_GUID = T_RECIPIENT.REC_GUID  
    and T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID  
    and T_RECIPIENT_ANDROID.RAND_DEVICETOKEN like @searchpattern)
or exists (select 42 from T_RECIPIENT  
    join T_RECIPIENT_IOS on T_RECIPIENT_IOS.RIOS_REC_GUID = T_RECIPIENT.REC_GUID  
    and T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID  
    and T_RECIPIENT_IOS.RIOS_DEVICETOKEN like @searchpattern)
or exists (select 42 from T_RECIPIENT  
    join T_RECIPIENT_SMS on T_RECIPIENT_SMS.RSMS_REC_GUID = T_RECIPIENT.REC_GUID  
    and T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID  
    and T_RECIPIENT_SMS.RSMS_PHONENUMBER like @searchpattern)
or exists (select 42 from T_RECIPIENT  
    join T_RECIPIENT_SMTP on T_RECIPIENT_SMTP.RSMTP_REC_GUID = T_RECIPIENT.REC_GUID  
    and T_RECIPIENT.REC_MSG_GUID = T_MESSAGE.MSG_GUID  
    and T_RECIPIENT_SMTP.RSMTP_ADDRESS like @searchpattern)

您还应该确认适当的索引支持连接。

于 2012-07-04T16:49:34.027 回答