简体   繁体   中英

How to count records per ranged partition within a single select in oracle 12c?

The solution I'm looking for (if it exists) is to directly partition by the actual partition and not by a calculated value.

Table Pseudo-Code:

create table table_1 as (part_col DATE, val_col VARCHAR2(1))
partition by range part_col (
   PARTITION t_20160515 VALUES LESS THAN (TO_DATE('2016-05-15','YYYY-MM-DD'))
   PARTITION t_20160516 VALUES LESS THAN (TO_DATE('2016-05-16','YYYY-MM-DD'))
   ...
);

Select Pseudo-Code:

select partition as P, count(*) as C from table_1
group by partition;

Desired Result

  P        |  C
-----------|-----
2016-05-15 | 8
2016-05-16 | 99

Works but not what I'm looking for:

select trunc(part_col) P, count(*) C from table_1;

Run this to generate a set of selects for each partition

SELECT 'SELECT ' || chr(39) || partition_Name || chr(39) || ', count(*) 
        FROM ' ||table_name ||' partition (' || partition_name || 
        ') UNION ALL ' as test 
FROM all_tab_partitions 
WHERE table_Name = 'Table_1'

Results in something like:

SELECT 'P1', count(*) FROM Table_1 partition (P1) UNION ALL 
SELECT 'P2', count(*) FROM Table_1 partition (P2) UNION ALL 
SELECT 'P3', count(*) FROM Table_1 partition (P3) UNION ALL 
SELECT 'P4', count(*) FROM Table_1 partition (P4) UNION ALL 
SELECT 'P5', count(*) FROM Table_1 partition (P5) UNION ALL

Copy and paste results removing last union all and then run. Not so sure about performance... I hit a table with 5 partitions and it completed in about 2 sec total rows only around 5 mil.

The query can count the rows in each partition for a given partitioned table as below.

select table_name ,Partition_name,high_value ,
 to_number(extractvalue(xmltype(dbms_xmlgen.getxml('select /*+ parallel(a,8) */
count(*) c from '||table_name||' partition ('||partition_name||') a ')),'/ROWSET/ROW/C')) as count 
from user_tab_partitions where table_name='LINEORDER';

LINEORDER   P1  TO_DATE(' 1900-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN') 0
LINEORDER   SYS_P4365   TO_DATE(' 1996-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN') 108
LINEORDER   SYS_P4366   TO_DATE(' 1994-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN') 142
LINEORDER   SYS_P4367   TO_DATE(' 1995-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN') 186
LINEORDER   SYS_P4368   TO_DATE(' 1997-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN') 175
LINEORDER   SYS_P4369   TO_DATE(' 1998-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN') 213
LINEORDER   SYS_P4370   TO_DATE(' 1993-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN') 128
LINEORDER   SYS_P4371   TO_DATE(' 1999-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN') 47

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM