是否压缩了分区中的数据

时间:2014-04-24 16:02:14

标签: oracle compression partitioning

如果压缩已标记为压缩

,此查询将为我提供
Select *
From All_Tab_Partitions

在讨论中,标记为压缩的分区实际上可以包含未压缩的数据(如果它已加载FAST_LOAD)。

我想只为未压缩的分区发出压缩命令。

如何判断分区内的数据是否已压缩?

1 个答案:

答案 0 :(得分:1)

分区上的压缩可能启用但如果数据是从直接路径插入创建的,则只能使用。使用抽样和功能 DBMS_COMPRESSION.GET_COMPRESSION_TYPE估算行数百分比 每个分区压缩。

<强>模式

create table compression_test
(
  a number,
  b varchar2(100)
) partition by range(a)
(
  partition p_all_compressed values less than (2) compress,
  partition p_none_compressed values less than (3) compress,
  partition p_half_compressed values less than (4) compress
);

insert /*+ append */ into compression_test partition (p_all_compressed)
select 1, '0123456789' from dual connect by level <= 100000;
commit;
insert into compression_test partition (p_none_compressed)
select 2, '0123456789' from dual connect by level <= 100000;
commit;
insert /*+ append */ into compression_test partition (p_half_compressed)
select 3, '0123456789' from dual connect by level <= 50000;
commit;
insert into compression_test partition (p_half_compressed)
select 3, '0123456789' from dual connect by level <= 50000;
commit;

估算代码

--Find percent compressed from sampling each partition.
declare
  v_percent_compressed number;
begin
  --Loop through partitions.
  for partitions in
  (
    select table_owner, table_name, partition_name
    from dba_tab_partitions
    --Enter owner and table_name here.
    where table_owner = user
      and table_name = 'COMPRESSION_TEST'
  ) loop
    --Dynamic SQL to sample a partition and test for compression.
    execute immediate '
      select
        round(sum(is_compressed)/count(is_compressed) * 100) percent_compressed
      from
      (
        --Compression status of sampled rows.
        --
        --Numbers are based on constants from:
        --docs.oracle.com/cd/E16655_01/appdev.121/e17602/d_compress.htm
        --Assumption: Only basic compression is used.
        --Assumption: Partitions are large enough for 0.1% sample size.
        select
          case dbms_compression.get_compression_type(
              user,
              '''||partitions.table_name||''',
              rowid,
              '''||partitions.partition_name||'''
            )
            when 4096 then 1
            when 1 then 0
          end is_compressed
        from '||partitions.table_owner||'.'||partitions.table_name||'
        partition ('||partitions.partition_name||') sample (0.1)
      )
    ' into v_percent_compressed;

    dbms_output.put_line(rpad(partitions.partition_name||':', 31, ' ')||
      lpad(v_percent_compressed, 3, ' '));
  end loop;
end;
/

示例输出

P_ALL_COMPRESSED:              100
P_HALF_COMPRESSED:              55
P_NONE_COMPRESSED:               0