Sharding expansion plan (implementation)

Database expansion configuration design scheme:

CREATE TABLE `business_unit` (
   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Unique primary key',
   `name` varchar(100) NOT NULL COMMENT 'Business flag',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8


CREATE TABLE `share_group` (
   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id',
   `business_id` bigint(20) NOT NULL COMMENT 'business line id',
   `start_id` bigint(20) NOT NULL COMMENT 'start_id',
   `end_id` bigint(20) NOT NULL COMMENT 'end id',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

CREATE TABLE `share` (
   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '分片主键id',
   `share_group_id` bigint(20) NOT NULL COMMENT 'shard group id',
   `ip` varchar(100) NOT NULL COMMENT 'database ip',
   `port` varchar(100) NOT NULL COMMENT 'Database port number',
   `db_name` varchar(100) NOT NULL COMMENT 'Database name',
   `hash` tinyint(4) NOT NULL COMMENT 'modulus value',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8

CREATE TABLE `share_table` (
   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '表主键id',
   `share_id` bigint(20) NOT NULL COMMENT '分片id',
   `start_id` bigint(20) NOT NULL COMMENT 'business primary key start id',
   `end_id` bigint(20) NOT NULL COMMENT 'business primary key end id',
   `table_name` varchar(100) NOT NULL COMMENT 'table name',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8


 

init data:

/*Data for the table `business_unit` */

insert  into `business_unit`(`id`,`name`) values (1,'user'),(2,'order'),(3,'product');

/*Data for the table `share` */

insert  into `share`(`id`,`share_group_id`,`ip`,`port`,`db_name`,`hash`) values (1,1,'127.0.0.1','3309','user_db_1',0),(2,1,'127.0.0.2','3309','user_db_2',1),(3,1,'127.0.0.3','3319','user_db_3',2),(4,1,'127.0.0.4','3313','user_db_4',3),(5,2,'127.0.0.1','3309','user_db_5',0),(6,2,'127.0.0.1','3309','user_db_6',1),(7,3,'127.0.0.1','3309','user_db_7',0),(8,3,'127.0.0.1','3309','user_db_8',1),(9,3,'127.0.0.1','3309','user_db_9',2);

/*Data for the table `share_group` */

insert  into `share_group`(`id`,`business_id`,`start_id`,`end_id`) values (1,1,1,500000),(2,1,500001,1000000),(3,1,1000001,1500000),(4,2,1,1000000),(5,2,1000001,2000000),(6,3,1,100000),(7,3,100001,500000);

/*Data for the table `share_table` */

insert  into `share_table`(`id`,`share_id`,`start_id`,`end_id`,`table_name`) values (1,1,1,200000,'t_user_1'),(2,1,200001,400000,'t_user_2'),(3,1,400001,500000,'t_user_3'),(4,2,1,500000,'t_user_4'),(5,3,1,300000,'t_user_5'),(6,3,300001,500000,'t_user_6'),(7,4,1,500000,'t_user_7'),(8,5,500001,700000,'t_user_8'),(9,5,700001,1000000,'t_user_9'),(10,6,500001,1000000,'t_user_10'),(11,7,1000001,1500000,'t_user_11'),(12,8,1000001,1500000,'t_user_12'),(13,9,1000001,1500000,'t_user_13');

 

 

The above four tables are the configuration for expansion:

SELECT
  b.name,
  g.start_id,
  g.end_id,
  s.hash,
  s.db_name,
  s.ip,
  s.port,
  
  t.start_id,
  t.end_id,
  t.table_name
FROM business_unit b,
  share_group g,
  `share`s,
  share_table t
WHERE b.id = g.business_id
    AND s.share_group_id = g.id
    AND t.share_id = s.id;
    

 

The output is:

 

Guess you like

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