OR语句的效率

问题描述 投票:0回答:0

在我们的产品表中(< 200K records) , we list which "Lists" the product can appear on in our webpage

Product    char(10)   Key
InList1    char(1)    NonKey   Y/N
InList2    char(1)    NonKey   Y/N
InList3    char(1)    NonKey   Y/N
InList4    char(1)    NonKey   Y/N
InList5    char(1)    NonKey   Y/N
InList6    char(1)    NonKey   Y/N
InList7    char(1)    NonKey   Y/N
InList8    char(1)    NonKey   Y/N
InList9    char(1)    NonKey   Y/N
InList10   char(1)    NonKey   Y/N
InList11   char(1)    NonKey   Y/N
InList12   char(1)    NonKey   Y/N
InList13   char(1)    NonKey   Y/N
InList14   char(1)    NonKey   Y/N

在我的 SSRS 参数中,我有一个 ListNames 作为值和标签,用户可以选择 1 到 All。

然后我使用 JOIN(@List,";") 将单个参数发送到存储过程。

我需要做的是找到一种方法,在找到第一个之后不再继续检查所有列表#。

此刻,我正在做以下事情:

-- **********************************************************************
select ' ' into #List
insert into #List
SELECT trim(value ) List
FROM STRING_SPLIT(@List, ';');
-- **********************************************************************
select Product
into #temp_Product
from ProductTable  with(nolock);
-- **********************************************************************

select Product from #temp_Product with(nolock)
where (
    ('List1' in (select List from #List) and InList1 = 'Y') or 
    ('List2' in (select List from #List) and InList2 = 'Y') or 
    ('List3' in (select List from #List) and InList3 = 'Y') or 
    ('List4' in (select List from #List) and InList4 = 'Y') or 
    ('List5' in (select List from #List) and InList5 = 'Y') or 
    ('List6' in (select List from #List) and InList6 = 'Y') or 
    ('List7' in (select List from #List) and InList7 = 'Y') or 
    ('List8' in (select List from #List) and InList8 = 'Y') or 
    ('List9' in (select List from #List) and InList9 = 'Y') or 
    ('List10' in (select List from #List) and InList10 = 'Y') or 
    ('List11' in (select List from #List) and InList11 = 'Y') or 
    ('List12' in (select List from #List) and InList12 = 'Y') or 
    ('List13' in (select List from #List) and InList13 = 'Y') or 
    ('List14' in (select List from #List) and InList14 = 'Y')
      )

因为单个产品可以出现在一对多列表中,所以我遇到的问题是必须使用 OR 语句检查每种可能性的性能问题。

据我了解,即使您只需要一个为真,TSQL 也会检查所有可能性的正确性。检查至少一个陈述是否为真的最有效方法是什么?

换句话说,如果将“List1”、“List3”、“List4”和“List5”传递到 SP,如果在所有这些列表中都找到了产品,则查询将 NOT 执行其他验证,或在找到第一个 TRUE 语句后停止进一步验证...如果您需要进一步说明,请告诉我。

提前致谢。

我试过上面的代码

performance query-optimization processing-efficiency
© www.soinside.com 2019 - 2024. All rights reserved.