如何计算oracle 12c中单个选择内的每个远程分区的记录?

时间:2016-08-16 17:13:57

标签: sql oracle oracle12c

我正在寻找的解决方案(如果存在)是按实际分区直接划分, 按计算值划分。

表伪代码:

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 partition as P, count(*) as C from table_1
group by partition;

期望的结果

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

有效,但不是我正在寻找的东西:

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

2 个答案:

答案 0 :(得分:3)

运行此选项可为每个分区生成一组选择

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'

结果如下:

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

复制并粘贴结果删除最后一个union all然后运行。性能不太确定......我打了一个有5个分区的桌子,它在大约2秒的总行中完成了大约5密耳。

答案 1 :(得分:1)

查询可以为给定的分区表计算每个分区中的行,如下所示。

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