Home » SQL & PL/SQL » SQL & PL/SQL » need to get row count for database with 20 k tables (oracle 11 g)
need to get row count for database with 20 k tables [message #650297] Thu, 21 April 2016 22:26 Go to next message
TorontoTrader
Messages: 8
Registered: January 2007
Junior Member
i built a p/l sql script but i am guessing that there is a better way to do it than what i am trying to do. my script loops thro all the tables in all schemas except system tables and does a select count(1) and stores it another table. do you have any ideas or suggestions. we need to do this as we are loading data daily and need to keep some metrics on the # of rows daily

declare 
  -- Local variables here
  i integer;
   query_str VARCHAR2(1000);
   tablenames varchar2(200);
   err_msg VARCHAR2(100);
c integer;

begin
  -- Test statements here
 execute immediate  'truncate table TABLE_LIST ';
  for tab_name in (select  distinct owner,table_name from dba_tables where owner not in ('SYS' ,'SYSTEM') AND TABLESPACE_NAME NOT IN ('SYSTEM') AND TABLE_NAME not like '%$%')
     
loop 
  begin
  tablenames:= tab_name.owner|| '.'||  tab_name.table_name;
  query_str := ' select count(1) from ' || tablenames;
 --dbms_output.put_line(   query_str )  ;
 EXECUTE IMMEDIATE query_str INTO c ;
  
 insert into TABLE_LIST 
   (table_name,row_count)
 values
   (tablenames,c);
    commit;
  exception 
    when others then
      dbms_output.put_line(SQLCODE);
       dbms_output.put_line( SUBSTR(SQLERRM, 1, 100));
      dbms_output.put_line(  tablenames|| ','||  c  );
      end;
      
--


 end loop;
 
  
end;
Re: need to get row count for database with 20 k tables [message #650298 is a reply to message #650297] Thu, 21 April 2016 22:31 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
https://asktom.oracle.com/pls/asktom/f?p=100:11:3342464118875057::::P11_QUESTION_ID:1660875645686
Re: need to get row count for database with 20 k tables [message #650299 is a reply to message #650298] Thu, 21 April 2016 22:45 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>we need to do this as we are loading data daily and need to keep some metrics on the # of rows daily
You should COUNT() the rows as they are loaded, but I question the usefulness of this whole exercise.
How close is close enough for the row counts?
Re: need to get row count for database with 20 k tables [message #650301 is a reply to message #650297] Fri, 22 April 2016 00:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

http://www.orafaq.com/forum/m/615461/?srch=dbms_xmlgen.getXMLtype+count#msg_615461

Re: need to get row count for database with 20 k tables [message #650306 is a reply to message #650301] Fri, 22 April 2016 03:05 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And stop using count(1). There's a long standing myth that it's faster than count(*). It's not and oracle internally converts count(1) to count(*). So just always use count(*).
Re: need to get row count for database with 20 k tables [message #650318 is a reply to message #650306] Fri, 22 April 2016 06:28 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
select  distinct owner,table_name from dba_tables where owner not in ('SYS' ,'SYSTEM') AND TABLESPACE_NAME NOT IN ('SYSTEM') AND TABLE_NAME not like '%$%'


Get rid of distinct. Owner, table name combination is unique. There is ton of Oracle supplied tables that match your SQL:

SQL> select  nvl(owner,'Total') owner,
  2          count(*)
  3    from  dba_tables
  4    where owner not in ('SYS' ,'SYSTEM')
  5      AND TABLESPACE_NAME NOT IN ('SYSTEM')
  6      AND TABLE_NAME not like '%$%'
  7      AND OWNER NOT IN ('SCOTT','HR')
  8    group by rollup(owner)
  9  /

OWNER                            COUNT(*)
------------------------------ ----------
APEX_040200                           404
APPQOSSYS                               4
CTXSYS                                  2
DBSNMP                                 16
GSMADMIN_INTERNAL                      13
MDSYS                                  82
OLAPSYS                                 2
ORDDATA                                71
ORDSYS                                  5
WMSYS                                   1
XDB                                     5
Total                                 605

12 rows selected.

SQL>  


Also, your query will return (if you have any) auxiliary tables like materialized view logs, IOT overflow tables, Oracle text index tables, etc. you need to exclude.

Do you gather stats on your tables? If so, selecting NUM_ROWS from DBA_TABLES right after collecting stats will give you what you need.

SY.

[Updated on: Fri, 22 April 2016 06:38]

Report message to a moderator

Re: need to get row count for database with 20 k tables [message #650319 is a reply to message #650297] Fri, 22 April 2016 06:34 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
Why do you start by truncating TABLE_LIST? With that, you are keeping no history, so what's the point if the entire exercise?
In spite of your assertion that you "need to keep some metrics on the # of rows daily", you are not keeping anything.
Re: need to get row count for database with 20 k tables [message #650320 is a reply to message #650297] Fri, 22 April 2016 06:41 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member

  exception 
    when others then
      dbms_output.put_line(SQLCODE);
       dbms_output.put_line( SUBSTR(SQLERRM, 1, 100));
      dbms_output.put_line(  tablenames|| ','||  c  );
      end;



A huge bug in your code. dbms_output.put_line simply writes to a buffer. It is up to the client process to access that buffer and display the contents. Unless you are running this procedure interactively (!) you'll never see any errors that get reported this way.
Re: need to get row count for database with 20 k tables [message #650322 is a reply to message #650318] Fri, 22 April 2016 09:17 Go to previous messageGo to next message
TorontoTrader
Messages: 8
Registered: January 2007
Junior Member
How can i filter out all the system tables and materialized views from dba_Tables?
select owner,table_name from dba_tables where owner not in ('SYS' ,'SYSTEM') AND TABLESPACE_NAME NOT IN ('SYSTEM') AND TABLE_NAME not like '%$%'
Re: need to get row count for database with 20 k tables [message #650325 is a reply to message #650322] Fri, 22 April 2016 09:53 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
select owner, table_name, num_rows from all_tables;
above will be MUCH, much faster than you PL/SQL code.
Re: need to get row count for database with 20 k tables [message #650327 is a reply to message #650322] Fri, 22 April 2016 09:56 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
You need to join to DBA_OBJECTS.

SY.
Re: need to get row count for database with 20 k tables [message #650332 is a reply to message #650327] Fri, 22 April 2016 10:16 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
I would do the following steps.

1) do NOT truncate the table, keep the history. Make your table TABLE_LIST with the following columns
owner varchar2(30)
table_name varchar2(30)
tablespace_name varchar2(30)
num_rows number
create_date date

create a unique index on owner,table_name,create_date

2) Run your daily database wide statistics run, this should be done and will populate the num_rows with the correct values

3) Run the following query. I would put it in the same job that ran the statistics update

insert into TABLE_LIST
select A.owner,A.table_name,a.tablespace_name,NUM_ROWS,TRUNC(SYSDATE) RUN_DATE
from DBA_OBJECTS B,dba_tables A
where A.owner not in ('SYS' ,'SYSTEM')
AND A.TABLE_NAME = B.OBJECT_NAME
AND A.OWNER = B.OWNER
AND B.OBJECT_TYPE = 'TABLE'
AND A.TABLESPACE_NAME NOT IN ('SYSTEM')
AND A.TABLE_NAME not like '%$%';

commit;

[Updated on: Fri, 22 April 2016 11:50]

Report message to a moderator

Re: need to get row count for database with 20 k tables [message #650342 is a reply to message #650332] Fri, 22 April 2016 15:14 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
This will not eliminate materialized views, just materialized view logs:
SQL>  create materialized view emp_mv as select * from emp;

Materialized view created.

SQL> select  object_type
  2    from  user_objects
  3    where object_name = 'EMP_MV'
  4  /

OBJECT_TYPE
-----------------------
MATERIALIZED VIEW
TABLE

SQL> 


It should be something like:

AND (OWNER,TABLE_NAME) IN (
                           SELECT  B.OWNER,
                                   B.TABLE_NAME
                             FROM  DBA_OBJECTS B
                             WHERE B.OBJECT_TYPE != 'MATERIALIZED VIEW'
                         )


Also, OP needs to add

AND A.SECONDARY != 'Y' -- This will eliminate domain index tables
AND NVL(A.IOT_TYPE,'NOT_IOT') NOT IN ('IOT_OVERFLOW','IOT_MAPPING') -- This will eliminate IOT overflow and mapping tables
AND A.TEMPORARY != 'Y' -- This will eliminate GTT


to main query.

SY.
Previous Topic: Dynamic SQL - with CONCAT function
Next Topic: with clause logic
Goto Forum:
  


Current Time: Wed Apr 24 23:33:08 CDT 2024