Search code examples
mysqlpartitioningdata-partitioning

Partitioning by range columns unexpected behavior


I have MySQL table partitioned by range columns (c_id and created_at) and I created 2 partitions:

  1. logs_1_2020 (c_id less than 2 and created less than 2021-01-01 00:00:00)

  2. logs_1_2021 (c_id less than 2 and created less than 2022-01-01 00:00:00)

When I run

INSERT INTO example_log_table (c_id, data, created)
    VALUES (1, 'test', '2021-10-24 18:16:08')

I'm supposed to find the result stored in logs_1_2021, but I was shocked when I found her in logs_1_2020.

Does anyone have an explanation for that?

This table SQL generator:

CREATE TABLE example_log_table (
                        id int auto_increment ,
                        c_id int,
                        data TEXT NOT NULL,
                        created DATETIME NOT NULL,
                        primary key (id,c_id,created)
) PARTITION BY RANGE columns (c_id,created)(
    PARTITION logs_1_2020 VALUES LESS THAN  (2,'2021-01-01 00:00:00'),
    PARTITION logs_1_2021 VALUES LESS THAN  (2,'2022-01-01 00:00:00')
);

Solution

  • After searching a lot for Is there a way to make Mysql insert data into partition based on column values not tuple comparison I decide to make my partitions like that :

            PARTITION logs_1_2020 VALUES LESS THAN  (1,'2021-01-01'),
            PARTITION logs_2_2020 VALUES LESS THAN  (2,'2021-01-01'),
            PARTITION logs_2_2021 VALUES LESS THAN  (2,'2022-01-01')
    

    And at insertion i'm insert with exact first parameter to make Mysql compare the second parameter is less or not.

    So when run :

    INSERT INTO example_log_table (c_id, created) VALUES (2, '2021-10-21')
    

    Will be inserted at logs_2_2021 because c_id is matched and created is less than created at second row