Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify a table with maximum rows in Oracle

Tags:

oracle

plsql

I have a set of tables in Oracle and I would like to identify the table that contains the maximum number of rows.

So if, A has 200 rows, B has 345 rows and C has 120 rows I want to be able to identify table B.

Is there a simple query I can run to achieve this?

Edit: There are 100 + tables so I am looking for something generic.

like image 520
Preets Avatar asked Dec 24 '08 07:12

Preets


People also ask

What is the maximum number of rows in Oracle table?

3 bytes are used to store the page number a maximum of 16,777,215 pages can be used in one table. 1 byte is used to store the slot number, a single page can have at most 255 slots/rows. The maximum number of rows in a table or fragment is 4,278,189,825.

How do you check the size of a table in Oracle?

select segment_name,segment_type, sum(bytes/1024/1024/1024) GB from dba_segments where segment_name='&Your_Table_Name' group by segment_name,segment_type; Storage.


2 Answers

Given that you said you were using Oracle I would just query the meta-data.

select table_name, max(num_rows) from all_tables where table_name in ('A', 'B', 'C');

Just saw your edit. Just run the above without the where clause and it will return the largest table in the database. Only problem may be that you might get a SYS$ table or something. Alternately if you are just doing this for your own knowledge just do

select table_name, num_rows from all_tables order by num_rows; 

and you'll see what the biggest are.

like image 135
Dan Avatar answered Oct 12 '22 03:10

Dan


The table in your schema that has max rows:

with data as 
(
 select table_name,
        to_number(extractvalue(xmltype(
                  dbms_xmlgen.getxml (
                 ' select count(*) c from ' || table_name)),
                  '/ROWSET/ROW/C')) countrows
 from   user_tables
)
select table_name, countrows
from   data 
where  countrows = (select max(countrows)
                    from   data);

dbms_xmlgen.getxml(' select .... ') is extremely flexible.

like image 36
tuinstoel Avatar answered Oct 12 '22 03:10

tuinstoel