MySQL Partitioning a Table That Contains a Primary Key
I have a table that I want to partition:
CREATE TABLE `tbl_orders` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL DEFAULT '0' COLLATE 'utf8mb4_general_ci',
`system_id` INT(11) NOT NULL DEFAULT '0',
`created_at` DATETIME NULL DEFAULT NULL,
`updated_at` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `system_id` (`system_id`) USING BTREE
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=8
;
ALTER table tbl_orders
PARTITION BY HASH(system_id)
PARTITIONS 4;
Example of what im trying to achieve:
I have a table which I want to partition by system_id
in order to speed up queries.
When I run the partition I get the following error:
/* SQL Error (1503): A PRIMARY KEY must include all columns in the table's partitioning function */
- What would I change to run this partition successfully whilst still achieving my aim which is to split the table on
system_id
? - Is partitioning this way achievable with a primary key on the table?
Solution 1:
PARTITIONing
requires you to add the "partition key" (system_id
) to every Unique index, including the PRIMARY KEY
.
You will, I predict, find that PARTITION BY HASH
is useless for performance. It may even slow down the query.
Please show a query that you hope to speed up; I will advise in more detail.