Skip to content

Instantly share code, notes, and snippets.

@fdwills
Last active August 29, 2015 14:10
Show Gist options
  • Save fdwills/1136d33ab5ec30a2f298 to your computer and use it in GitHub Desktop.
Save fdwills/1136d33ab5ec30a2f298 to your computer and use it in GitHub Desktop.

DB分库、分表

  1. peiwo_common: 存储应用通用数据,存储user的shard数据,存储部分跨用户的数据
  2. peiwo_shard#{1,2,3,4}系列: 主要存储用户数据,以uid分库。存储部分跨用户的数据

1. peiwo_common数据库

第一类: 存储无uid master表

  • pw_shard
column 说明
id shard_id
weight 权重
  • pw_user_shard
column 说明
uid pw_user表里的uid,用于shard的key
shard_id 此uid所属的shard
  • pw_captcha

第二类: 有uid和tuid表

存储在common的好处是方便查询,后期partition

  • pw_contact
  • pw_contact_request
  • pw_contact_note
  • pw_payment
  • pw_message
  • pw_block

2. peiwo_shard1, peiwo_shard2, peiwo_shard3系列数据库

类别: 只有一个uid的表

  • pw_user
  • pw_event
  • pw_dialog
  • pw_push
  • pw_iap_error
  • pw_withdraw
  • pw_task
  • pw_call_ledger

新用户注册流程

  1. 查找pw_shard表,按权重随机选择shard_id
  2. 将(uid, shard_id)写入pw_user_shard表或redis

用户数据读取

  1. 按查找的数据类别,去common或者shard里面查找
  2. 查找pw_user_shard表或redis
  3. 去peiwo_shard#{shard_id}数据库里面查找数据

上线移植

  1. peiwo_common指向当前的peiwo-0的sneaky数据库
  2. peiwo_shard1指向当前的peiwo-0的sneaky数据库
  3. peiwo-1上新建数据库sneaky,peiwo_shard2指向该数据库
  4. peiwo-2上新建数据库sneaky,peiwo_shard3指向该数据库
  5. peiwo_common里建表pw_shard, 插入(1, 0), (2, 50), (3,50)
  6. peiwo_common里建表pw_user_shard,将当前所有用户的shard_id设为1插入pw_user_shard中
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment