Home » RDBMS Server » Server Administration » no. of records in a table: count(*) or num_rows (Oracle 7.3.4, Solaris 8)
no. of records in a table: count(*) or num_rows [message #494349] Sun, 13 February 2011 19:09 Go to next message
reym21
Messages: 241
Registered: February 2010
Location: Philippines
Senior Member

Sirs/ Madame:

Please enlighten me with these 2 SQL statements in getting the
actual record count of a certain table.

1) select count(*) from MYTABLE1;

or

2) select num_rows from DBA_TABLES
where table_name = 'MYTABLE1';

Is it possible that this may produce discrepancies?

Thank you in advance.
Re: no. of records in a table: count(*) or num_rows [message #494351 is a reply to message #494349] Sun, 13 February 2011 19:16 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>Is it possible that this may produce discrepancies?
The values returned will be "close" to each other for some definition of close.
Only when the table is static will both values be the same.

>1) select count(*) from MYTABLE1;
above will be real time correct; but can consume noticeable resources.

>2) select num_rows from DBA_TABLES where table_name = 'MYTABLE1';
NUM_ROWS is updated what statistics are collected.
Re: no. of records in a table: count(*) or num_rows [message #494352 is a reply to message #494349] Sun, 13 February 2011 19:19 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
try a test such as this:

create table t1 as select * from all_users;
select count(*) from t1;
select num_rows from user_tables where table_name='T1';
analyze table t1 compute statistics;
select count(*) from t1;
select num_rows from user_tables where table_name='T1';
delete from t1;
commit;
select count(*) from t1;
select num_rows from user_tables where table_name='T1';
analyze table t1 compute statistics;
select count(*) from t1;
select num_rows from user_tables where table_name='T1';

what do you think?

[update: typo]

[Updated on: Sun, 13 February 2011 19:20]

Report message to a moderator

Re: no. of records in a table: count(*) or num_rows [message #494357 is a reply to message #494352] Sun, 13 February 2011 20:07 Go to previous messageGo to next message
reym21
Messages: 241
Registered: February 2010
Location: Philippines
Senior Member

Thanks for this.

But, are you saying that the 'analyze table compute statistics' will be the answer for such discrepancies?

I raised this topic because of the discrenpancies we've noted when our outsourcer executed their db reorganization. We have a script of 'after' and 'before' that take the snapshot of a database. In our script, we use the num_rows for the table while our outsourcer use their own script with 'count(*)'.

Right now, as I checked again, using both 'num_rows' and 'count(*)' the count were equal.

Thanks again.


Re: no. of records in a table: count(*) or num_rows [message #494358 is a reply to message #494351] Sun, 13 February 2011 20:10 Go to previous messageGo to next message
reym21
Messages: 241
Registered: February 2010
Location: Philippines
Senior Member

Can you help me to know if tables are in their 'static' status?

I raised this topic because of the discrenpancies we've noted when our outsourcer executed their db reorganization activities. We have a script of 'after' and 'before' that take the snapshot of a database. In our script, we use the 'num_rows' for the table while our outsourcer use their own script with 'count(*)'.

Thanks a lot.
Re: no. of records in a table: count(*) or num_rows [message #494359 is a reply to message #494358] Sun, 13 February 2011 20:19 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>Can you help me to know if tables are in their 'static' status?
a table is STATIC when no DELETE or INSERT against table occurs (the number of rows in the table does not change).

>I raised this topic because of the discrenpancies we've noted when our outsourcer executed their db reorganization activities.
I would expect discrepancies to exist PRIOR to the start of the "reorganization activities".
If so, the same discrepancies remain after the "reorganization activities".

[Updated on: Sun, 13 February 2011 20:25]

Report message to a moderator

Re: no. of records in a table: count(*) or num_rows [message #494361 is a reply to message #494358] Sun, 13 February 2011 20:23 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
You might want to ask your outsourcer why he is re-organizing anything. Even with your twentieth century release of Oracle, re-organizing was never necessary if the database were administered properly.
Re: no. of records in a table: count(*) or num_rows [message #494371 is a reply to message #494361] Mon, 14 February 2011 00:00 Go to previous messageGo to next message
reym21
Messages: 241
Registered: February 2010
Location: Philippines
Senior Member

Reorganization because of data purging they've made.

BTW, we've been stuck with our issue of using the 'num_rows' column in extracting the actual count of tables per schema...
This is our script:

REM GET ROWCOUNT OF ALL TABLES
select TO_CHAR(SysDate,'Mon/DD/YYYY HH:MI P.M.') todays_date from dual;
SPOOL /path/.../SITE1_tab_rowcnt.csv
prompt =============================================
prompt TABLES ROWCOUNT (ABC, CDE, STU) FOR SITE1
prompt =============================================
break on owner on report
compute sum of cnt on owner
compute sum of cnt on report
select owner, table_name, num_rows
from dba_tables
where owner in ('ABC', 'CDE', 'STU')
order by owner, table_name;
spool off

.. said script produce this output:
ABC ABC_ACTUAL_PARAMETERS 1477893
ABC_ADHOC_SQLS 1
ABC_BATCH_DEFINITIONS 34
CDE CDE_CLIENTS 2192402
CDE_SELECT_DISCRYS 1417
CDE_UPLOAD_TEMP 0
STU STU_DOCS_INVENTORY_DTLS 104
STU_DOCS_LOOSE_DTLS 149
STU_DOCS_LOOSE_DTLS_TRANS 181

How can we modify this script with the same output, but using the of 'count(*)' syntax? Everytime we include the 'count(*)' from our select command it gave us this error: "ORA-00937: not a single-group group function".
Re: no. of records in a table: count(*) or num_rows [message #494376 is a reply to message #494371] Mon, 14 February 2011 01:01 Go to previous messageGo to next message
Michel Cadot
Messages: 68648
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
get row counts of tables in schema without using dba_tables.num_rows

Regards
Michel
Re: no. of records in a table: count(*) or num_rows [message #494382 is a reply to message #494376] Mon, 14 February 2011 02:11 Go to previous messageGo to next message
reym21
Messages: 241
Registered: February 2010
Location: Philippines
Senior Member

thanks for this.

But I'm getting the error when I try executing the script
(in Oracle 7.3.4)

SQL:-) select table_name,
2 to_number(extractvalue(
3 dbms_xmlgen.getXMLtype ('select count(*) cnt from '||table_name),
4 '/ROWSET/ROW/CNT')) rows_in_table
5 from user_tables
6 where tablespace_name is not null or partitioned='YES'
7 order by 1
8 /
where tablespace_name is not null or partitioned='YES'
*
ERROR at line 6:
ORA-00904: invalid column name

(in Oracle 9i)


SQL> select table_name,
2 to_number(extractvalue(
3 dbms_xmlgen.getXMLtype ('select count(*) cnt from '||table_name),
4 '/ROWSET/ROW/CNT')) rows_in_table
5 from user_tables
6 where tablespace_name is not null or partitioned='YES'
7 order by 1
8 /
to_number(extractvalue(
*
ERROR at line 2:
ORA-00904: invalid column name
Re: no. of records in a table: count(*) or num_rows [message #494384 is a reply to message #494382] Mon, 14 February 2011 02:26 Go to previous messageGo to next message
Littlefoot
Messages: 21809
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Well, your database version (7.3) might be the reason, not knowing "advanced" syntax those queries use. You might need to rewrite them, using "classic" dynamic queries.
Re: no. of records in a table: count(*) or num_rows [message #494395 is a reply to message #494384] Mon, 14 February 2011 03:47 Go to previous message
cookiemonster
Messages: 13922
Registered: September 2008
Location: Rainy Manchester
Senior Member
Considering your ancient version of the DB (you do know oracle desupported that a decade ago?) the following is probably your best bet:
set head off
set feed off
set echo off
set verify off
set lines 100
spool counts.sql
SELECT 'SELECT '''||table_name||''' table_name, count(*) FROM '||table_name||';' 
FROM user_tables;

Run that in sqlplus, then run the resulting file in sqlplus.
Previous Topic: Logminer Dictionary file- Large in Size
Next Topic: Oracle Trace
Goto Forum:
  


Current Time: Wed May 08 21:13:16 CDT 2024