Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Improving SQL Queries in DB2

Do you have some SQL you want to improve, but you're not sure how?
Erin Gannon
Editorial Assistant
[116 byte] By [Erin Gannon] at [2007-11-9 18:52:16]
# 1 Re: Improving SQL Queries in DB2
Basically I want to delete a large number of records in groups of
about 100000 so the logs don't over flow. The only way I have been
able to get something equivalent to a row ID is the following:

DELETE FROM UDBADM.whole_loan_master_detailed_lnlv
WHERE (LOAN_SEQ_NBR,LOAN_NBR) IN
(SELECT LOAN_SEQ_NBR,LOAN_NBR
FROM (SELECT ROW_NUMBER() OVER(ORDER BY LOAN_SEQ_NBR) as ROWID,

LOAN_SEQ_NBR,LOAN_NBR FROM UDBADM.whole_loan_master_detailed_lnlv) A

WHERE ROWID < 1000)

This works, but it looks like it would take about six hours to delete a million
records if we let it run all the way through.

Is there some easier, i.e faster way to do this?

"Erin Gannon" <egannon@dev-archive.com> wrote:
>
>Do you have some SQL you want to improve, but you're not sure how?
>
>Erin Gannon
>Editorial Assistant
>
Melissa Matthews at 2007-11-11 23:59:00 >
# 2 Re: Improving SQL Queries in DB2
Is this a one time thing? If so, pick some slow time and do it.
Another thing to try but you will need a down time. Export the records you
want, drop the table, and import the data.

The best thing to do long term is not to let it get this way. Create an
agent (program) the cleans up on a schedule or an event.

"Melissa Matthews" <melissa_matthews@freddiemac.com> wrote:
>
>Basically I want to delete a large number of records in groups of
>about 100000 so the logs don't over flow. The only way I have been
>able to get something equivalent to a row ID is the following:
>
>DELETE FROM UDBADM.whole_loan_master_detailed_lnlv
>WHERE (LOAN_SEQ_NBR,LOAN_NBR) IN
>(SELECT LOAN_SEQ_NBR,LOAN_NBR
> FROM (SELECT ROW_NUMBER() OVER(ORDER BY LOAN_SEQ_NBR) as ROWID,

>
> LOAN_SEQ_NBR,LOAN_NBR FROM UDBADM.whole_loan_master_detailed_lnlv) A

>
> WHERE ROWID < 1000)
>
>This works, but it looks like it would take about six hours to delete a
million
>records if we let it run all the way through.
>
>Is there some easier, i.e faster way to do this?

>
>"Erin Gannon" <egannon@dev-archive.com> wrote:
>>
>>Do you have some SQL you want to improve, but you're not sure how?
>>
>>Erin Gannon
>>Editorial Assistant
>>
>
MarkN at 2007-11-12 >
# 3 Re: Improving SQL Queries in DB2
"Erin Gannon" <egannon@dev-archive.com> wrote:
>
>Do you have some SQL you want to improve, but you're not sure how?
>
>Erin Gannon
>Editorial Assistant
>
Hi Erin,

I want to create a Paging Mechanism for my pages. Say my table has 104 rows
and I can show only 25/page. Currently I know my upper and lower boundaries
for my filed, When loading intially I'm extracting everything, While displaying
once 25 is reached, I'm keeping the PageMin and PageMax values. When "next
page" is wanted, I'm fetching from PageMax to Upper boundry again and follow
the same steps. When going previous I'm doing the same thing, with data in
descending order and stop when the limit reaches.
Is there any better way by using the ROW_Count kinda stuff by which I can
put this logic in my SQL HAVING Clause itself ?
Sundar Yenesan at 2007-11-12 0:01:04 >