分区表使用建议 
============================



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

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

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

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

```javascript
obclient> CREATE TABLE account(
         id bigint NOT NULL PRIMARY KEY
         , name varchar(50) NOT NULL UNIQUE
         , value number NOT NULL
         , gmt_create timestamp DEFAULT current_timestamp NOT NULL
         , gmt_modified timestamp DEFAULT current_timestamp NOT NULL
     ) PARTITION BY HASH(id) PARTITIONS 16;
Query OK, 0 rows affected (0.16 sec)

obclient> CREATE TABLE account2(
         id bigint NOT NULL PRIMARY KEY
         , name varchar(50) NOT NULL
         , value number NOT NULL
         , gmt_create timestamp DEFAULT current_timestamp NOT NULL
         , gmt_modified timestamp DEFAULT current_timestamp NOT NULL
     ) PARTITION BY HASH(id) PARTITIONS 16;
Query OK, 0 rows affected (0.11 sec)

obclient> CREATE UNIQUE INDEX account2_uk ON account2(name, id) LOCAL ;
Query OK, 0 rows affected (0.73 sec)

obclient> show indexes from account;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
| Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment   | Index_comment | Visible |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
| account |          0 | PRIMARY  |            1 | id          | A         |        NULL | NULL     | NULL   |      | BTREE      | available |               | YES     |
| account |          0 | name     |            1 | name        | A         |        NULL | NULL     | NULL   |      | BTREE      | available |               | YES     |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
2 rows in set (0.01 sec)

obclient> show indexes from account2;
+----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
| Table    | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment   | Index_comment | Visible |
+----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
| account2 |          0 | PRIMARY     |            1 | id          | A         |        NULL | NULL     | NULL   |      | BTREE      | available |               | YES     |
| account2 |          0 | account2_uk |            1 | name        | A         |        NULL | NULL     | NULL   |      | BTREE      | available |               | YES     |
| account2 |          0 | account2_uk |            2 | id          | A         |        NULL | NULL     | NULL   |      | BTREE      | available |               | YES     |
+----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
3 rows in set (0.00 sec)

obclient> SELECT * FROM information_schema.`TABLE_CONSTRAINTS` WHERE table_schema='TPCCDB' AND table_name LIKE 'ACCOUNT%';
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
| def                | tpccdb            | PRIMARY         | tpccdb       | account    | PRIMARY KEY     |
| def                | tpccdb            | name            | tpccdb       | account    | UNIQUE          |
| def                | tpccdb            | PRIMARY         | tpccdb       | account2   | PRIMARY KEY     |
| def                | tpccdb            | account2_uk     | tpccdb       | account2   | UNIQUE          |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
4 rows in set (0.02 sec)
```





