TDSQL分区表测试记录
在TDSQL中,分表也叫一级分区表。有hash、range、list三种规则。
一级hash分区使用shardkey关键字指定拆分键。
range和list分区使用 tdsql_distributed by语法指定拆分键。
一级HASH分区:
一级hash分区支持类型
– DATE,DATETIME
– TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT
– CHAR,VARCHAR
Shardkey 字段必须是主键以及所有唯一索引的一部分
Shardkey字段的值不能为中文,因为Proxy不会转换字符集,所以不同字符集可能会路由到不同的分区
Shardkey=a 需放在SQL语句的最后
例子:
DROP TABLE IF EXISTS employees_hash;
CREATE TABLE `employees_hash` (
`id`int NOT NULL,
`city` varchar(10),
`fired` DATE NOT NULL DEFAULT '1970.01.01',
PRIMARY KEY(id)
) shardkey=id;
一级RANGE分区:
一级range分区支持类型
– DATE,DATETIME,TIMESTAMP
– TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT
– CHAR,VARCHAR
例子:
create table t1(a int key, b int) tdsql_distributed by range(a) (s1 values less than(100), s2 values less than(200));
【禁止】避免使用TIMESTAMP类型作为分区键,因为timestamp受到时区的影响,同时只能使用到2038年
【建议】如果分区键是char或者varchar类型,建议长度不超255
一级LIST分区:
一级list分区支持类型
– DATE,DATETIME,TIMESTAMP
– TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT
– CHAR,VARCHAR
例子:
create table t2(a int key, b int) tdsql_distributed by list(a) (s1 values in(1,2), s2 values in (3,4));
注意:
dsql_distributed by ...语法放置于create table ...的末尾
创建一级range分区表语句中指定的s1和s2是每个set的别名,基于实现原理,s1、s2不能自定义,只能按照顺序依次命名为s1、s2…
set的别名可通过/proxy/show status;获取到
实操创建3个分区表:
1、创建range分区表:
CREATE TABLE test_range(
sale_id INT AUTO_INCREMENT,
sale_date DATE,
region VARCHAR(20),
sale_amount DECIMAL(10,2),
PRIMARY KEY (sale_id, sale_date)
)
PARTITION BY RANGE (YEAR(sale_date))
(
PARTITION p0 VALUES LESS THAN (2020),
PARTITION p1 VALUES LESS THAN (2021),
PARTITION p2 VALUES LESS THAN (2023),
PARTITION p3 VALUES LESS THAN MAXVALUE
);
INSERT INTO test_range ( sale_date, region, sale_amount) VALUES ( '2009-12-31', 'east', 11000.00);
INSERT INTO test_range ( sale_date, region, sale_amount) VALUES ('2020-06-30', 'west', 12000.00);
INSERT INTO test_range ( sale_date, region, sale_amount) VALUES ( '2021-09-30', 'north', 13000.00);
INSERT INTO test_range ( sale_date, region, sale_amount) VALUES ( '2022-07-15', 'south', 14000.00);
--要查看分区表 test_range 中的 p0 分区中的数据,使用 SELECT 语句并指定 PARTITION 子句
-- 查询 p0 分区中所有行
SELECT * FROM test_range PARTITION (p0);
2、创建list分区表:
CREATE TABLE test_list (
id INT NOT NULL,
name VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
store_id INT,
PRIMARY KEY(id,store_id)
)
PARTITION BY LIST(store_id)
(
PARTITION pNorth VALUES IN (3,5,6,9,17),
PARTITION pEast VALUES IN (1,2,10,11,19,20),
PARTITION pWest VALUES IN (4,12,13,14,18),
PARTITION pCentral VALUES IN (7,8,15,16)
);
INSERT INTO test_list (id, name, hired, separated, store_id)
VALUES
(1, 'John Doe', '2021-01-01', '9999-12-31', 3),
(2, 'Jane Smith', '2021-02-01', '9999-12-31', 5),
(3, 'Bob Johnson', '2021-03-01', '9999-12-31', 1),
(4, 'Alice Williams', '2021-04-01', '9999-12-31', 2),
(5, 'Tom Brown', '2021-05-01', '9999-12-31', 4),
(6, 'Sara Davis', '2021-06-01', '9999-12-31', 3),
(7, 'Mike Lee', '2021-07-01', '9999-12-31', 1),
(8, 'Emily Clark', '2021-08-01', '9999-12-31', 2),
(9, 'David Allen', '2021-09-01', '9999-12-31', 4),
(10, 'Lily Wilson', '2021-10-01', '9999-12-31', 3);
SELECT * FROM test_list PARTITION (pNorth);
3、创建hash分区表:
CREATE TABLE test_hash (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
store_id INT,
PRIMARY KEY(id,store_id)
)
PARTITION BY HASH(store_id)
PARTITIONS 10;
INSERT INTO test_hash (name, store_id)
VALUES
('John Doe', 3),
('Jane Smith', 5),
('Bob Johnson', 1),
('Alice Williams', 2),
('Tom Brown', 4),
('Sara Davis', 3),
('Mike Lee', 1),
('Emily Clark', 2),
('David Allen', 4),
( 'Lily Wilson',3);
SELECT * FROM test_hash PARTITION (p0);
更多详情参考:
https://main.qcloudimg.com/raw/document/product/pdf/1515_62371_cn.pdf
https://cloud.tencent.com/document/product/557/58907
Tag标签:「TDSQL 分区表」更新时间:「2023-05-18 11:01:55」阅读次数:「852」