ShardingSphere horizontal sub-table

Application Scenario

The data table is used to receive the data information of each project group. The data upload frequency is extremely high, and the data volume reaches 100,000 plus within a week. The simple structure of the data
table sys_order
insert image description here
is as follows: Due to the continuous increase in the data volume, the data volume of the project database table is too large . It is necessary to use shardingSphere to perform horizontal table division according to the groupId field, and each project group corresponds to a table, which is convenient for management and reduces the pressure on the data table.

Implementation steps

Import Maven dependency

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc-core</artifactId>
    <version>${shardingsphere.version}</version>
</dependency>

Configure the yaml file

spring:
  shardingsphere:
    mode:
      # 运行模式类型。可选配置:内存模式 Memory、单机模式 Standalone、集群模式 Cluster
      type: Memory
    props:
      #是否在日志中打印 SQL。
      #打印 SQL 可以帮助开发者快速定位系统问题。日志内容包含:逻辑 SQL,真实 SQL 和 SQL 解析结果。
      #如果开启配置,日志将使用 Topic ShardingSphere-SQL,日志级别是 INFO。 false
      sql-show: true
      #是否在日志中打印简单风格的 SQL。false
      sql-simple: false
      #用于设置任务处理线程池的大小。每个 ShardingSphereDataSource 使用一个独立的线程池,同一个 JVM 的不同数据源不共享线程池。
      executor-size: 20
      #次查询请求在每个数据库实例中所能使用的最大连接数。1
      max-connections-size-per-query: 1
      #在程序启动和更新时,是否检查分片元数据的结构一致性。
      check-table-metadata-enabled: false
      #在程序启动和更新时,是否检查重复表。false
      check-duplicate-table-enabled: false
    datasource:
      names: ds0
      ds0:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db0
        username: root
        password: root
    rules:
      sharding: #ShardingSphere-JDBC全局序列配置规则
        key-generators:
          # 雪花算法生成数据库id,不使用可以不设置
          snowflake:
            type: SNOWFLAKE
            props:
              worker-id: 123
        #分片算法配置
        sharding-algorithms:
          ## 分片算法名称
          fenpian-table-1:
            #对应 Override getType
            type: MOD # 使用取模分片算法
            props:
              sharding-count: 20 # 取模数量,暂时分20张表吧
        
        tables:
          # 逻辑表名称
          sys_order:
            # 由数据源名 + 表名组成(参考 Inline 语法规则)
            actual-data-nodes: ds0.sys_order${
    
    0..20}
            # 分表策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
            table-strategy:
              #standard: # 用于单分片键的标准分片场景
              standard:
                #数据库的键
                shardingColumn: group_id
                sharding-algorithm-name: fenpian-table-1
            # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一

Key configuration instructions

The sharing part is the algorithm we need to use to divide the table, one is the snowflake algorithm used to generate id, and the other is the modulo algorithm used by the table division. The tables part is the
insert image description here
configuration of the table, and the division table is used by the data source and the division strategy. This use does not temporarily divide the library
insert image description here

follow-up test

waiting to be added...

Guess you like

Origin blog.csdn.net/weixin_49363689/article/details/132249073