DB2 SQL 技巧/提示

发布于 2024-12-10 12:50:09 字数 1280 浏览 0 评论 0原文

我在我的应用程序(其生成)的一部分中使用了此语句。 LIKE '%%' 为空只是因为没有向其发送搜索参数,因此它会拉入所有结果。

我对整个数据库仍然是新手,想知道是否有任何改进我的语句的技巧?

我确信这看起来很可怕,但请对我放轻松!

更新:我主要希望从中获得更好的性能。我已经使用了 DB2 Advisor 并创建了它说我应该创建的密钥。这对一些人有所帮助,但仍然比我希望的要慢。

谢谢。

SELECT * FROM 
(
    SELECT rownumber() OVER (ORDER BY pgmajdsc, item) AS ROW_NUM, 
    line, item, pgmajdsc, manufacturer 
    FROM 
    (
        SELECT iline AS line, iitem AS item, pgmajdsc, ldesc AS manufacturer 
        FROM itemmast 
        LEFT JOIN itemweb on iline=line and iitem=item 
        JOIN linemst ON iline=lline 
        LEFT JOIN custord ON opline=iline AND opitem=iitem AND opcust='1234' 
        LEFT JOIN cartwdtl ON cwline=iline and cwitem=iitem and cwusr='foo' AND cwcust='1234' 
        LEFT JOIN itematr ON iline=ialine AND iitem=iaitem 
        LEFT JOIN prodgrp ON iaclass=pgclass 
        WHERE ico = 01 
        AND iecomm = 'Y' 
        AND (UPPER(ITEMDESC) LIKE '%%' OR UPPER(PRODDESC) LIKE '%%' OR 
            LINE LIKE '%%' OR UPPER(MFGNAME) LIKE '%%' OR ITEM LIKE '%%' OR 
            PRODNAME LIKE '%%' OR IDESC1 LIKE '%%' OR IDESC2 LIKE '%%' OR 
            IMFGNO LIKE '%%' OR IITEM LIKE '%%')
    ) AS TEMP
) AS ROW_NUM  
WHERE ROW_NUM BETWEEN 0 AND 25 
ORDER BY pgmajdsc, item

I am using this statement in part of my application, its generated. The LIKE '%%' are empty only because no search parameter was sent to it, so its pulling in all results.

I am still a novice to the whole database thing and was wondering if there are any tips for improving my statement?

I'm sure it looks horrible but please go easy on me!

Update: I am mostly looking to get better performance from this. I already used the DB2 adviser and created the keys it said I should make. That helped some but its still slower than I had hoped.

Thank you.

SELECT * FROM 
(
    SELECT rownumber() OVER (ORDER BY pgmajdsc, item) AS ROW_NUM, 
    line, item, pgmajdsc, manufacturer 
    FROM 
    (
        SELECT iline AS line, iitem AS item, pgmajdsc, ldesc AS manufacturer 
        FROM itemmast 
        LEFT JOIN itemweb on iline=line and iitem=item 
        JOIN linemst ON iline=lline 
        LEFT JOIN custord ON opline=iline AND opitem=iitem AND opcust='1234' 
        LEFT JOIN cartwdtl ON cwline=iline and cwitem=iitem and cwusr='foo' AND cwcust='1234' 
        LEFT JOIN itematr ON iline=ialine AND iitem=iaitem 
        LEFT JOIN prodgrp ON iaclass=pgclass 
        WHERE ico = 01 
        AND iecomm = 'Y' 
        AND (UPPER(ITEMDESC) LIKE '%%' OR UPPER(PRODDESC) LIKE '%%' OR 
            LINE LIKE '%%' OR UPPER(MFGNAME) LIKE '%%' OR ITEM LIKE '%%' OR 
            PRODNAME LIKE '%%' OR IDESC1 LIKE '%%' OR IDESC2 LIKE '%%' OR 
            IMFGNO LIKE '%%' OR IITEM LIKE '%%')
    ) AS TEMP
) AS ROW_NUM  
WHERE ROW_NUM BETWEEN 0 AND 25 
ORDER BY pgmajdsc, item

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

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

发布评论

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

