分区表分区字段的update操作

默认情况下,oracle的分区表对于分区字段是不允许进行update操作的,如果有对分区字段行进update,就会报错——ORA-14402: 更新分区关键字列将导致分区的更改。但是可以通过打开表的row movement属性来允许对分区字段的update操作。

例:创建分区表test_part进行实验

create table TEST_PART
(
  A1 NUMBERnot null,
  A2 DATE      not null,
  A3 VARCHAR2(6) not null,
  A4 DATE not null,
  A5 NUMBER not null,
)
partition by range (A1)
(
  partition P1 values less than (1000),
  partition P2 values less than (2000),
  partition P3 values less than (3000),
  partition P4 values less than (4000),
  partition P5 values less than (5000),
  partition P6 values less than (MAXVALUE)
);

插入如下的数据
SQL> select * from test_part;

        A1 A2          A3     A4                  A5
---------- ----------- ------ ----------- ----------
       123 2006-06-30  123456 2006-06-30         123
       456 2006-06-30  asdfgh 2006-06-30         456
         1 2006-06-30  234123 2006-06-30           1
         2 2006-06-30  234234 2006-06-30           2
      1234 2006-06-30  456789 2006-06-30        1234
      1111 2006-06-30  ewrqwe 2006-06-30        1111
      2222 2006-06-30  fdafda 2006-06-30        2222
      3333 2006-06-30  342342 2006-06-30        3333
      5678 2006-06-30  qwerty 2006-06-30        5678

9 rows selected

分区P1、P2的数据分别为:
SQL> select rowid,t.* from test_part partition(p1) t;

ROWID                      A1 A2          A3     A4                  A5
------------------ ---------- ----------- ------ ----------- ----------
AAAGLoAAGAAAtsEAAB        456 2006-06-30  asdfgh 2006-06-30         456
AAAGLoAAGAAAtsEAAC          1 2006-06-30  234123 2006-06-30           1
AAAGLoAAGAAAtsEAAD          2 2006-06-30  234234 2006-06-30           2
AAAGLoAAGAAAtsEAAE        123 2006-06-30  123456 2006-06-30         123

SQL> select rowid,t.* from test_part partition(p2) t;

ROWID                      A1 A2          A3     A4                  A5
------------------ ---------- ----------- ------ ----------- ----------
AAAGLwAAGAAA+8MAAC       1234 2006-06-30  456789 2006-06-30        1234
AAAGLwAAGAAA+8MAAD       1111 2006-06-30  ewrqwe 2006-06-30        1111

直接update提示错误
SQL> update test_part set a1=1123 where a1=123;

update test_part set a1=1123 where a1=123

ORA-14402: 更新分区关键字列将导致分区的更改

打开row movement属性
SQL> alter table test_part enable row movement;

Table altered

再次执行update操作
SQL> update test_part set a1=1123 where a1=123;

1 row updated

执行是成功的并迁移到分区P2上了,且这时候rowid也发生了变化
SQL> select rowid,t.* from test_part partition(p2) t;

ROWID                      A1 A2          A3     A4                  A5
------------------ ---------- ----------- ------ ----------- ----------
AAAGLwAAGAAA+8MAAC       1234 2006-06-30  456789 2006-06-30        1234
AAAGLwAAGAAA+8MAAD       1111 2006-06-30  ewrqwe 2006-06-30        1111
AAAGLwAAGAAA+8PAAB       1123 2006-06-30  123456 2006-06-30         123

SQL> 

enable row movement可以允许数据段的压缩、update分区字段的数据(跨分区的)

但是,也是有限制性的:对于普通表(heap-organized)行迁移后rowid会发生变化,对于索引表(index-organized)rowid虽然依然有效,但是其实际对应的物理构成是错误的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在星环hive中创建拉链分区表需要先了解什么是拉链分区表。拉链分区表是一种特殊的分区表,它在每个分区中都会保存一定数量的历史数据,并且在新数据插入时,会尝试将新数据与历史数据合并,以减少数据量和查询时的开销。因此,拉链分区表适用于数据更新频繁且查询历史数据较多的场景。 下面是创建星环hive中拉链分区表的具体步骤: 1. 创建包含历史数据的分区表 首先,需要创建一个包含历史数据的分区表。例如,我们创建一个名为 `sales` 的表,包含 `date` 和 `sales_amount` 两个字段,并按照 `date` 字段进行分区。可以使用如下命令创建: ``` CREATE TABLE sales ( date string, sales_amount double ) PARTITIONED BY (date) STORED AS PARQUET; ``` 2. 添加拉链分区字段 接着,需要为表添加一个额外的字段,用于保存每个分区中的历史数据,这个字段通常被称为拉链分区字段。我们可以添加一个类型为 `array<struct<date:string, sales_amount:double>>` 的字段,这个数组中的每个元素表示该分区中的历史数据。可以使用如下命令添加: ``` ALTER TABLE sales ADD COLUMNS ( history array<struct<date:string, sales_amount:double>> ); ``` 3. 开启拉链分区 然后,需要开启拉链分区功能,这可以通过设置表属性 `transactional_properties` 来实现。具体来说,需要将 `transactional_properties` 设置为 `{'transactional': true, 'transactional_properties': {'insert_only': 'false', 'update': 'true'}}`。可以使用如下命令设置: ``` ALTER TABLE sales SET TBLPROPERTIES ( 'transactional'='true', 'transactional_properties'='{"insert_only": "false", "update": "true"}' ); ``` 4. 插入数据 现在,可以插入数据到 `sales` 表中。插入新数据时,需要将历史数据一起插入,并且在插入前需要对历史数据进行去重和合并。具体来说,可以使用如下语句插入数据: ``` INSERT INTO sales PARTITION (date='2022-01-01') VALUES ( '2022-01-01', 100.0, array(named_struct('date', '2022-01-01', 'sales_amount', 50.0)) ); ``` 在这个例子中,我们插入了一条新数据,并且将 `history` 字段设置为一个包含一条历史数据的数组。 5. 查询数据 现在,可以查询 `sales` 表中的数据。在查询时,需要使用 `LATERAL VIEW` 和 `explode` 函数将 `history` 字段展开,并且按照 `date` 字段进行排序。例如,可以使用如下语句查询 `2022-01-01` 分区中的数据: ``` SELECT s.date, s.sales_amount, h.date as history_date, h.sales_amount as history_sales_amount FROM sales s LATERAL VIEW explode(history) h AS history WHERE s.date = '2022-01-01' ORDER BY h.date; ``` 这个查询会返回 `2022-01-01` 分区中的所有数据,包括新数据和历史数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值