dbTalk Databases Forums  

number of rows in cursor?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss number of rows in cursor? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
eudorica
 
Posts: n/a

Default number of rows in cursor? - 12-10-2004 , 02:45 AM






Is there way to count a number of rows in opened cursor before fetching it
(%rowcount works only after fetching)? I need to know the number of rows
before enter loop. Thanks.



Reply With Quote
  #2  
Old   
Jim Kennedy
 
Posts: n/a

Default Re: number of rows in cursor? - 12-10-2004 , 06:28 AM







"eudorica" <snjezana.katusic (AT) htnet (DOT) hr> wrote

Quote:
Is there way to count a number of rows in opened cursor before fetching it
(%rowcount works only after fetching)? I need to know the number of rows
before enter loop. Thanks.


No, you have to fetch the data.
Jim




Reply With Quote
  #3  
Old   
Frank Piron
 
Posts: n/a

Default Re: number of rows in cursor? - 12-10-2004 , 06:54 AM



Am Fri, 10 Dec 2004 09:45:50 +0100 schrieb eudorica
<snjezana.katusic (AT) htnet (DOT) hr>:

Quote:
Is there way to count a number of rows in opened cursor before fetching
it
(%rowcount works only after fetching)? I need to know the number of rows
before enter loop. Thanks.


You may count the rows explicitly before entering the loop.

if the cursor statement is: select <projection-clause>
from ....

you may count with: select count(rowid) into <counter_var>
from ....

where <counter_var> is some local variable into which you may
store the number of rows the cursor will retrieve.

--
Frank Piron,
defrankatkonaddot
(leftrotate two)


Reply With Quote
  #4  
Old   
Jim Kennedy
 
Posts: n/a

Default Re: number of rows in cursor? - 12-10-2004 , 07:11 AM




"Frank Piron" <empty (AT) zero (DOT) nil> wrote

Quote:
Am Fri, 10 Dec 2004 09:45:50 +0100 schrieb eudorica
snjezana.katusic (AT) htnet (DOT) hr>:

Is there way to count a number of rows in opened cursor before fetching
it
(%rowcount works only after fetching)? I need to know the number of rows
before enter loop. Thanks.



You may count the rows explicitly before entering the loop.

if the cursor statement is: select <projection-clause
from ....

you may count with: select count(rowid) into <counter_var
from ....

where <counter_var> is some local variable into which you may
store the number of rows the cursor will retrieve.

--
Frank Piron,
defrankatkonaddot
(leftrotate two)
Except the number could change from count to the fetch of the data.
Jim




Reply With Quote
  #5  
Old   
Frank Piron
 
Posts: n/a

Default Re: number of rows in cursor? - 12-10-2004 , 08:14 AM



Am Fri, 10 Dec 2004 13:11:32 GMT schrieb Jim Kennedy
<kennedy-downwithspammersfamily (AT) attbi (DOT) net>:

Quote:
"Frank Piron" <empty (AT) zero (DOT) nil> wrote in message
newspsisl5wh3m0et4w (AT) news (DOT) online.de...
Am Fri, 10 Dec 2004 09:45:50 +0100 schrieb eudorica
snjezana.katusic (AT) htnet (DOT) hr>:

Is there way to count a number of rows in opened cursor before
fetching
it
(%rowcount works only after fetching)? I need to know the number of
rows
before enter loop. Thanks.



You may count the rows explicitly before entering the loop.

if the cursor statement is: select <projection-clause
from ....

you may count with: select count(rowid) into <counter_var
from ....

where <counter_var> is some local variable into which you may
store the number of rows the cursor will retrieve.

--
Frank Piron,
defrankatkonaddot
(leftrotate two)
Except the number could change from count to the fetch of the data.
Sure. But this is inevitable.

--
Frank Piron,
defrankatkonaddot
(leftrotate two)


Reply With Quote
  #6  
Old   
Anurag Varma
 
Posts: n/a

Default Re: number of rows in cursor? - 12-10-2004 , 05:30 PM




"Frank Piron" <empty (AT) zero (DOT) nil> wrote

