Practical Tutorial Using PolarDB-X's TTL Table Function

For the convenience of user experience, PolarDB-X provides a free experimental environment, where you can experience the installation and deployment of PolarDB-X and various kernel features. In addition to free experiments, PolarDB-X also provides free video courses to teach you how to use PolarDB-X distributed database.

This experiment will guide you how to manage PolarDB-X partitions

Free experimental address for this issue

Address of this teaching video

Preparation

Assuming that PolarDB-X has been built and deployed according to the content of the previous lecture , PolarDB-X is installed using PolarDB-X Operator, and the PolarDB-X database can be successfully linked.

Connect to PolarDB-X cluster

This step will guide you how to connect to the PolarDB-X cluster deployed through K8s.

1. Execute the following command to view the PolarDB-X cluster login password.

kubectl get secret polardb-x -o jsonpath="{.data['polardbx_root']}" | base64 -d - | xargs echo "Password: "

The returned results are as follows, and you can view the PolarDB-X cluster login password.

2. Click the + icon in the upper right corner to create a new terminal 2, and execute the following command to forward the PolarDB-X cluster port to port 3306.

Before using MySQL Client to log in to the PolarDB-X cluster deployed through k8s, you need to obtain the PolarDB-X cluster login password and port forwarding.

su galaxykube kubectl port-forward svc/polardb-x 3306

3. Execute the following command to connect to the PolarDB-X cluster.

illustrate:

  • You need to replace <PolarDB-X cluster login password> with the obtained PolarDB-X cluster login password.
  • If mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 error is reported, please wait a moment Minutes, re-forward the port and connect to the PolarDB-X cluster.
mysql -h127.0.0.1 -P3306 -upolardbx_root -p<PolarDB-X集群登录密码>

Use of TTL function

1. Create a database.

Execute the following SQL statement to create a database and use it.

CREATE DATABASE local_partition mode='auto'; use local_partition;

2. Create a TTL table.

To learn the CREATE TABLE syntax for creating a TTL table, see Creating a TTL Table .

Referring to the document, execute the following SQL prediction, create a partition table, divide the partitions according to the granularity of the month, and the partition will expire after 12 months.

Need to master the meaning of several key parameters: INTERVAL, EXPIRE AFTER, PRE ALLOCATE.

CREATE TABLE t_order (
    id bigint NOT NULL AUTO_INCREMENT,
    gmt_modified DATETIME NOT NULL,
    PRIMARY KEY (id, gmt_modified)
)
LOCAL PARTITION BY RANGE (gmt_modified)
STARTWITH '2021-01-01'
INTERVAL 1 MONTH
EXPIRE AFTER 12
PRE ALLOCATE 3
PIVOTDATE NOW()
DISABLE SCHEDULE
;
SHOW FULL CREATE TABLE t_order\G;

3. View the metadata of the TTL table.

The information_schema.local_partitions view records all physical partitions. Execute the following SQL statement to query the expiration time of the physical partition and the range of data through the select statement.

select * from information_schema.local_partitions where table_name = 't_order' and table_schema='local_partition'\G;

4. Create a new physical time partition

To learn how to create new physical time partitions for TTL tables, see Changing TTL Tables .

Create physical partitions in advance through DDL commands. Calling this command multiple times will not create unlimited new partitions, but will only create new partitions within a predetermined time. If a TTL scheduled task is created, the TTL scheduled task will automatically call this command.

ALTER TABLE t_order ALLOCATE LOCAL PARTITION;

5. Regardless of the time partition for invalidation and expiration.

Use the DDL command to delete expired physical partitions. This command only deletes expired partitions.

ALTER TABLE t_order EXPIRE LOCAL PARTITION;  select * from information_schema.local_partitions where table_name = 't_order' and table_schema='local_partition'\G;

6. Create a scheduled task.

The above two DDL commands can make the physical partition of the TTL table roll according to time, but we still need to call it manually.

TTL provides a scheduled task function, which can automatically perform partition rolling.

To learn how to create, view, and delete scheduled tasks in TTL tables, see Scheduled Tasks in TTL Tables .

CREATE SCHEDULE  FOR LOCAL_PARTITION ON `local_partition`.`t_order`  CRON '0 0 12 1/5 * ?'  TIMEZONE '+08:00';

7. View scheduled tasks.

View the scheduled tasks that have been created so far.

SELECT * FROM INFORMATION_SCHEMA.LOCAL_PARTITIONS_SCHEDULE\G;

8. Manually trigger a timed task.

set global BACKGROUND_TTL_EXPIRE_END_TIME="23:59"; FIRE SCHEDULE <id>;

9. View the timing task execution results.

SHOW SCHEDULE RESULT <id>;

10. Delete scheduled tasks

According to the id of the scheduled task, delete the scheduled task.

DROP SCHEDULE <id>;

11. TTL table conversion.

TTL tables can be converted into ordinary tables, and ordinary tables can also be converted into TTL tables

To learn how to convert the TTL table, see Changing the TTL Table .

ALTER TABLE t_order REMOVE LOCAL PARTITIONING;
ALTER TABLE t_order
LOCAL PARTITION BY RANGE (gmt_modified)
INTERVAL 1 MONTH
EXPIRE AFTER 36
PRE ALLOCATE 1
;
Click to try cloud products for free now to start the practical journey on the cloud!

Original link

This article is the original content of Alibaba Cloud and may not be reproduced without permission.

Guess you like

Origin blog.csdn.net/yunqiinsight/article/details/131811099