英文网站建设图片,天津网站制作网页,外贸建站seo优化,企业网站不付服务费应该怎么做1. Sharding-Jdbc 分库分表执⾏核⼼流程 Sharding-JDBC执行流程
1. SQL解析 - SQL优化 - SQL路由 - SQL改写 - SQL执⾏- 结果归并 -返回结果简写为#xff1a;解析-路由-改写-执⾏-结果归并1.1 SQL解析
1. SQL解析过程分为词法解析…1. Sharding-Jdbc 分库分表执⾏核⼼流程 Sharding-JDBC执行流程
1. SQL解析 - SQL优化 - SQL路由 - SQL改写 - SQL执⾏- 结果归并 -返回结果简写为解析-路由-改写-执⾏-结果归并1.1 SQL解析
1. SQL解析过程分为词法解析语法解析。
2. 词法解析词法解析器用于将SQL拆解为不可再分的原子符号称为Token,再使用语法解析器将SQL转换为抽象语法树。
3. 抽象语法树的遍历去提炼分片所需的上下文并标记有可能需要的SQL改写。1.2 两大SQL路由
1. 分片路由带分片键直接路由标准路由笛卡尔积路由
2. 广播路由不带分片键全库表路由全库路由全实例路由等。1.3 SQL改写
1. Logic SQL逻辑表SQL不能够直接在真实的数据库表中执行SQL改写会将逻辑SQL改写为在真实数据库中可以正确执行的ActualSQL.1.4 SQL执行
1. 采用自动化的执行引擎将路由和改写完之后的Actual SQL安全且高效发送给底层数据源执行自动平衡资源控制与执行效率
2. 两大模式内存限制模式数据库连接数量不做限制多线程并发执行效率最大化适用OLAP操作。连接限制模式严格控制对一次操作所耗费的数据库连接数量1库1线程多库多线程使用OLTP操作保证数据库资源被足够多应用使用。1.5结果归并
从各个数据节点获取的多数据结果集组合成为一个结果集并正确的返回至请求客户端功能上可分为遍历排序分组分页和聚合5种。
1. 结构划分两大类流式-归并每一次从结果集中获取到的数据都通过逐条获取的方式返回正确的单条数据它与数据库原生的返回结果集的方式最为契合。占用了额外的带宽但不会导致内存暴涨使用的最多。内存-归并分片结果集的数据存储在内存中再通过统一的分组排序以及聚合等计算之后再将其封装成为逐条访问的数据结果集返回消耗内存。2. 标准分⽚策略-精准分⽚算法《分表》
2.1 标准分⽚策略-精准分⽚算法
StandardShardingStrategy
1. 只⽀持【单分⽚键】提供PreciseShardingAlgorithm和RangeShardingAlgorithm两个分⽚算法.
2. PreciseShardingAlgorithm 精准分⽚ 是必选的⽤于处理和IN的分⽚.
3. RangeShardingAlgorithm 范围分⽚ 是可选的⽤于处理BETWEEN AND分⽚.
4. 如果不配置RangeShardingAlgorithm如果SQL中⽤了BETWEEN AND语法则将按照全库路由处理性能下降.2.2 代码
package com.dss.sharding.strategy;import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;import java.util.Collection;/****/
public class CustomTablePreciseShardingAlgorithm implements PreciseShardingAlgorithmLong {/**** param dataSourceNames 数据源集合* 在分库时值为所有分片库的集合 databaseNames* 分表时为对应分片库中所有分片表的集合 tablesNames** param shardingValue 分片属性包括* logicTableName 为逻辑表* columnName 分片健字段* value 为从 SQL 中解析出的分片健的值* return*/Overridepublic String doSharding(CollectionString dataSourceNames, PreciseShardingValueLong preciseShardingValue) {for(String datasourceName : dataSourceNames){/**通过取模获取命中的表* value是0则进⼊0库表1则进⼊1库表*/String value preciseShardingValue.getValue() % dataSourceNames.size() ;//product_order_0表示命中库if(datasourceName.endsWith(value)){return datasourceName;}}return null;}
}
2.3配置 spring.application.namesharding-jdbc
server.port8080# 打印执行的数据库以及语句
spring.shardingsphere.props.sql.showtrue# 数据源 db0
spring.shardingsphere.datasource.namesds0,ds1# 第一个数据库
spring.shardingsphere.datasource.ds0.typecom.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-namecom.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-urljdbc:mysql://127.0.0.1:3306/xdclass_shop_order_0?useUnicodetruecharacterEncodingutf-8useSSLfalseserverTimezoneAsia/ShanghaiallowPublicKeyRetrievaltrue
spring.shardingsphere.datasource.ds0.usernameroot
spring.shardingsphere.datasource.ds0.passwordroot# 第二个数据库
spring.shardingsphere.datasource.ds1.typecom.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-namecom.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-urljdbc:mysql://127.0.0.1:3306/xdclass_shop_order_1?useUnicodetruecharacterEncodingutf-8useSSLfalseserverTimezoneAsia/ShanghaiallowPublicKeyRetrievaltrue
spring.shardingsphere.datasource.ds1.usernameroot
spring.shardingsphere.datasource.ds1.passwordroot#配置workId
spring.shardingsphere.sharding.tables.product_order.key-generator.props.worker.id1#配置广播表
spring.shardingsphere.sharding.broadcast-tablesad_config
spring.shardingsphere.sharding.tables.ad_config.key-generator.columnid
spring.shardingsphere.sharding.tables.ad_config.key-generator.typeSNOWFLAKE#配置【默认分库策略】
#spring.shardingsphere.sharding.default-database-strategy.inline.sharding-columnuser_id
#spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expressionds$-{user_id % 2 }
#配置分库规则
#spring.shardingsphere.sharding.tables.product_order.database-strategy.inline.sharding-columnuser_id
#spring.shardingsphere.sharding.tables.product_order.database-strategy.inline.algorithm-expressionds$-{user_id % 2 }#id生成策略
spring.shardingsphere.sharding.tables.product_order.key-generator.columnid
spring.shardingsphere.sharding.tables.product_order.key-generator.typeSNOWFLAKE# 指定product_order表的数据分布情况配置数据节点,行表达式标识符使用 ${...} 或 $-{...}
# 但前者与 Spring 本身的文件占位符冲突所以在 Spring 环境中建议使用 $-{...}
#spring.shardingsphere.sharding.tables.product_order.actual-data-nodesds0.product_order_$-{0..1}
#spring.shardingsphere.sharding.tables.product_order.actual-data-nodesds$-{0..1}.product_order_$-{0..1}
# 指定product_order表的分片策略分片策略包括【分片键和分片算法】
#spring.shardingsphere.sharding.tables.product_order.table-strategy.inline.sharding-columnid
#spring.shardingsphere.sharding.tables.product_order.table-strategy.inline.algorithm-expressionproduct_order_$-{id % 2}# 指定product_order_item表的分片策略分片策略包括【分片键和分片算法】
#spring.shardingsphere.sharding.tables.product_order_item.actual-data-nodesds$-{0..1}.product_order_item_$-{0..1}
#spring.shardingsphere.sharding.tables.product_order_item.table-strategy.inline.sharding-columnproduct_order_id
#spring.shardingsphere.sharding.tables.product_order_item.table-strategy.inline.algorithm-expressionproduct_order_item_$-{product_order_id % 2}#配置绑定表
#spring.shardingsphere.sharding.binding‐tables[0] product_order,product_order_item#精准分片-水平分表
# 指定product_order表的数据分布情况配置数据节点,在 Spring 环境中建议使用 $-{...}
spring.shardingsphere.sharding.tables.product_order.actual-data-nodesds0.product_order_$-{0..1}#指定精准分片算法(水平分表)
spring.shardingsphere.sharding.tables.product_order.table-strategy.standard.sharding-columnid
spring.shardingsphere.sharding.tables.product_order.table-strategy.standard.precise-algorithm-class-namecom.dss.sharding.strategy.CustomTablePreciseShardingAlgorithm
2.4 测试
Testpublic void testSaveProductOrder(){Random random new Random();for(int i0; i20;i){ProductOrderDO productOrderDO new ProductOrderDO();productOrderDO.setCreateTime(new Date());productOrderDO.setNickname(PreciseShardingAlgorithm ii);productOrderDO.setOutTradeNo(UUID.randomUUID().toString().substring(0,32));productOrderDO.setPayAmount(100.00);productOrderDO.setState(PAY);productOrderDO.setUserId( Long.valueOf(random.nextInt(50)) );productOrderMapper.insert(productOrderDO);}}