有人可以告诉我不同之处......我不知道是什么问题,不知道如何用几句话来描述它上面的stackoverflow,)........
我的SQL Server 2008 R2 x64上的这个选择需要 1min和16secons :
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
...是的!你是对的我刚删除了一个'OR块'......但是我移除了哪一个
并不重要如果我有超过3(!)的查询需要很长时间(顺便说一句:如果一个块的结果为0,而且结果也是表空为止)
现在来了......
这个也非常快(不到一秒)并且我已经包含了所有'OR块'
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个条目)
答案 0 :(得分: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)
您还应确认适当的索引支持联接。