https://dev.mysql.com/doc/refman/9.2/en/partitioning.html
https://www.cnblogs.com/chenmh/p/5644496.html
https://kimi.moonshot.cn/share/cuj182051tqdt9c6uu4g
sql
DROP TABLE IF EXISTS `tb_ad_account_insight_0`;
CREATE TABLE `tb_ad_account_insight_0`
(
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`account_id` varchar(30) NOT NULL COMMENT '账户id',
`account_name` varchar(30) NOT NULL COMMENT '账户名称',
`date_start` varchar(30) NOT NULL COMMENT '统计起始日期',
`date_stop` varchar(30) NOT NULL COMMENT '统计终止日期',
`impressions` decimal(20, 2) NULL DEFAULT 0 COMMENT '展示',
`clicks` decimal(20, 2) NULL DEFAULT 0 COMMENT '点击',
`created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
`is_delete` tinyint(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除',
PRIMARY KEY (`id`, `account_id`)
) PARTITION BY KEY(account_id)
PARTITIONS 10;
sql
DROP TABLE IF EXISTS `tb_ad_account_insight_0`;
CREATE TABLE `tb_ad_account_insight_0`
(
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
`account_id` varchar(30) NOT NULL COMMENT '账户id',
`account_name` varchar(30) NOT NULL COMMENT '账户名称',
`country` varchar(80) NOT NULL COMMENT '国家',
`date_start` varchar(30) NOT NULL COMMENT '统计开始日期',
`date_stop` varchar(30) NOT NULL COMMENT '统计终止日期',
`impressions` decimal(20, 2) NULL DEFAULT 0 COMMENT '展示',
`clicks` decimal(20, 2) NULL DEFAULT 0 COMMENT '点击',
`created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
`is_delete` tinyint(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除',
PRIMARY KEY (`id`, `account_id`, `country`),
INDEX `idx_account_id_date_start` (`account_id`, `date_start`)
)
PARTITION BY KEY(account_id, country)
PARTITIONS 10;
account_id 本身就是唯一的,那么这里的 id 还有必要吗?
MySQL 中的分区仅支持水平分区
MySQL 中的分区会对表中的数据及表中的唯一索引(包括主键)进行分区,而分区的依据是分区键,故表的所有唯一索引中必须包含分区键。
1503 - A UNIQUE INDEX must include all columns in the table's partitioning function (prefixed columns are not considered).
前缀列的含义:在某些数据库系统中,前缀列(prefixed columns)可能指的是那些只使用了部分列值的列。例如,如果你有一个列是字符串类型,而你只用它的前几个字符作为索引的一部分,那么这个列就被认为是"前缀列"。这种情况下,数据库系统会明确指出,仅使用列的前缀部分是不足以满足分区键的要求的。