Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Juan Carlos.

Asked: March 29, 2001 - 9:13 am UTC

Last updated: September 16, 2008 - 10:21 pm UTC

Version: 8.1.6

Viewed 1000+ times

You Asked

Hi Tom:

Thank you very much for answering all my previous questions.

I have been trying to find a sample code of how to use the Pro*C directive "PREFETCH" with no results yet.
Could you provide me with some of it, using it inside the program, i.e. before opening the cursor versus in a configuration file.

Thanks again,
Juan Carlos.

and Tom said...

Prefetch is a new 8i feature whereby the client will automagically array fetch for you -- you don't have to do array fetching anymore, it'll just happen.

You use it the same way you would ANY of the options for. Typically it is used on the command line

$ proc ... prefetch=N ...

but you can embedded it in your program like this:

main( argc, argv )
int argc;
char * argv[];
{
exec sql begin declare section;
char oracleid[500]="scott/tiger";
int empno;
exec sql end declare section;


EXEC SQL WHENEVER SQLERROR DO sqlerror_hard();
EXEC SQL WHENEVER SQLWARNING DO sqlerror_hard();
EXEC SQL CONNECT :oracleid;
exec sql alter session set sql_trace=true;


exec sql declare c1 cursor for select empno from emp A;
exec sql declare c2 cursor for select empno from emp B;

exec oracle option( prefetch=5 );

exec sql open c1;
for( ;; )
{
exec sql whenever notfound do break;
exec sql fetch c1 into :empno;
}
exec sql close c1;

exec oracle option( prefetch=50 );


exec sql open c2;
for( ;; )
{
exec sql whenever notfound do break;
exec sql fetch c2 into :empno;
}
exec sql close c2;

exec sql commit work release;
}

and then to verify it worked -- I'll run tkprof on the trace file:


select empno
from
emp A


call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 3 0.00 0.00 0 3 4 15
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 5 0.00 0.00 0 3 4 15

select empno
from
emp B


call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 1 0.00 0.00 0 1 4 15
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 3 0.00 0.00 0 1 4 15

and sure enough -- the first cursor was actually fetched from 3 times for 15 rows and the second one fetched from only once since the array size was changed.






Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Multiple settting of prefetch is very interesting

Harrison Picot, September 18, 2001 - 12:19 am UTC

Thanks Tom, resetting prefetch in the middle of the code points out an interesting ability. I am not sure where it will come in handy but everything seems to. The example is really good, a reminder of how to test. Prefetch is definitely a valuable tool, both for traffic reasons on high speed connections, and time on low speed. This is much better than the documentation, which give prefetch short shrift.

Thanks.

Mike

Mike Cameron, September 15, 2008 - 11:27 pm UTC

Hi Tom,

This is an old thread but is very relevent to my question:

DB: 8.1.7.4
Pro*C: 8.1.7


Does the PREFETCH option have the same / better / worse impact of including a ROWNUM on the cursor statement...?

I.e.
=================================
exec sql declare c1 cursor for select empno from emp A where rownum <= 201;
exec sql declare c2 cursor for select empno from emp B;

exec oracle option( prefetch=201 );
exec sql open c1;
for( ;; )
{...
......
=================================

Thanks in advance,
Mike
Tom Kyte
September 16, 2008 - 10:21 pm UTC

it would not matter - if the cursor with or without the rownum returned 201 records that is.

rownum won't make prefetch go faster, or slower.