MySQL partition table example

There are a lot of Baidu on the concept of MySQL partition table, here are a few examples for reference, basically you can understand it at a glance. cool
Range type
You need to provide a numeric column as a basis for judgment
ALTER TABLE log_regist PARTITION BY RANGE (UNIX_TIMESTAMP( event_at ))(
PARTITION P201702 VALUES LESS THAN (1485878400) ENGINE = InnoDB,
PARTITION P201703 VALUES LESS THAN (1488297600) ENGINE = InnoDB,
PARTITION P201704 VALUES LESS THAN (1490976000) ENGINE = InnoDB,
PARTITION P201705 VALUES LESS THAN (1493568000) ENGINE = InnoDB,
PARTITION P201706 VALUES LESS THAN (1496246400) ENGINE = InnoDB
)
Notice The red letter must be a linear value, both numbers and timestamps (date needs to use the form of to_days(DATE), which is supported after 5.5). The core idea is to compare whether the specified column is in a certain interval (partition). The problem is that partitions will continue to increase over time, so be sure to add new partitions in time or you won't be able to write data. In addition, the reference column must be listed as a composite primary key (eg PRIMARY KEY (`id`,`create_at`) ).
Hash type
Use the Hash function to get the hash value of a column, and then provide a positive number of partitions.
ALTER TABLE log_order_finished PARTITION BY HASH(id) PARTITIONS 12;
The core idea is to judge the specified partition according to a HASH value. Unlike Range, the number of partitions is directly given here. In other words, the upper limit of the number of MySQL partitions is 1024. If it exceeds, there will be miracles. For details, please refer to the MySQL5 manual or Baidu.
List type
The List type must explicitly specify the range value, which is different from the Range's judgment on continuous sequences
Suppose there are 20 video stores, distributed in 4 regions with distribution rights, as shown in the following table:
area Store ID number
North District 3, 5, 6, 9, 17
Eastern District 1, 2, 10, 11, 19, 20
West End 4, 12, 13, 14, 18
Central District 7, 8, 15, 16
To split a table in such a way that rows belonging to the same regional store are kept in the same partition, the following "CREATE TABLE" statement can be used:
CREATE TABLE employees (
    id INT NOT NULL,
    fname VARCHAR(30),
    lname VARCHAR(30),
    hired DATE NOT NULL DEFAULT '1970-01-01',
    separated DATE NOT NULL DEFAULT '9999-12-31',
    job_code INT,
    store_id INT
)
PARTITION BY LIST(store_id)
    PARTITION pNorth VALUES IN (3,5,6,9,17),
    PARTITION pEast VALUES IN (1,2,10,11,19,20),
    PARTITION pWest VALUES IN (4,12,13,14,18),
    PARTITION pCentral VALUES IN (7,8,15,16)
);
This paragraph is copied from the manual. The reason I don't use this partitioning method is that the content range of the column cannot be agreed.
Key type
The only difference between them is that the key used is KEY instead of HASH, and the KEY partition takes only one list of one or more column names
It is also possible to split a table by linear KEY. Here is a simple example:
CREATE TABLE tk (
    col1 INT NOT NULL,
    col2 CHAR(5),
    col3 DATE
)
PARTITION BY LINEAR KEY (col1)
PARTITIONS 3;
This paragraph is very strange. The manual states that it is based on the hash method inside MySQL, similar to the password() function.
delete partition
ALTER TABLE tr DROP PARTITION p2;
add partition
ALTER TABLE members ADD PARTITION (PARTITION p3 VALUES LESS THAN (1960));
for ($i = 1; $i <= 36; $i++) {
$ts = mktime(0, 0, 0, $i + 1, 1);
$p = 'P' . date('Ym', $ts);
$d = date('Y-m-01', $ts);
echo "PARTITION $p VALUES LESS THAN ($ts) ENGINE = InnoDB,", '<br/>';
}
In addition, in order to easily generate SQL statements, you can refer to the following PHP code:
function makePartition(){
        echo "ALTER TABLE log_mobi_regist PARTITION BY RANGE (UNIX_TIMESTAMP(event_at)) \r\n<br/>";
        $partitions = [];

        for ($i = 1; $i <= 36; $i++) {
            $ts = mktime(0, 0, 0, $i + 1, 1);
            $p  = 'P' . date('Ym', $ts);
            $d  = date('Y-m-01', $ts);
            $partitions[] = "PARTITION $p VALUES LESS THAN ($ts) ENGINE = InnoDB\r\n".'<br/>';
        }
        echo '(', join(',', $partitions), ')';
}
 This section is the code I often use to generate log SQL, just for reference.
In addition, you can see the article on partition performance evaluation here will help: http://blog.csdn.net/jhq0113/article/details/44593511
Tucao, this editor is getting worse and worse

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326434786&siteId=291194637