分库分表详解 下-分库分表方案模版

152 阅读5分钟

分库分表详解 上-基础概念

表使用情况

查询条件

方案对比

方案ShardingSphere自己实现ShardingSphere-proxy
分表根据userid做分表xx变为 根据时间每天一张表1. 功能点和ShardingSphere-jdbc相同
2. 解决了ShardingSphere-jdbc分表后业务无法正常使用数据库无查询和做数据抽取的问题
但会损失部分性能
查询支持情况查询需要携带分片键 分片算法采用精确 (精确仅支持in 和=)必须根据分片键查询映射表找到对应表 如需要支持更多场景需要手动编写代码
是否可以支持现有功能可以可以
复杂度一般较复杂
现有数据迁移需要预先建立好表并写停机导入数据 如不停机则需要双写比较麻烦简单只需将现有数据保存到映射表即可
分表扩展性一般 分表之后的扩容较麻烦较好因为按照时间分故可以灵活扩展
查询扩展性较好可以实现对业务透明化查询一般必须携带分片查询
性能一般
开发时间较长
总结数据迁移和扩展较麻烦 使用简单数据迁移和扩展较简单 需要自己实现可能有场景考虑不到 而且开发较麻烦

影响业务

业务使用场景语句调整

具体方案

数据量预估

按照每个订单 平均xxxx条数据 最多xx条数据 每月xx订单 每年xx订单 计算可得每年约为xx亿

表拆分方案

分表数量

为何我们经常使用 2 的 N 次方作为分片数量

表名

根据计算分512张表

分片键和算法策略

分片算法 :: ShardingSphere

表名

分片键:userID

算法:MOD

策略:行表达式 精确 策略

数据迁移

分库分表详解 中- 数据迁移

shardingsphere配置

#应用名称
spring:
  application:
    name: sharding-jdbc-test

  #开发环境设置
  profiles:
    active: dev

  #配置为内存模式
  shardingsphere:
    mode:
      type: Memory

    # 配置真实数据源
    datasource:
      names: server-order00,server-order01

      # 配置第 1 个数据源
      server-order00:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/sharding_jdbc_order01
        username: root
        password: 123456

      # 配置第 2 个数据源
      server-order01:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/sharding_jdbc_order02
        username: root
        password: 123456


    #--------------------------------------标准分片的配置(数据节点配置)
    #由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持 inline 表达式。
    #缺省表示使用已知数据源与逻辑表名称生成数据节点,用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
    #https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/rules/sharding/
    rules:
      sharding:
        tables: # 数据分片规则配置
          t_order: # 逻辑表名称
            actualDataNodes: server-order0$->{0..1}.t_order$->{0..1} # 由数据源名 + 表名组成(参考 Inline 语法规则)
            databaseStrategy: # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
              standard: # 用于单分片键的标准分片场景
                shardingColumn: user_id # 分片列名称(数据库列字段,用户名称)
                shardingAlgorithmName: t-order-inline # 分片算法名称
            tableStrategy: # 分表策略,同分库策略
              standard: # 用于单分片键的标准分片场景
                shardingColumn: order_no # 分片列名称(数据库列字段,订单号)
                shardingAlgorithmName: t-order-hash-mod # 分片算法名称
            keyGenerateStrategy: # 分布式序列策略
              column: id # 自增列名称,缺省表示不使用自增主键生成器
              keyGeneratorName: kgan-snowflake # 分布式序列算法名称

          t_order_item: # 逻辑表名称
            actualDataNodes: server-order0$->{0..1}.t_order_item$->{0..1} # 由数据源名 + 表名组成(参考 Inline 语法规则)
            databaseStrategy: # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
              standard: # 用于单分片键的标准分片场景
                shardingColumn: user_id # 分片列名称(数据库列字段,用户名称)
                shardingAlgorithmName: t-order-inline # 分片算法名称
            tableStrategy: # 分表策略,同分库策略
              standard: # 用于单分片键的标准分片场景
                shardingColumn: order_no # 分片列名称(数据库列字段,订单号)
                shardingAlgorithmName: t-order-hash-mod # 分片算法名称
            keyGenerateStrategy: # 分布式序列策略
              column: id # 自增列名称,缺省表示不使用自增主键生成器
              keyGeneratorName: kgan-snowflake # 分布式序列算法名称


          t_dict: # 逻辑表名称
            actualDataNodes: server-order0$->{0..1}.t_dict # 由数据源名 + 表名组成(参考 Inline 语法规则)


        # ---------------------------------------分片算法配置
        #https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/algorithm/
        shardingAlgorithms:
          #行表达式分片算法
          t-order-inline: # 分片算法名称
            # type 和 props,请参考分片内置算法:https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/sharding/
            type: INLINE # 分片算法类型
            props: # 分片算法属性配置
              #t_order0$->{user_id % 2} 表示 t_order 表根据 user_id 取模(取余) 2,而分成 2 张表,表名称为 t_order01 到 t_order02
              algorithm-expression: server-order0$->{user_id % 2}
          #取模分片算法
          t-order-mod: # 分片算法名称
            # type 和 props,请参考分片内置算法:https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/sharding/
            type: MOD # 分片算法类型
            props: # 分片算法属性配置
              #分片数量
              sharding-count: 2
          #哈希取模分片算法
          t-order-hash-mod: # 分片算法名称
            # type 和 props,请参考分片内置算法:https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/sharding/
            type: HASH_MOD # 分片算法类型
            props: # 分片算法属性配置
              #分片数量
              sharding-count: 2
            # 分布式序列算法配置


        #---------------------------------------分布式序列算法
        #https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/keygen/
        keyGenerators:
          kgan-snowflake: # 分布式序列算法名称
            type: SNOWFLAKE # 分布式序列算法类型
            #props: # 分布式序列算法属性配置

        #指分片规则一致的一组分片表。 使用绑定表进行多表关联查询时,必须使用分片键进行关联,否则会出现笛卡尔积关联或跨库关联,从而影响查询效率。
        #例如:t_order 表和 t_order_item 表,均按照 order_id 分片,并且使用 order_id 进行关联,则此两张表互为绑定表关系。
        #绑定表之间的多表关联查询不会出现笛卡尔积关联,关联查询效率将大大提升
        #https://shardingsphere.apache.org/document/current/cn/features/sharding/concept/
        bindingTables: # 绑定表规则列表
          - t_order,t_order_item  #解决笛卡尔积查询

        #指所有的分片数据源中都存在的表,表结构及其数据在每个数据库中均完全一致。 适用于数据量不大且需要与海量数据的表进行关联查询的场景,例如:字典表。
        #https://shardingsphere.apache.org/document/current/cn/features/sharding/concept/
        broadcastTables: # 广播表规则列表
          - t_dict

    props:
      #是否在日志中打印 SQL 打印 SQL 默认值 false
      sql-show: true