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
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.
EDIT: Another thought is unless you actually need the row number you can leave that out and just do your order by like so:
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.