Question

I have the following table.

I want to add partitions too.

CREATE TABLE `app_log_Test` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `dateCreated` datetime NOT NULL,
  `host` varchar(512) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `label` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `event` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `level` varchar(8) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `message` text COLLATE utf8mb4_unicode_ci,
  `version` bigint(20) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `app_log_dateCreated` (`dateCreated`),
  KEY `app_log_label` (`label`),
  KEY `app_log_event` (`event`),
  KEY `app_log_level` (`level`)
) ENGINE=TokuDB `COMPRESSION`=tokudb_quicklz

I am using MariaDB 10.

MariaDB [test2]> alter table app_log_Test partition by RANGE(TO_DAYS(dateCreated))(
-> PARTITION p_201809 VALUES LESS THAN (TO_DAYS('2018-09-01 00:00:00')) ENGINE = TokuDB,
-> PARTITION p_201810 VALUES LESS THAN (TO_DAYS('2018-10-01 00:00:00')) ENGINE = TokuDB);

I get the following error

ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function
Was it helpful?

Solution

First I need to deal with the error

ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function

This makes sense and partitioning means putting data into different files based on some condition. If the partition condition is not part of the tables primary key index this would not know how where to put the data.

alter table app_log_Test drop PRIMARY KEY, add primary key (`id`, `dateCreated`);

Next, I can re-run my alter table to add the partitions I care about.

ALTER TABLE app_log_Test
PARTITION BY RANGE (TO_DAYS(dateCreated))
    (PARTITION p_invalid_date VALUES LESS THAN (0) ENGINE = TokuDB,
    PARTITION p_201809 VALUES LESS THAN (TO_DAYS('2018-09-01 00:00:00')),
    PARTITION p_201810 VALUES LESS THAN (TO_DAYS('2018-10-01 00:00:00')),
    PARTITION p_max_future_dates VALUES LESS THAN MAXVALUE);

If I need to add more partitions after that. I don't have to specify the partition scheme again I can just add the partition and its constraints.

ALTER TABLE app_log_Test
    REORGANIZE PARTITION p_max_future_dates INTO (
        PARTITION p_201811 VALUES LESS THAN (TO_DAYS('2018-11-01 00:00:00')),
        PARTITION p_201812 VALUES LESS THAN (TO_DAYS('2018-12-01 00:00:00')),
        PARTITION p_max_future_dates  VALUES LESS THAN MAXVALUE);  

My table now looks like this.

show create table app_log_Test;
Create Table: CREATE TABLE `app_log_Test` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `dateCreated` datetime NOT NULL,
  `host` varchar(512) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `label` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `event` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `level` varchar(8) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `message` text COLLATE utf8mb4_unicode_ci,
  `version` bigint(20) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`,`dateCreated`),
  KEY `app_log_dateCreated` (`dateCreated`),
  KEY `app_log_label` (`label`),
  KEY `app_log_event` (`event`),
  KEY `app_log_level` (`level`)
) ENGINE=TokuDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci `COMPRESSION`=tokudb_zlib

/*!50100 PARTITION BY RANGE (TO_DAYS(dateCreated))
(PARTITION p_invalid_date VALUES LESS THAN (0) ENGINE = TokuDB,
PARTITION p_201901 VALUES LESS THAN (737425) ENGINE = TokuDB,
PARTITION p_201810 VALUES LESS THAN (737333) ENGINE = TokuDB,
PARTITION p_201811 VALUES LESS THAN (737364) ENGINE = TokuDB,
PARTITION p_201812 VALUES LESS THAN (737394) ENGINE = TokuDB,
PARTITION p_max_future_dates VALUES LESS THAN MAXVALUE ENGINE = TokuDB) */
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top