Quote:
Am Fri, 10 Dec 2004 13:11:32 GMT schrieb Jim Kennedy
kennedy-downwithspammersfamily (AT) attbi (DOT) net>:


"Frank Piron" <empty (AT) zero (DOT) nil> wrote in message
newspsisl5wh3m0et4w (AT) news (DOT) online.de...
Am Fri, 10 Dec 2004 09:45:50 +0100 schrieb eudorica
snjezana.katusic (AT) htnet (DOT) hr>:

Is there way to count a number of rows in opened cursor before
fetching
it
(%rowcount works only after fetching)? I need to know the number of
rows
before enter loop. Thanks.



You may count the rows explicitly before entering the loop.

if the cursor statement is: select <projection-clause
from ....

you may count with: select count(rowid) into <counter_var
from ....

where <counter_var> is some local variable into which you may
store the number of rows the cursor will retrieve.

--
Frank Piron,
defrankatkonaddot
(leftrotate two)
Except the number could change from count to the fetch of the data.

Sure. But this is inevitable.

An approximate count of the number of rows that will be returned can also found by
using CBO, analyzing and doing an explain plan. Not a very precise method .. but definitely worth looking into
if performance is critical.

Tom Kyte mentions this in his site: search for "estimated cardinality" or "pagination"
on http://asktom.oracle.com

Anurag




Reply With Quote
  #7  
Old   
Sybrand Bakker
 
Posts: n/a

Default Re: number of rows in cursor? - 12-11-2004 , 03:55 AM



On Fri, 10 Dec 2004 15:14:28 +0100, Frank Piron <empty (AT) zero (DOT) nil>
wrote:

Quote:
You may count the rows explicitly before entering the loop.

if the cursor statement is: select <projection-clause
from ....

you may count with: select count(rowid) into <counter_var
from ....

where <counter_var> is some local variable into which you may
store the number of rows the cursor will retrieve.

--
Frank Piron,
defrankatkonaddot
(leftrotate two)
Except the number could change from count to the fetch of the data.

Sure. But this is inevitable.

--
Frank Piron,
defrankatkonaddot
(leftrotate two)

As your advice also effectively selects the data twice, and
consequently consumes twice as much resources, I wouldn't recommend
this 'workaround' to anyone.


--
Sybrand Bakker, Senior Oracle DBA


Reply With Quote
  #8  
Old   
ak_tiredofspam@yahoo.com
 
Posts: n/a

Default Re: number of rows in cursor? - 12-11-2004 , 09:47 PM



there is no efficient way, you need to run a COUNT(*) query before.
What is your problem? Maybe there is another solution other than SELECT
COUNT(*) ...


Reply With Quote
  #9  
Old   
Cile
 
Posts: n/a

Default Re: number of rows in cursor? - 12-12-2004 , 03:24 AM



ak_tiredofspam (AT) yahoo (DOT) com wrote:
Quote:
there is no efficient way, you need to run a COUNT(*) query before.
What is your problem? Maybe there is another solution other than SELECT
COUNT(*) ...

I need to send number of steps to outside code. Select count(*) is no
good for performance. Thanks anyway!


Reply With Quote
  #10  
Old   
Mark C. Stock
 
Posts: n/a

Default Re: number of rows in cursor? - 12-12-2004 , 08:04 AM




"Cile" <cile (AT) hi (DOT) htnet.hr> wrote

Quote:
ak_tiredofspam (AT) yahoo (DOT) com wrote:
there is no efficient way, you need to run a COUNT(*) query before.
What is your problem? Maybe there is another solution other than SELECT
COUNT(*) ...

I need to send number of steps to outside code. Select count(*) is no
good for performance. Thanks anyway!

do this:

select just the rowid's in to a pl/sql table variable (bulk collect for
performance)

examine the count to determine how to proceed

fetch the actual data using rowids in the pl/sql table variable (bulk bind
for performance)

depending, however, on what you're fetching (number and size of rows) it
might be cheaper to just bulk collect the rows into a pl/sql table variable,
examine the count, then loop thru the pl/sql table variable

++ mcs




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.