Home » SQL & PL/SQL » SQL & PL/SQL » Table count with rows and no rows (Oracle 10.2.0.2.0 , windows)
Table count with rows and no rows [message #615437] Wed, 04 June 2014 00:37 Go to next message
Jack14
Messages: 497
Registered: December 2011
Location: INDIA
Senior Member
Hi,

There are 2000 to 3000 tables in a schema. Some tables having rows and some tables having
no rows .I want to find out and segregate how many tables have rows and how many tables
have no rows along with table name.

Is there a query to find out, if so could you share the query here?

Thanks,
Jack
Re: Table count with rows and no rows [message #615443 is a reply to message #615437] Wed, 04 June 2014 01:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

If your statistics are up to date you can use USER_TABLES.NUM_ROWS.
For a safer result you can use the following query:
select table_name,
       to_number(extractvalue(
                   dbms_xmlgen.getXMLtype ('select count(*) cnt from '||table_name),
                   '/ROWSET/ROW/CNT')) rows_in_table
from user_tables
where -- a real table
      (   tablespace_name is not null 
       or partitioned='YES' 
       or nvl(iot_type,'NOT_IOT')='IOT' )
      -- not an iot overflow
  and nvl(iot_type,'NOT_IOT') not in ('IOT_OVERFLOW','IOT_MAPPING') 
      -- not a mview log
  and (user, table_name) not in (select log_owner, log_table from user_mview_logs)
order by 1
/

which gives on ScOTT schema:
SCOTT> 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 -- a real table
  7        (   tablespace_name is not null 
  8         or partitioned='YES' 
  9         or nvl(iot_type,'NOT_IOT')='IOT' )
 10        -- not an iot overflow
 11    and nvl(iot_type,'NOT_IOT') not in ('IOT_OVERFLOW','IOT_MAPPING') 
 12        -- not a mview log
 13    and (user, table_name) not in (select log_owner, log_table from user_mview_logs)
 14  order by 1
 15  /
TABLE_NAME                     ROWS_IN_TABLE
------------------------------ -------------
BONUS                                      0
DEPT                                       4
EMP                                       14
SALGRADE                                   5

or if you just want the count of tables:
SCOTT> with 
  2    data as (
  3      select table_name,
  4             to_number(extractvalue(
  5                         dbms_xmlgen.getXMLtype ('select count(*) cnt from '||table_name),
  6                         '/ROWSET/ROW/CNT')) rows_in_table
  7      from user_tables
  8      where -- a real table
  9            (   tablespace_name is not null 
 10             or partitioned='YES' 
 11             or nvl(iot_type,'NOT_IOT')='IOT' )
 12            -- not an iot overflow
 13        and nvl(iot_type,'NOT_IOT') not in ('IOT_OVERFLOW','IOT_MAPPING') 
 14            -- not a mview log
 15        and (user, table_name) not in (select log_owner, log_table from user_mview_logs)
 16    )
 17  select decode(rows_in_table, 0, 'EMPTY', 'SOME ROWS') what,
 18         count(*) nb_tables
 19  from data
 20  group by decode(rows_in_table, 0, 'EMPTY', 'SOME ROWS') 
 21  /
WHAT       NB_TABLES
--------- ----------
SOME ROWS          3
EMPTY              1


Re: Table count with rows and no rows [message #615453 is a reply to message #615443] Wed, 04 June 2014 03:54 Go to previous messageGo to next message
deepakmannazhi
Messages: 137
Registered: February 2010
Location: Dubai, UAE
Senior Member

Dear Michel

is there any issue in using

select table_name,num_rows from user_tables



Deepak
Re: Table count with rows and no rows [message #615454 is a reply to message #615453] Wed, 04 June 2014 04:04 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

As I said, this is only valid if the statistics are up to date, otherwise you will have a snapshot at the time of theses statistics (which may not be the same one for all tables).

Re: Table count with rows and no rows [message #615455 is a reply to message #615454] Wed, 04 June 2014 04:08 Go to previous messageGo to next message
deepakmannazhi
Messages: 137
Registered: February 2010
Location: Dubai, UAE
Senior Member

Okay, Thankz. Smile
I used to use

NUM_ROWS
Re: Table count with rows and no rows [message #615457 is a reply to message #615443] Wed, 04 June 2014 04:10 Go to previous messageGo to next message
Jack14
Messages: 497
Registered: December 2011
Location: INDIA
Senior Member
Michel,
I get error when i try to run your query.
SQL>
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 -- a real table
  7          (   tablespace_name is not null
  8           or partitioned='YES'
  9           or nvl(iot_type,'NOT_IOT')='IOT' )
 10          -- not an iot overflow
 11      and nvl(iot_type,'NOT_IOT') not in ('IOT_OVERFLOW','IOT_MAPPING')
 12          -- not a mview log
 13      and (user, table_name) not in (select log_owner, log_table from user_mview_logs)
 14    order by 1
 15  /
                     dbms_xmlgen.getXMLtype ('select count(*) cnt from '||table_name),
                     *
ERROR at line 3:
ORA-19202: Error occurred in XML processing
ORA-00911: invalid character
ORA-06512: at "SYS.DBMS_XMLGEN", line 288
ORA-06512: at line 1


Could you help me to fix it ?

Regards,
Jack

Re: Table count with rows and no rows [message #615458 is a reply to message #615457] Wed, 04 June 2014 04:16 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Post the result of:
select table_name from user_tables 
where (   translate(table_name,' ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$#',' ') is not null
       or translate(substr(table_name,1,1),' ABCDEFGHIJKLMNOPQRSTUVWXYZ',' ') is not null )
  and (   tablespace_name is not null 
       or partitioned='YES' 
       or nvl(iot_type,'NOT_IOT')='IOT' )
      -- not an iot overflow
  and nvl(iot_type,'NOT_IOT') not in ('IOT_OVERFLOW','IOT_MAPPING') 
      -- not a mview log
  and (user, table_name) not in (select log_owner, log_table from user_mview_logs)
order by 1
/


[Edit: Add restrictions on table selection]

[Updated on: Wed, 04 June 2014 04:18]

Report message to a moderator

Re: Table count with rows and no rows [message #615459 is a reply to message #615458] Wed, 04 June 2014 04:19 Go to previous messageGo to next message
deepakmannazhi
Messages: 137
Registered: February 2010
Location: Dubai, UAE
Senior Member

I too have the same issue

result of the second query is Quote:
gs


If i exclude that table, then we are getting result for first query

[Updated on: Wed, 04 June 2014 04:22]

Report message to a moderator

Re: Table count with rows and no rows [message #615461 is a reply to message #615459] Wed, 04 June 2014 04:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Try:
select table_name,
       to_number(extractvalue(
                   dbms_xmlgen.getXMLtype ('select count(*) cnt from "'||table_name||'"'),
                   '/ROWSET/ROW/CNT')) rows_in_table
from user_tables
where -- a real table
      (   tablespace_name is not null
       or partitioned='YES'
       or nvl(iot_type,'NOT_IOT')='IOT' )
      -- not an iot overflow
  and nvl(iot_type,'NOT_IOT') not in ('IOT_OVERFLOW','IOT_MAPPING')
      -- not a mview log
  and (user, table_name) not in (select log_owner, log_table from user_mview_logs)
order by 1
/


And avoid the usage of lower case characters.

Re: Table count with rows and no rows [message #615463 is a reply to message #615461] Wed, 04 June 2014 04:32 Go to previous messageGo to next message
deepakmannazhi
Messages: 137
Registered: February 2010
Location: Dubai, UAE
Senior Member

Dear Michel

Now It is working. Thankz.

In fact I dont have any idea about that 'gs' table.

Somebody purposfully created that in small letters
script is like that


CREATE TABLE FZEXPRD_DBA."gs" ( "hours" NUMBER(19) NOT NULL, "min" NUMBER(5) NOT NULL, "type" VARCHAR2(21 BYTE) NOT NULL, "sys" VARCHAR2(28 BYTE) NOT NULL, "day" DATE NOT NULL ) TABLESPACE TS_OTHER_TAB PCTUSED 0 PCTFREE 10 INITRANS 1 MAXTRANS 255 STORAGE ( INITIAL 64K MINEXTENTS 1 MAXEXTENTS UNLIMITED PCTINCREASE 0 BUFFER_POOL DEFAULT ) LOGGING NOCOMPRESS NOCACHE NOPARALLEL MONITORING;







Re: Table count with rows and no rows [message #615467 is a reply to message #615463] Wed, 04 June 2014 04:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

The previous query I gave allows to find all this kind of tables.
You can write the same kind of query to check column names.

Re: Table count with rows and no rows [message #615468 is a reply to message #615467] Wed, 04 June 2014 04:45 Go to previous messageGo to next message
deepakmannazhi
Messages: 137
Registered: February 2010
Location: Dubai, UAE
Senior Member

Yes, I will follow.

Thankz Michel Smile
Re: Table count with rows and no rows [message #615477 is a reply to message #615468] Wed, 04 June 2014 08:53 Go to previous messageGo to next message
Jack14
Messages: 497
Registered: December 2011
Location: INDIA
Senior Member
Michel,

I am gathering statistics for entire schema.Its still running and would update when i am able to see the output.

Thanks,
Jack
Re: Table count with rows and no rows [message #615481 is a reply to message #615477] Wed, 04 June 2014 09:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Do you gather complete statistics or estimate only?
Which command do you use?

Re: Table count with rows and no rows [message #615548 is a reply to message #615481] Thu, 05 June 2014 05:53 Go to previous message
Jack14
Messages: 497
Registered: December 2011
Location: INDIA
Senior Member
Michel,

Here is the command i used to gather schema stats.
begin
DBMS_STATS.GATHER_SCHEMA_STATS( 'PIRM_OWNER'); 
end;

Currently all table stats are up to date after i gathered stats yesterday.
Your query is useful.Thank you

Regds,
Jack

[Updated on: Thu, 05 June 2014 05:55]

Report message to a moderator

Previous Topic: Create Sql Query
Next Topic: Insert with different key values to avoid contraint violation
Goto Forum:
  


Current Time: Wed Apr 17 22:46:37 CDT 2024