Welcome to Dream.In.Code
Getting Help is Easy!

Join 118,860 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,675 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Row Count in Oracle

 
Reply to this topicStart new topic

Row Count in Oracle

fyrestorm
post 14 Jan, 2007 - 04:27 PM
Post #1


D.I.C Lover

Group Icon
Joined: 4 Apr, 2002
Posts: 3,079



Dream Kudos: 228
My Contributions


I need to get a row count of about 20 tables in an oracle db. Some of these tables have millions of records and it would take about 30 min just to get the row count of a table with about 5 million records if I did count(*).

Is there a simpler way to get a table rowcount besides doing "SELECT COUNT(*) FROM tbl"?
User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 14 Jan, 2007 - 07:04 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 11,910



Thanked 24 times

Dream Kudos: 25
My Contributions


Try the tbl.count syntax...depends which version of Oracle you are using.

Do you have access to the system table for that db? If so, the row count for each table should be stored in it.
User is offlineProfile CardPM

Go to the top of the page

fyrestorm
post 14 Jan, 2007 - 09:42 PM
Post #3


D.I.C Lover

Group Icon
Joined: 4 Apr, 2002
Posts: 3,079



Dream Kudos: 228
My Contributions


does the system table have a specific name and do you know what column in it gives the row count?
User is offlineProfile CardPM

Go to the top of the page

fyrestorm
post 15 Jan, 2007 - 06:54 AM
Post #4


D.I.C Lover

Group Icon
Joined: 4 Apr, 2002
Posts: 3,079



Dream Kudos: 228
My Contributions


I found out that I can just do a

SELECT MAX(ROWNUM) FROM tbl

and that would return the number of rows.
User is offlineProfile CardPM

Go to the top of the page

wzeller
post 22 Jan, 2007 - 11:55 AM
Post #5


New D.I.C Head

Group Icon
Joined: 9 Apr, 2006
Posts: 39



Dream Kudos: 25
My Contributions


QUOTE(fyrestorm @ 15 Jan, 2007 - 06:54 AM) *

I found out that I can just do a

SELECT MAX(ROWNUM) FROM tbl

and that would return the number of rows.


Not quite. That will give you the number of rows used in the storage of the table. If any rows have been deleted, then you will have a higher number than the actual rows used, since any deleted rows will be empty. (Higher numbered rows are not renumbered when previous rows are deleted.)

Furthermore, subsequent inserts will use those empty rows before adding new rows. So, if you insert 100 rows into a new table and then delete rows 25-75 and then insert 15 rows, max(rownum) will give you 100 even though there are only 65 rows. And the most recently added row has a rownum of 40.

Do the tables have a primary key, like "id"? If so, count(id) might be a lot faster than count(*). Also, if you have access to it, take a look at the system table. (It's called 'system'.) Just DESC it and go from there. It will provide some good info for you.

Wayne
User is offlineProfile CardPM

Go to the top of the page

gwungfu
post 6 May, 2008 - 10:35 AM
Post #6


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 1

QUOTE(fyrestorm @ 14 Jan, 2007 - 04:27 PM) *

I need to get a row count of about 20 tables in an oracle db. Some of these tables have millions of records and it would take about 30 min just to get the row count of a table with about 5 million records if I did count(*).

Is there a simpler way to get a table rowcount besides doing "SELECT COUNT(*) FROM tbl"?


SELECT table_name, nvl(num_rows,1)
FROM dba_tables
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 01:44AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month