I am implementing data partitioning on mysql server(8.0).
As a "must-have requirement of partitioning" I have to add my partition key as a primary key, which is created_at
in my table. eg:
alter table notifications drop primary key, add primary key(`id`, `created_at`);
In above query, id
is an auto-increment primary key. I have more than 10M entries in table.
Mysql won't allow live updates on schemas; it will lock the table when I run alter
command.
At the same time, I can take a downtime for this process. Please guide the best approach to take this live.
I tried generating same amount of data on my local using "Faker" library & while running alter query on it. I observed it taking more than 2 hours for this process.
Schema
CREATE TABLE `data` (
`seq_id` int NOT NULL AUTO_INCREMENT,
`id` varchar(100) NOT NULL,
`doc_id` varchar(100) NOT NULL,
`page_no` int DEFAULT '1',
`file_store` varchar(100) DEFAULT NULL,
`s_id` varchar(100) NOT NULL,
`s_f_id` varchar(100) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`f_store` varchar(100) DEFAULT 'GCP',
`purged` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`seq_id`),
UNIQUE KEY `id` (`id`),
KEY `doc_id` (`doc_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `data `
PARTITION BY RANGE (UNIX_TIMESTAMP(created_at))
(
PARTITION `partition_2020` VALUES LESS THAN (UNIX_TIMESTAMP('2020-01-01 00:00:00')),
PARTITION `partition_2021` VALUES LESS THAN (UNIX_TIMESTAMP('2021-01-01 00:00:00')),
PARTITION `partition_2022` VALUES LESS THAN (UNIX_TIMESTAMP('2022-01-01 00:00:00')),
PARTITION `partition_2022` VALUES LESS THAN (UNIX_TIMESTAMP('2023-05-31 00:00:00')),
PARTITION `partition_future` VALUES LESS THAN (MAXVALUE)
);