我在我的应用程序的一部分中使用此语句,它已生成。 LIKE'%%'仅为空,因为没有向其发送搜索参数,因此它会提取所有结果。
我仍然是整个数据库的新手,并想知道是否有任何改进我的陈述的提示?
我确定它看起来很可怕,但请放轻松我!
更新:我主要希望从中获得更好的表现。我已经使用了DB2顾问并创建了它说我应该创建的密钥。这对一些人有所帮助,但仍然比我希望的要慢。
谢谢。
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
答案 0 :(得分:1)
假设你正在使用like子句的参数,我要改变的第一件事是检查它是否为null的语句,它将避免所有其他比较。另一个变化是删除嵌套。通过使用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
我不知道计算rownumber对性能有多大影响,但如果你不需要它,则无需返回它。