当前位置:网站首页>Initial experience of creating partitioned tables under MySQL cases tdsql for MySQL (I)

Initial experience of creating partitioned tables under MySQL cases tdsql for MySQL (I)

2022-06-24 02:18:00 Yaochong

installed TDSQL for MySQL after , Log in to the red rabbit platform , The first thing that comes into view is the overview of the cluster , Display the overall information of the cluster , For example, the amount used 、 Total capacity, etc , You can click in “ Get into ”, Enter instance management .

Or click instance management to enter the instance , We choose 【 Distributed 】 example , Click to enter

After entering, it will display , Example details 、SET management 、 Database management 、DB monitor 、Proxy Monitoring and other information , The following figure shows the example details

The following figure for SET management

You can see , The current two SET, The cluster is set to 4 A shard , Then we create tables based on the sharding

Let's change the following table into a distributed table , According to the customer ID Slice

CREATE TABLE `orders` (
  `o_orderkey` int(11) NOT NULL,
  `O_CUSTKEY` int(11) NOT NULL,
  `O_ORDERSTATUS` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `O_TOTALPRICE` decimal(15,2) NOT NULL,
  `O_ORDERDATE` date NOT NULL,
  `O_ORDERPRIORITY` char(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `O_CLERK` char(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `O_SHIPPRIORITY` int(11) NOT NULL,
  `O_COMMENT` varchar(79) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  PRIMARY KEY (`o_orderkey`),
  KEY `orders_fk1` (`O_CUSTKEY`),
  KEY `i_orders_date_clerk` (`O_ORDERDATE`,`O_CLERK`),
  KEY `i_orders_clerk_date` (`O_CLERK`,`O_ORDERDATE`),
  CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`O_CUSTKEY`) REFERENCES `customer` (`c_custkey`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci

The list is as follows

The index section is as follows , No foreign key options

Click Create , as for DATE type , What default values need to be added , I didn't know how to specify the default value !!! So it's changed to datetime type , And in the attribute on update current_timestamp

If you encounter Tips :A PRIMARY KEY must include all columns in the table's partitioning function

For the above problem, add the partition key to the primary key !!!

View the table creation statement

CREATE TABLE `orders` (
  `o_orderkey` int(11) NOT NULL,
  `O_CUSTKEY` int(11) NOT NULL,
  `O_ORDERSTATUS` char(1) NOT NULL DEFAULT '',
  `O_TOTALPRICE` decimal(15,2) NOT NULL,
  `O_ORDERDATE` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `O_ORDERPRIORITY` char(15) NOT NULL DEFAULT '',
  `O_CLERK` char(15) NOT NULL DEFAULT '',
  `O_SHIPPRIORITY` int(11) NOT NULL,
  `O_COMMENT` varchar(79) NOT NULL DEFAULT '',
  PRIMARY KEY (`o_orderkey`,`O_CUSTKEY`),
  KEY `orders_fk1` (`O_CUSTKEY`),
  KEY `i_orders_date_clerk` (`O_ORDERDATE`,`O_CLERK`),
  KEY `i_orders_clerk_date` (`O_CLERK`,`O_ORDERDATE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 shardkey=o_custkey

You can see shardkey=o_custkey keyword

summary

1、TDSQL You need to specify keywords when creating a sub table shardkey, And the primary key should contain the fragment field .

2、 The production environment can be created in batches by script .

More articles are welcome to follow my official account , search dbachongzi Or scan QR code

author : Yao Chong Oracle OCM、MySQL OCP、Oceanbase OBCA、PingCAP PCTA authentication , Good at based on Oracle、MySQL Performance Turning And a variety of relational NoSQL database .

原网站

版权声明
本文为[Yaochong]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211103132850072K.html