Search This Blog

Total Pageviews

Thursday 23 June 2022

Find Row Count Of All Partitions for oracle Table

Find Row Count Of All Partitions for oracle Table



--- create test table 


 https://asktom.oracle.com/pls/apex/asktom.search?tag=daywise-partition-automatically

create table sales
  ( ts   timestamp,
    id   int,
    amt  number,
    product int,
    customer int,
    item_cnt int,
    terminal int,
    operator int,
    credit_card int
      )
partition by range ( ts )
interval ( numtodsinterval(1,'HOUR') )
(
  partition p1 values less than ( timestamp '2022-04-01 00:00:00' )
);



insert /*+ APPEND */ into sales (ts,id,amt,product,customer)
   select date '2022-04-01' + rownum / 240, rownum, dbms_random.value(1,100), dbms_random.value(1,100),dbms_random.value(1,100)
   from dual
   connect by level ;

 
 
 set line 300 pagesize 300
 col high_value format a60
 col PARTITION_NAME for a20
select partition_name, high_value from dba_tab_partitions
where table_name = 'SALES'
order by partition_position;




define table_owner='SCOTT'
define TABLE_NAME='SALES'

set serverout on size 1000000
set verify off
declare
sql_stmt varchar2(1024);
row_count number;
cursor get_tab is
select table_name,partition_name
from dba_tab_partitions
where table_owner=upper('&&TABLE_OWNER') 
and table_name='&&TABLE_NAME'
;
begin
dbms_output.put_line('Checking Record Counts for table_name');
dbms_output.put_line('Log file to numrows_part_&&TABLE_OWNER.lst ....');
dbms_output.put_line('....');
for get_tab_rec in get_tab loop
BEGIN
sql_stmt := 'select count(*) from &&TABLE_OWNER..'||get_tab_rec.table_name||' partition ( '||get_tab_rec.partition_name||' )';

EXECUTE IMMEDIATE sql_stmt INTO row_count;
dbms_output.put_line('Table '||rpad(get_tab_rec.table_name||'('||get_tab_rec.partition_name||')',50)||' '||TO_CHAR(row_count)||' rows.');
exception when others then
dbms_output.put_line
('Error counting rows for table '||get_tab_rec.table_name);
END;
end loop;
end;
/
set verify on


....
Table SALES(P1)                                          0 rows.
Table SALES(SYS_P67687)                                  9 rows.
Table SALES(SYS_P67688)                                  10 rows.
Table SALES(SYS_P67689)                                  10 rows.
Table SALES(SYS_P67690)                                  10 rows.
Table SALES(SYS_P67691)                                  10 rows.



Oracle DBA

anuj blog Archive