通常当表的数据量非常大,以致于可能使数据库空间紧张,或者由于表非常大导致相关 SQL 查询性能变慢时,可以考虑使用分区表。

唯一索引和拆分键

使用分区表时要选择合适的拆分键以及拆分策略。如果是日志类型的大表,根据时间类型的列做 RANGE 分区是最合适的。如果是并发访问非常高的表,结合业务特点选择能满足绝大部分核心业务查询的列作为拆分键是最合适的。无论选哪个列做为分区键,都不大可能满足所有的查询性能。

分区表中的全局唯一性需求可以通过主键约束和唯一约束实现。OceanBase 数据库的分区表的主键约束和唯一键约束必须包含拆分键。唯一约束也是一个全局索引。全局唯一的需求也可以通过本地唯一索引实现,只需要在唯一索引里包含拆分键。

示例:创建有唯一性需求的分区表

  1. obclient> CREATE TABLE account(
  2. id number NOT NULL PRIMARY KEY
  3. , name varchar2(50) NOT NULL UNIQUE
  4. , value number NOT NULL
  5. , gmt_create date DEFAULT sysdate NOT NULL
  6. , gmt_modified date DEFAULT sysdate NOT NULL
  7. ) PARTITION BY HASH(id) PARTITIONS 16;
  8. Query OK, 0 rows affected (0.22 sec)
  9. obclient> CREATE TABLE account2(
  10. id number NOT NULL PRIMARY KEY
  11. , name varchar2(50) NOT NULL
  12. , value number NOT NULL
  13. , gmt_create date DEFAULT sysdate NOT NULL
  14. , gmt_modified date DEFAULT sysdate NOT NULL
  15. ) PARTITION BY HASH(id) PARTITIONS 16;
  16. Query OK, 0 rows affected (0.14 sec)
  17. obclient> CREATE UNIQUE INDEX account2_uk ON account2(name, id) LOCAL ;
  18. Query OK, 0 rows affected (0.75 sec)
  19. obclient> SELECT table_Name,index_name,uniqueness,partitioned FROM user_Indexes WHERE table_name LIKE 'ACCOUNT%';
  20. +------------+-----------------------------------+------------+-------------+
  21. | TABLE_NAME | INDEX_NAME | UNIQUENESS | PARTITIONED |
  22. +------------+-----------------------------------+------------+-------------+
  23. | ACCOUNT | ACCOUNT_OBPK_1585823071504331 | UNIQUE | NO |
  24. | ACCOUNT | ACCOUNT_OBUNIQUE_1585823071505517 | UNIQUE | NO |
  25. | ACCOUNT2 | ACCOUNT2_OBPK_1585823071631837 | UNIQUE | NO |
  26. | ACCOUNT2 | ACCOUNT2_UK | UNIQUE | YES |
  27. +------------+-----------------------------------+------------+-------------+
  28. 4 rows in set (0.03 sec)
  29. obclient> SELECT table_name, constraint_name, constraint_type, status, index_name FROM user_constraints t WHERE table_name LIKE 'ACCOUNT%'\G
  30. *************************** 1. row ***************************
  31. TABLE_NAME: ACCOUNT
  32. CONSTRAINT_NAME: ACCOUNT_OBUNIQUE_1585385880987688
  33. CONSTRAINT_TYPE: U
  34. STATUS: NULL
  35. INDEX_NAME: ACCOUNT_OBUNIQUE_1585385880987688
  36. *************************** 2. row ***************************
  37. TABLE_NAME: ACCOUNT2
  38. CONSTRAINT_NAME: ACCOUNT2_UK
  39. CONSTRAINT_TYPE: U
  40. STATUS: NULL
  41. INDEX_NAME: ACCOUNT2_UK
  42. *************************** 3. row ***************************
  43. TABLE_NAME: ACCOUNT
  44. CONSTRAINT_NAME: ACCOUNT_OBPK_1585385880986752
  45. CONSTRAINT_TYPE: P
  46. STATUS: ENABLED
  47. INDEX_NAME: ACCOUNT_OBPK_1585385880986752
  48. *************************** 4. row ***************************
  49. TABLE_NAME: ACCOUNT2
  50. CONSTRAINT_NAME: ACCOUNT2_OBPK_1585386277230890
  51. CONSTRAINT_TYPE: P
  52. STATUS: ENABLED
  53. INDEX_NAME: ACCOUNT2_OBPK_1585386277230890
  54. 4 rows in set (0.01 sec)

更新分区键

在针对分区表做 UPDATE 时,如果更新了分区键,理论上记录有可能需要从一个分区移动到另外一个分区。如果系统检测到 UPDATE 操作会导致分区移动,则会禁止这种更新。

  1. obclient> create table t_part(id number not null , c1 varchar2(10) not null, c2 varchar2(100), primary key(id,c1)) partition by hash(c1) partitions 8;
  2. Query OK, 0 rows affected (0.29 sec)
  3. obclient> insert into t_part(id,c1,c2) values(1,'A','aaaaaaaa'),(2,'B','bbbbbbbbb'),(3,'C','ccccccccc'),(4,'D','dddddddd');
  4. Query OK, 4 rows affected (0.04 sec)
  5. Records: 4 Duplicates: 0 Warnings: 0
  6. obclient> select * from t_part;
  7. +----+----+-----------+
  8. | ID | C1 | C2 |
  9. +----+----+-----------+
  10. | 3 | C | ccccccccc |
  11. | 2 | B | bbbbbbbbb |
  12. | 4 | D | dddddddd |
  13. | 1 | A | aaaaaaaa |
  14. +----+----+-----------+
  15. 4 rows in set (0.05 sec)
  16. obclient> update t_part set c1='CC' where c1='C*';
  17. ORA-14402: updating partition key column would cause a partition change

如果分区键的值没有变化,建议不要更新它。如果分区键的值有变化,则需要开启表的如下属性:

  1. obclient> alter table t_part enable row movement;
  2. Query OK, 0 rows affected (0.02 sec)
  3. obclient> update t_part set c1='CC' where c1='C';
  4. Query OK, 1 row affected (0.00 sec)
  5. Rows matched: 1 Changed: 1 Warnings: 0