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

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

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

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

    1. obclient> CREATE TABLE account(
    2. id bigint NOT NULL PRIMARY KEY
    3. , name varchar(50) NOT NULL UNIQUE
    4. , value number NOT NULL
    5. , gmt_create timestamp DEFAULT current_timestamp NOT NULL
    6. , gmt_modified timestamp DEFAULT current_timestamp NOT NULL
    7. ) PARTITION BY HASH(id) PARTITIONS 16;
    8. Query OK, 0 rows affected (0.16 sec)
    9. obclient> CREATE TABLE account2(
    10. id bigint NOT NULL PRIMARY KEY
    11. , name varchar(50) NOT NULL
    12. , value number NOT NULL
    13. , gmt_create timestamp DEFAULT current_timestamp NOT NULL
    14. , gmt_modified timestamp DEFAULT current_timestamp NOT NULL
    15. ) PARTITION BY HASH(id) PARTITIONS 16;
    16. Query OK, 0 rows affected (0.11 sec)
    17. obclient> CREATE UNIQUE INDEX account2_uk ON account2(name, id) LOCAL ;
    18. Query OK, 0 rows affected (0.73 sec)
    19. obclient> show indexes from account;
    20. +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
    21. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
    22. +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
    23. | account | 0 | PRIMARY | 1 | id | A | NULL | NULL | NULL | | BTREE | available | | YES |
    24. | account | 0 | name | 1 | name | A | NULL | NULL | NULL | | BTREE | available | | YES |
    25. +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
    26. 2 rows in set (0.01 sec)
    27. obclient> show indexes from account2;
    28. +----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
    29. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
    30. +----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
    31. | account2 | 0 | PRIMARY | 1 | id | A | NULL | NULL | NULL | | BTREE | available | | YES |
    32. | account2 | 0 | account2_uk | 1 | name | A | NULL | NULL | NULL | | BTREE | available | | YES |
    33. | account2 | 0 | account2_uk | 2 | id | A | NULL | NULL | NULL | | BTREE | available | | YES |
    34. +----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
    35. 3 rows in set (0.00 sec)
    36. obclient> SELECT * FROM information_schema.`TABLE_CONSTRAINTS` WHERE table_schema='TPCCDB' AND table_name LIKE 'ACCOUNT%';
    37. +--------------------+-------------------+-----------------+--------------+------------+-----------------+
    38. | CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE |
    39. +--------------------+-------------------+-----------------+--------------+------------+-----------------+
    40. | def | tpccdb | PRIMARY | tpccdb | account | PRIMARY KEY |
    41. | def | tpccdb | name | tpccdb | account | UNIQUE |
    42. | def | tpccdb | PRIMARY | tpccdb | account2 | PRIMARY KEY |
    43. | def | tpccdb | account2_uk | tpccdb | account2 | UNIQUE |
    44. +--------------------+-------------------+-----------------+--------------+------------+-----------------+
    45. 4 rows in set (0.02 sec)