评论(1

爱的故事 2024-12-17 12:50:09

假设您正在为 like 子句使用参数,我要更改的第一件事是检查它是否为空的语句,它将避免所有其他比较。另一个变化是删除嵌套。通过使用 Fetch first 子句,您可以达到相同的效果(无论如何我认为)。

注意:这尚未经过测试,因为我目前面前没有数据库,但我很确定它会起作用。

SELECT rownumber() OVER (ORDER BY pgmajdsc, item) AS ROW_NUM, 
       iline AS line, iitem AS item, pgmajdsc, ldesc AS manufacturer 
FROM itemmast 
   LEFT JOIN itemweb on iline=line and iitem=item 
   JOIN linemst ON iline=lline 
   LEFT JOIN custord ON opline=iline AND opitem=iitem AND opcust='1234' 
   LEFT JOIN cartwdtl ON cwline=iline and cwitem=iitem and cwusr='foo' AND cwcust='1234' 
   LEFT JOIN itematr ON iline=ialine AND iitem=iaitem 
   LEFT JOIN prodgrp ON iaclass=pgclass 
WHERE ico = 01 
  AND iecomm = 'Y' 
  AND (parameter IS NULL 
       OR (UPPER(ITEMDESC) LIKE '%'||parameter||'%' 
       OR UPPER(PRODDESC) LIKE '%'||parameter||'%'
       OR LINE LIKE '%'||parameter||'%' 
       OR UPPER(MFGNAME) LIKE '%'||parameter||'%' 
       OR ITEM LIKE '%'||parameter||'%' 
       OR PRODNAME LIKE '%'||parameter||'%' 
       OR IDESC1 LIKE '%'||parameter||'%' 
       OR IDESC2 LIKE '%'||parameter||'%' 
       OR IMFGNO LIKE '%'||parameter||'%' 
       OR IITEM LIKE '%'||parameter||'%') )
ORDER BY ROW_NUM, pgmajdsc, item
FETCH FIRST 25 ROWS ONLY

编辑:另一个想法是,除非您确实需要行号,否则您可以将其省略,然后按如下方式执行订单:

SELECT iline AS line, iitem AS item, pgmajdsc, ldesc AS manufacturer 
FROM itemmast 
   LEFT JOIN itemweb on iline=line and iitem=item 
   JOIN linemst ON iline=lline 
   LEFT JOIN custord ON opline=iline AND opitem=iitem AND opcust='1234' 
   LEFT JOIN cartwdtl ON cwline=iline and cwitem=iitem and cwusr='foo' AND cwcust='1234' 
   LEFT JOIN itematr ON iline=ialine AND iitem=iaitem 
   LEFT JOIN prodgrp ON iaclass=pgclass 
WHERE ico = 01 
  AND iecomm = 'Y' 
  AND (parameter IS NULL 
       OR (UPPER(ITEMDESC) LIKE '%'||parameter||'%' 
       OR UPPER(PRODDESC) LIKE '%'||parameter||'%'
       OR LINE LIKE '%'||parameter||'%' 
       OR UPPER(MFGNAME) LIKE '%'||parameter||'%' 
       OR ITEM LIKE '%'||parameter||'%' 
       OR PRODNAME LIKE '%'||parameter||'%' 
       OR IDESC1 LIKE '%'||parameter||'%' 
       OR IDESC2 LIKE '%'||parameter||'%' 
       OR IMFGNO LIKE '%'||parameter||'%' 
       OR IITEM LIKE '%'||parameter||'%') )
ORDER BY pgmajdsc, item
FETCH FIRST 25 ROWS ONLY

我不知道计算行号对性能有多大影响,但如果您不需要它,则不需要退货。

Assuming you are using a parameter for the like clauses the first thing I would change is that statement to check if it is null, it will avoid all the other comparisons. The other change would be to remove the nesting. You can achieve the same effect (I think anyway) by using the Fetch first clause.

NOTE: This is untested as I do not have a db in front of me at the moment, but I am pretty sure it will work.

SELECT rownumber() OVER (ORDER BY pgmajdsc, item) AS ROW_NUM, 
       iline AS line, iitem AS item, pgmajdsc, ldesc AS manufacturer 
FROM itemmast 
   LEFT JOIN itemweb on iline=line and iitem=item 
   JOIN linemst ON iline=lline 
   LEFT JOIN custord ON opline=iline AND opitem=iitem AND opcust='1234' 
   LEFT JOIN cartwdtl ON cwline=iline and cwitem=iitem and cwusr='foo' AND cwcust='1234' 
   LEFT JOIN itematr ON iline=ialine AND iitem=iaitem 
   LEFT JOIN prodgrp ON iaclass=pgclass 
WHERE ico = 01 
  AND iecomm = 'Y' 
  AND (parameter IS NULL 
       OR (UPPER(ITEMDESC) LIKE '%'||parameter||'%' 
       OR UPPER(PRODDESC) LIKE '%'||parameter||'%'
       OR LINE LIKE '%'||parameter||'%' 
       OR UPPER(MFGNAME) LIKE '%'||parameter||'%' 
       OR ITEM LIKE '%'||parameter||'%' 
       OR PRODNAME LIKE '%'||parameter||'%' 
       OR IDESC1 LIKE '%'||parameter||'%' 
       OR IDESC2 LIKE '%'||parameter||'%' 
       OR IMFGNO LIKE '%'||parameter||'%' 
       OR IITEM LIKE '%'||parameter||'%') )
ORDER BY ROW_NUM, pgmajdsc, item
FETCH FIRST 25 ROWS ONLY

EDIT: Another thought is unless you actually need the row number you can leave that out and just do your order by like so:

SELECT iline AS line, iitem AS item, pgmajdsc, ldesc AS manufacturer 
FROM itemmast 
   LEFT JOIN itemweb on iline=line and iitem=item 
   JOIN linemst ON iline=lline 
   LEFT JOIN custord ON opline=iline AND opitem=iitem AND opcust='1234' 
   LEFT JOIN cartwdtl ON cwline=iline and cwitem=iitem and cwusr='foo' AND cwcust='1234' 
   LEFT JOIN itematr ON iline=ialine AND iitem=iaitem 
   LEFT JOIN prodgrp ON iaclass=pgclass 
WHERE ico = 01 
  AND iecomm = 'Y' 
  AND (parameter IS NULL 
       OR (UPPER(ITEMDESC) LIKE '%'||parameter||'%' 
       OR UPPER(PRODDESC) LIKE '%'||parameter||'%'
       OR LINE LIKE '%'||parameter||'%' 
       OR UPPER(MFGNAME) LIKE '%'||parameter||'%' 
       OR ITEM LIKE '%'||parameter||'%' 
       OR PRODNAME LIKE '%'||parameter||'%' 
       OR IDESC1 LIKE '%'||parameter||'%' 
       OR IDESC2 LIKE '%'||parameter||'%' 
       OR IMFGNO LIKE '%'||parameter||'%' 
       OR IITEM LIKE '%'||parameter||'%') )
ORDER BY pgmajdsc, item
FETCH FIRST 25 ROWS ONLY

I do not know how much a hit on performance is on calculating rownumber, but if you do not need it, no need to return it.

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