View previous topic :: View next topic |
Author |
Message |
20cents Beginner

Joined: 26 Oct 2005 Posts: 25 Topics: 7 Location: Between Heaven and Hell
|
Posted: Wed Apr 05, 2006 7:01 am Post subject: Use of indexes in DECLARE cursor |
|
|
Hi,
Good day. I don't mean to be rude, but let me get straight to the point.
I have a table which has 4 indexes - ACCTNO, YEAR, MONTH, and TIMEADD
ACCTNO - account number
YEAR - format CCYY
MONTH - format MM
TIMEADD - timestamp
I would like to make use of all 4 indexes to make my processing faster, since the table has billions of records in it, and I may retrieve thousands for a particular account.
here's the problem. I need to get 10 months worth of data for the account. For example, this month 04/2006, for a particular account, I need to get from 06/2005. How do I declare the cursor such that I would get only from 06/2005 to 04/2006?
The only way I could think of is putting the year and month in an array, but then I need to declare-fetch-close 10 times to retrieve the data I need.
Such that it would look like this:
Loop n from 1 to 10:
1. declare cursor where MONTH=:array_month[n] and YEAR=:array_year[n] and ACCTNO=:acctnumber and TIMEADD < current timestamp
2. fetch
3. close cursor
Is there some way I could avoid this?
I hope this explanation is clear enough by you, my masters. |
|
Back to top |
|
 |
20cents Beginner

Joined: 26 Oct 2005 Posts: 25 Topics: 7 Location: Between Heaven and Hell
|
Posted: Wed Apr 05, 2006 7:05 am Post subject: |
|
|
Forgot to include what the array contains:
array_month[1] = 06; array_year[1] = 2005
array_month[2] = 07; array_year[2] = 2005
array_month[3] = 08; array_year[3] = 2005
array_month[4] = 09; array_year[4] = 2005
array_month[5] = 10; array_year[5] = 2005
array_month[6] = 11; array_year[6] = 2005
array_month[7] = 12; array_year[7] = 2005
array_month[8] = 01; array_year[8] = 2006
array_month[9] = 02; array_year[9] = 2006
array_month[10] = 03; array_year[10] = 2006 |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Wed Apr 05, 2006 7:23 am Post subject: |
|
|
20cents,
You don't need an array or any thing. You can set up the host-variables to have the date range and use them in the cursor declaration
Code: |
SELECT YEAR(CURRENT DATE - 10 MONTHS)
,MONTH(CURRENT DATE - 10 MONTHS)
,YEAR(CURRENT DATE)
,MONTH(CURRENT DATE)
INTO :W-START-YEAR
,:W-START-MNTH
,:W-END-YEAR
,:W-END-MNTH
FROM SYSIBM.SYSDUMMY1
|
The output of this query is
Now use this information and open the cursor declared as below.
Code: |
SELECT *
FROM TABLE
WHERE ACT_NO = :W-ACT-NO
AND ((YEAR_COL = :W-START-YEAR AND
MONTH_COL >= :W-START-MNTH) OR
(YEAR_COL = :W-END-YEAR AND
MONTH_COL <= :W-END-MNTH))
|
now you got the 10 months worth of data and all you need to do is fetch until you sqlcode of 100.
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
20cents Beginner

Joined: 26 Oct 2005 Posts: 25 Topics: 7 Location: Between Heaven and Hell
|
Posted: Wed Apr 05, 2006 7:35 pm Post subject: |
|
|
thanks, my master. |
|
Back to top |
|
 |
20cents Beginner

Joined: 26 Oct 2005 Posts: 25 Topics: 7 Location: Between Heaven and Hell
|
Posted: Thu Apr 06, 2006 6:47 pm Post subject: |
|
|
kolusu wrote: | 20cents,
Now use this information and open the cursor declared as below.
Code: |
SELECT *
FROM TABLE
WHERE ACT_NO = :W-ACT-NO
AND ((YEAR_COL = :W-START-YEAR AND
MONTH_COL >= :W-START-MNTH) OR
(YEAR_COL = :W-END-YEAR AND
MONTH_COL <= :W-END-MNTH))
|
now you got the 10 months worth of data and all you need to do is fetch until you sqlcode of 100.
Hope this helps...
Cheers
Kolusu |
Kolusu,
Our DBA, tested select query above and it ended in a scan, thus not using the index. anyway we couls work around this?
20cents |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Fri Apr 07, 2006 5:16 am Post subject: |
|
|
20cents,
How are year and month col defined? Another option is to add the timestamp into the sql
Code: |
W-START-DAY PIC X(02) VALUE '01'
W-TSTMP-REST PIC X(16) VALUE '-00.00.00.000000'
|
Concatenate the start year and month portion the above fields and generate it like a timestamp field and use it in the query as
Code: |
AND TIMEADD >= :W-STMP-FIELD
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
|
|