SavefileArchive
USD/IDR ...
|
BTC ...
|
ETH ...
|
GOLD/gram ...
Terbaru
SavefileArchive — Tutorial coding, tips programming, dan dunia musik untuk developer & pecinta musik Indonesia
create a partitioned table in MySQL

create a partitioned table in MySQL


create a partitioned table in MySQL, you can use the PARTITION BY clause in the CREATE TABLE statement. The PARTITION BY clause specifies the column or expression that will be used to determine the partition to which each row belongs.

For example, to create a table with range partitions based on the date column, you can use the following code:

CREATE TABLE orders ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, date DATE NOT NULL, customer_id INT UNSIGNED NOT NULL, amount DECIMAL(10,2) NOT NULL ) PARTITION BY RANGE(date) ( PARTITION p0 VALUES LESS THAN ('2022-01-01'), PARTITION p1 VALUES LESS THAN ('2022-04-01'), PARTITION p2 VALUES LESS THAN ('2022-07-01'), PARTITION p3 VALUES LESS THAN ('2022-10-01'), PARTITION p4 VALUES LESS THAN (MAXVALUE) );

In this example, the orders table is partitioned into five partitions, based on the date column. The p0 partition contains rows with a date less than '2022-01-01', the p1 partition contains rows with a date less than '2022-04-01', and so on. The p4 partition contains rows with a date greater than or equal to '2022-10-01'.

You can also use the HASH or KEY algorithms to partition the table. For example, to create a table with hash partitions based on the customer_id column, you can use the following code:

CREATE TABLE orders ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, date DATE NOT NULL, customer_id INT UNSIGNED NOT NULL, amount DECIMAL(10,2) NOT NULL ) PARTITION BY HASH(customer_id) PARTITIONS 10;

In this example, the orders table is partitioned into 10 partitions, based on the customer_id column. The HASH algorithm determines the partition to which each row belongs by calculating a hash value of the customer_id column and then mapping it to one of the 10 partitions.

To add partitions to an existing table in MySQL, you can use the ALTER TABLE statement with the PARTITION BY clause. The PARTITION BY clause specifies the column or expression that will be used to determine the partition to which each row belongs.

For example, to add range partitions to an existing table based on the date column, you can use the following code:

ALTER TABLE orders PARTITION BY RANGE(date) ( PARTITION p0 VALUES LESS THAN ('2022-01-01'), PARTITION p1 VALUES LESS THAN ('2022-04-01'), PARTITION p2 VALUES LESS THAN ('2022-07-01'), PARTITION p3 VALUES LESS THAN ('2022-10-01'), PARTITION p4 VALUES LESS THAN (MAXVALUE) );

In this example, the orders table is partitioned into five partitions, based on the date column. The p0 partition contains rows with a date less than '2022-01-01', the p1 partition contains rows with a date less than '2022-04-01', and so on. The p4 partition contains rows with a date greater than or equal to '2022-10-01'.

You can also use the HASH or KEY algorithms to partition the table. For example, to add hash partitions to an existing table based on the customer_id column, you can use the following code:

ALTER TABLE orders PARTITION BY HASH(customer_id) PARTITIONS 10;

In this example, the orders table is partitioned into 10 partitions, based on the customer_id column. The HASH algorithm determines the partition to which each row belongs by calculating a hash value of the customer_id column and then mapping it to one of the 10 partitions.