Sharding扩容方案-2(实现)

栏目: 数据库 · 发布时间: 7年前

内容简介:Sharding扩容方案-2(实现)

数据库扩容配置设计方案:

CREATE TABLE `business_unit` (
   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '唯一主键',
   `name` varchar(100) NOT NULL COMMENT '业务标志',
   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 '业务线id',
   `start_id` bigint(20) NOT NULL COMMENT '开始id',
   `end_id` bigint(20) NOT NULL COMMENT '结束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 '分片组id',
   `ip` varchar(100) NOT NULL COMMENT '数据库ip',
   `port` varchar(100) NOT NULL COMMENT '数据库端口号',
   `db_name` varchar(100) NOT NULL COMMENT '数据库名字',
   `hash` tinyint(4) NOT NULL COMMENT '取模值',
   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 '业务主键开始id',
   `end_id` bigint(20) NOT NULL COMMENT '业务主键结束id',
   `table_name` varchar(100) NOT NULL COMMENT '表的名字',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8

init数据:

/*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');

以上四张表,是对扩容的配置:

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;

输出为:

Sharding扩容方案-2(实现)


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Agile Web Development with Rails 4

Agile Web Development with Rails 4

Sam Ruby、Dave Thomas、David Heinemeier Hansson / Pragmatic Bookshelf / 2013-10-11 / USD 43.95

Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details. Tens of thousands of deve......一起来看看 《Agile Web Development with Rails 4》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

html转js在线工具
html转js在线工具

html转js在线工具