MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Use of indexes in DECLARE cursor

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
20cents
Beginner


Joined: 26 Oct 2005
Posts: 25
Topics: 7
Location: Between Heaven and Hell

PostPosted: Wed Apr 05, 2006 7:01 am    Post subject: Use of indexes in DECLARE cursor Reply with quote

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
View user's profile Send private message Yahoo Messenger MSN Messenger
20cents
Beginner


Joined: 26 Oct 2005
Posts: 25
Topics: 7
Location: Between Heaven and Hell

PostPosted: Wed Apr 05, 2006 7:05 am    Post subject: Reply with quote

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
View user's profile Send private message Yahoo Messenger MSN Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Wed Apr 05, 2006 7:23 am    Post subject: Reply with quote

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
Code:

2005
06
2006
04


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
View user's profile Send private message Send e-mail Visit poster's website
20cents
Beginner


Joined: 26 Oct 2005
Posts: 25
Topics: 7
Location: Between Heaven and Hell

PostPosted: Wed Apr 05, 2006 7:35 pm    Post subject: Reply with quote

thanks, my master.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
20cents
Beginner


Joined: 26 Oct 2005
Posts: 25
Topics: 7
Location: Between Heaven and Hell

PostPosted: Thu Apr 06, 2006 6:47 pm    Post subject: Reply with quote

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
View user's profile Send private message Yahoo Messenger MSN Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Apr 07, 2006 5:16 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group