SQL(DB2) WHERE 子句优化

发布于 2024-11-27 14:32:17 字数 390 浏览 1 评论 0原文

我将大量产品收集到一个数组中,按行和项目命名唯一的产品。然后我将其提供给 SQL 语句的 WHERE 子句。当这个产品列表变得相当大时,我的 WHERE 子句也会扩展成一团糟。因此,我的 WHERE 子句的示例如下:

 WHERE FOO = 'Y'
 AND ((iline = ? AND iitem = ? )
 OR (iline = ? AND iitem = ? ) 
 OR ... 
 OR (iline = ? AND iitem = ? ))

依此类推,其中每个“iline = ? AND iitem = ?”是一种独特的产品。显然我不是这方面的专家,但似乎在我的 WHERE 子句中偶尔有 100 多个 OR 效率不是很高,我可以以某种方式做得更好。

谢谢。

I gather a large list of products into an array that name a unique product by line and item. Then I feed that to my SQL statement's WHERE clause. When this list of products gets rather large my WHERE clause also expands to an ugly mess. So an example of what my WHERE clause can look like is below:

 WHERE FOO = 'Y'
 AND ((iline = ? AND iitem = ? )
 OR (iline = ? AND iitem = ? ) 
 OR ... 
 OR (iline = ? AND iitem = ? ))

And so on, where each "iline = ? AND iitem = ?" are a unique product. It's apparent I am no expert at this, but it seems like having the occasional 100+ ORs in my WHERE clause is not very efficient and I could be doing it better somehow.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟的代价 2024-12-04 14:32:17

您可以使用类似这样的内容:

Presuming, iline has values like A,B,C,D,E.... and iitem posses 1,2,3,4,5... Now, you need combinations to be satisfied like 
(iline = 'A' AND iitem = '2'), 
(iline = 'E' AND iitem = '2'), 
(iline = 'B' AND iitem = '3'), 
(iline = 'A' AND iitem = '3'), 
(iline = 'E' AND iitem = '2'),
(iline = 'B' AND iitem = '4')

这可能会挤压到

WHERE FOO = 'Y'
AND (iline = 'A' and iitem IN ('2','3'))
AND (iline = 'B' and iitem IN ('3','4'))
AND (iline = 'E' and iitem IN ('2','3'))

理想情况下,您需要添加:

  • 当您在 iline 上有一个条件时,将
  • 另一个文字添加到现有条件的 IN 中。假设您必须添加一个新条件 (iline = 'B' and iitem = '5'),您可以简单地向现有 iline 添加文字“5”,而不是添加新的 OR = 'B' 条件如 (iline = 'B' and iitem IN ('3','4','5'))

希望我清楚地表达了我的概念,请让我知道您的问题。

You could use something like this:

Presuming, iline has values like A,B,C,D,E.... and iitem posses 1,2,3,4,5... Now, you need combinations to be satisfied like 
(iline = 'A' AND iitem = '2'), 
(iline = 'E' AND iitem = '2'), 
(iline = 'B' AND iitem = '3'), 
(iline = 'A' AND iitem = '3'), 
(iline = 'E' AND iitem = '2'),
(iline = 'B' AND iitem = '4')

This can probably squeeze to

WHERE FOO = 'Y'
AND (iline = 'A' and iitem IN ('2','3'))
AND (iline = 'B' and iitem IN ('3','4'))
AND (iline = 'E' and iitem IN ('2','3'))

Ideally, you'll need to add:

  • An AND when you have a condition on iline
  • another literal to the IN of existing condition. Say if you have to add a new condition (iline = 'B' and iitem = '5'), rather than adding a new OR, you could simply add a literal '5' to the existing iline = 'B' condition like (iline = 'B' and iitem IN ('3','4','5'))

Hope I made my concept clear, please lemme know your questions.

娇俏 2024-12-04 14:32:17

我怀疑它是否更有效,但你可以使用 case 语句来代替:

WHERE 
  FOO = 'Y'
  AND iline = CASE iitem
                WHEN ? THEN ?
                WHEN ? THEN ?
                ...
              END

I doubt it is any more efficient, but you could use a case statement instead:

WHERE 
  FOO = 'Y'
  AND iline = CASE iitem
                WHEN ? THEN ?
                WHEN ? THEN ?
                ...
              END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文