当前位置:网站首页>DistSQL 深度解析:打造动态化的分布式数据库
DistSQL 深度解析:打造动态化的分布式数据库
2022-07-21 16:29:00 【InfoQ】
一、背景
- 在线创建逻辑库;
- 动态配置规则(包括分片、数据加密、读写分离、数据库发现、影子库、全局规则等);
- 实时调整存储资源;
- 即时切换事务类型;
- 随时开关 SQL 日志;
- 预览 SQL 路由结果;
- ...同时,随着使用场景的深入,越来越多的 DistSQL 特性被发掘出来,众多宝藏语法也受到了用户的喜爱。
二、内容提要

三、实战演练
3.1 场景需求
- 创建两张分片表
t_order
和t_order_item
;
- 两张表均以
user_id
字段分库,以order_id
字段分表;
- 分片数量为 2 库 x 3 表;

3.2 环境准备
- 准备可供访问的 MySQL 数据库实例,创建两个新库
demo_ds_0
、demo_ds_1
;
mode:
type: Cluster
repository:
type: ZooKeeper
props:
namespace: governance_ds
server-lists: localhost:2181 # ZooKeeper 地址
retryIntervalMilliseconds: 500
timeToLiveSeconds: 60
maxRetries: 3
operationTimeoutMilliseconds: 500
overwrite: false
rules:
- !AUTHORITY
users:
- [email protected]%:root
- 启动 ShardingSphere-Proxy,并使用客户端连接到 Proxy,例如
mysql -h 127.0.0.1 -P 3307 -u root -p
3.3 添加存储资源
- 创建逻辑数据库
CREATE DATABASE sharding_db;USE sharding_db;
- 添加存储资源,对应之前准备的 MySQL 数据库
ADD RESOURCE ds_0 (
HOST=127.0.0.1,
PORT=3306,
DB=demo_ds_0,
USER=root,
PASSWORD=123456
), ds_1(
HOST=127.0.0.1,
PORT=3306,
DB=demo_ds_1,
USER=root,
PASSWORD=123456
);
- 查看存储资源
mysql> SHOW DATABASE RESOURCES\\G;
*************************** 1. row ***************************
name: ds_1
type: MySQL
host: 127.0.0.1
port: 3306
db: demo_ds_1
-- 省略部分属性
*************************** 2. row ***************************
name: ds_0
type: MySQL
host: 127.0.0.1
port: 3306
db: demo_ds_0
-- 省略部分属性
3.4 创建分片规则
3.4.1 主键生成器
- 创建主键生成器
CREATE SHARDING KEY GENERATOR snowflake\_key\_generator \(
TYPE(NAME=SNOWFLAKE)
);
- 查询主键生成器
mysql> SHOW SHARDING KEY GENERATORS;
+-------------------------+-----------+-------+
| name | type | props |
+-------------------------+-----------+-------+
| snowflake_key_generator | snowflake | {} |
+-------------------------+-----------+-------+
1 row in set (0.01 sec)
3.4.2 分片算法
- 创建一个分库算法,由
t_order
和t_order_item
共用
-- 分库时按 user_id 对 2 取模
CREATE SHARDING ALGORITHM database_inline (
TYPE(NAME=INLINE,PROPERTIES("algorithm-expression"="ds_${user_id % 2}"))
);
- 为
t_order
和t_order_item
创建不同的分表算法
-- 分表时按 order_id 对 3 取模
CREATE SHARDING ALGORITHM t_order_inline (
TYPE(NAME=INLINE,PROPERTIES("algorithm-expression"="t_order_${order_id % 3}"))
);
CREATE SHARDING ALGORITHM t_order_item_inline (
TYPE(NAME=INLINE,PROPERTIES("algorithm-expression"="t_order_item_${order_id % 3}"))
);
- 查询分片算法
mysql> SHOW SHARDING ALGORITHMS;
+---------------------+--------+---------------------------------------------------+
| name | type | props |
+---------------------+--------+---------------------------------------------------+
| database_inline | inline | algorithm-expression=ds_${user_id % 2} |
| t_order_inline | inline | algorithm-expression=t_order_${order_id % 3} |
| t_order_item_inline | inline | algorithm-expression=t_order_item_${order_id % 3} |
+---------------------+--------+---------------------------------------------------+
3 rows in set (0.00 sec)
3.4.3 默认分片策略
t_order
t_order_item
- 创建默认分库策略
CREATE DEFAULT SHARDING DATABASE STRATEGY (
TYPE=STANDARD,SHARDING_COLUMN=user_id,SHARDING_ALGORITHM=database_inline
);
- 查询默认策略
mysql> SHOW DEFAULT SHARDING STRATEGY\G;
*************************** 1. row ***************************
name: TABLE
type: NONE
sharding_column:
sharding_algorithm_name:
sharding_algorithm_type:
sharding_algorithm_props:
*************************** 2. row ***************************
name: DATABASE
type: STANDARD
sharding_column: user_id
sharding_algorithm_name: database_inline
sharding_algorithm_type: inline
sharding_algorithm_props: {algorithm-expression=ds_${user_id % 2}}
2 rows in set (0.00 sec)
3.4.4 分片规则
- t_order
CREATE SHARDING TABLE RULE t_order (
DATANODES("ds_${0..1}.t_order_${0..2}"),
TABLE_STRATEGY(TYPE=STANDARD,SHARDING_COLUMN=order_id,SHARDING_ALGORITHM=t_order_inline),
KEY_GENERATE_STRATEGY(COLUMN=order_id,KEY_GENERATOR=snowflake_key_generator)
);
- DATANODES 指定了分片表的数据节点;
- TABLE_STRATEGY 指定了分表策略,其中 SHARDING_ALGORITHM 使用了已创建好的分片算法t_order_inline;
- KEY_GENERATE_STRATEGY 指定该表的主键生成策略,若不需要主键生成,可省略该配置。
- t_order_item
CREATE SHARDING TABLE RULE t_order_item (
DATANODES("ds_${0..1}.t_order_item_${0..2}"),
TABLE_STRATEGY(TYPE=STANDARD,SHARDING_COLUMN=order_id,SHARDING_ALGORITHM=t_order_item_inline),
KEY_GENERATE_STRATEGY(COLUMN=order_item_id,KEY_GENERATOR=snowflake_key_generator)
);
- 查询分片规则
mysql> SHOW SHARDING TABLE RULES\G;
*************************** 1. row ***************************
table: t_order
actual_data_nodes: ds_${0..1}.t_order_${0..2}
actual_data_sources:
database_strategy_type: STANDARD
database_sharding_column: user_id
database_sharding_algorithm_type: inline
database_sharding_algorithm_props: algorithm-expression=ds_${user_id % 2}
table_strategy_type: STANDARD
table_sharding_column: order_id
table_sharding_algorithm_type: inline
table_sharding_algorithm_props: algorithm-expression=t_order_${order_id % 3}
key_generate_column: order_id
key_generator_type: snowflake
key_generator_props:
*************************** 2. row ***************************
table: t_order_item
actual_data_nodes: ds_${0..1}.t_order_item_${0..2}
actual_data_sources:
database_strategy_type: STANDARD
database_sharding_column: user_id
database_sharding_algorithm_type: inline
database_sharding_algorithm_props: algorithm-expression=ds_${user_id % 2}
table_strategy_type: STANDARD
table_sharding_column: order_id
table_sharding_algorithm_type: inline
table_sharding_algorithm_props: algorithm-expression=t_order_item_${order_id % 3}
key_generate_column: order_item_id
key_generator_type: snowflake
key_generator_props:
2 rows in set (0.00 sec)
t_order
t_order_item
3.5 语法糖
t_order_detail
CREATE SHARDING TABLE RULE t_order_detail (
DATANODES("ds_${0..1}.t_order_detail_${0..1}"),
DATABASE_STRATEGY(TYPE=STANDARD,SHARDING_COLUMN=user_id,SHARDING_ALGORITHM(TYPE(NAME=INLINE,PROPERTIES("algorithm-expression"="ds_${user_id % 2}")))),
TABLE_STRATEGY(TYPE=STANDARD,SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME=INLINE,PROPERTIES("algorithm-expression"="t_order_detail_${order_id % 3}")))),
KEY_GENERATE_STRATEGY(COLUMN=detail_id,TYPE(NAME=snowflake))
);
t_order_detail
- 主键生成器
mysql> SHOW SHARDING KEY GENERATORS;
+--------------------------+-----------+-------+
| name | type | props |
+--------------------------+-----------+-------+
| snowflake_key_generator | snowflake | {} |
| t_order_detail_snowflake | snowflake | {} |
+--------------------------+-----------+-------+
2 rows in set (0.00 sec)
- 分片算法
mysql> SHOW SHARDING ALGORITHMS;
+--------------------------------+--------+-----------------------------------------------------+
| name | type | props |
+--------------------------------+--------+-----------------------------------------------------+
| database_inline | inline | algorithm-expression=ds_${user_id % 2} |
| t_order_inline | inline | algorithm-expression=t_order_${order_id % 3} |
| t_order_item_inline | inline | algorithm-expression=t_order_item_${order_id % 3} |
| t_order_detail_database_inline | inline | algorithm-expression=ds_${user_id % 2} |
| t_order_detail_table_inline | inline | algorithm-expression=t_order_detail_${order_id % 3} |
+--------------------------------+--------+-----------------------------------------------------+
5 rows in set (0.00 sec)
- 分片规则
mysql> SHOW SHARDING TABLE RULES\G;
*************************** 1. row ***************************
table: t_order
actual_data_nodes: ds_${0..1}.t_order_${0..2}
actual_data_sources:
database_strategy_type: STANDARD
database_sharding_column: user_id
database_sharding_algorithm_type: inline
database_sharding_algorithm_props: algorithm-expression=ds_${user_id % 2}
table_strategy_type: STANDARD
table_sharding_column: order_id
table_sharding_algorithm_type: inline
table_sharding_algorithm_props: algorithm-expression=t_order_${order_id % 3}
key_generate_column: order_id
key_generator_type: snowflake
key_generator_props:
*************************** 2. row ***************************
table: t_order_item
actual_data_nodes: ds_${0..1}.t_order_item_${0..2}
actual_data_sources:
database_strategy_type: STANDARD
database_sharding_column: user_id
database_sharding_algorithm_type: inline
database_sharding_algorithm_props: algorithm-expression=ds_${user_id % 2}
table_strategy_type: STANDARD
table_sharding_column: order_id
table_sharding_algorithm_type: inline
table_sharding_algorithm_props: algorithm-expression=t_order_item_${order_id % 3}
key_generate_column: order_item_id
key_generator_type: snowflake
key_generator_props:
*************************** 3. row ***************************
table: t_order_detail
actual_data_nodes: ds_${0..1}.t_order_detail_${0..1}
actual_data_sources:
database_strategy_type: STANDARD
database_sharding_column: user_id
database_sharding_algorithm_type: inline
database_sharding_algorithm_props: algorithm-expression=ds_${user_id % 2}
table_strategy_type: STANDARD
table_sharding_column: order_id
table_sharding_algorithm_type: inline
table_sharding_algorithm_props: algorithm-expression=t_order_detail_${order_id % 3}
key_generate_column: detail_id
key_generator_type: snowflake
key_generator_props:
3 rows in set (0.01 sec)
CREATE SHARDING TABLE RULE
3.6 配置验证
3.6.1 检查节点分布
SHOW SHARDING TABLE NODES

3.6.2 SQL 预览
PREVIEW sql
- 无分片键查询,全路由



3.7 辅助查询 DistSQL
3.7.1 查询未使用的资源
- 语法:SHOW UNUSED RESOURCES
- 示例:

3.7.2 查询未使用的主键生成器
- 语法:SHOW UNUSED SHARDING KEY GENERATORS
- 示例:

3.7.3 查询未使用的分片算法

3.7.4 查询使用目标存储资源的规则
- 语法:SHOW RULES USED RESOURCE
- 示例:
3.7.5 查询使用目标主键生成器的分片规则
- 语法:SHOW SHARDING TABLE RULES USED KEY GENERATOR
- 示例:
3.7.6 查询使用目标算法的分片规则
- 语法:SHOW SHARDING TABLE RULES USED ALGORITHM
- 示例:
4、结语
5、参考文献
- 概念 DistSQL:https://shardingsphere.apache.org/document/current/cn/concepts/distsql/
- 概念 分布式主键:https://shardingsphere.apache.org/document/current/cn/features/sharding/concept/key-generator/
- 概念 分片策略:https://shardingsphere.apache.org/document/current/cn/features/sharding/concept/sharding/
- 概念 行表达式:https://shardingsphere.apache.org/document/current/cn/features/sharding/concept/inline-expression/
- 内置分片算法:https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/builtin-algorithm/sharding/
- 用户手册:DistSQLhttps://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-proxy/distsql/syntax/
作者
边栏推荐
- Geek planet ByteDance one stop data governance solution and platform architecture
- DS二叉树——二叉树之父子结点
- Development dynamics | stonedb 2022 release milestone
- 使用OpenCv+Arduino实现挂机自动打怪
- kubernetes 静态存储与动态存储
- Dynamics crm: how to search entity records associated with connection through advanced search
- js原生——数组排序 去重 找出最大数 字符串出现最多的字符
- Read the paper with me - multi model text recognition network
- Front line engineers tell you the real status and development prospects of embedded "suggestions collection"
- web3再牛 也沒能逃出這幾個老巨頭的手掌心
猜你喜欢
Dynamics crm: encountered "the plug-in execution failed because no sandbox hosts are currently available“
leetcode-09(下一个排列+快乐数+全排列)
mysql搭建主从同步-手把手使用docker搭建
如何让游戏中的随机因素重新赢得玩家信任
DS二叉树—二叉树结点的最大距离
AG. DS二叉树--层次遍历
Dynamics crm: xrmtoolbox plug-in recommendation
Wechat side: what is consistent hash, usage scenarios, and what problems have been solved?
Dynamics crm: when removing a control from the form, the prompt "the field you are trying to remove is required by the system or business"“
MemoryThrashing:抖音直播解决内存抖动实践
随机推荐
Dynamics crm: how to monitor and manage workflow processes and view their running history
CString转int
What are the common exceptions?
Outlook 教程,如何在 Outlook 中创建任务和待办事项?
Leetcode 1288. 删除被覆盖区间(可以,已解决)
ROS机械臂 Movelt 学习笔记1 | 基础准备
文件上传下载与Excel、数据表数据之间的转换
Wechat payment native (I) preparation and related knowledge
国家互联网信息办公室有关负责人就对滴滴全球股份有限公司依法作出网络安全审查相关行政处罚的决定答记者问
编程学习的资料分享
看完这个,还不会DVMA,请你吃瓜
mysql搭建主从同步-手把手使用docker搭建
How to isomorphism + cross end, knowing applet +kbone+finclip is enough!
微信支付Native(一)准备和相关知识
Web3 n'a jamais échappé à ces vieux géants.
After reading this, I can't DVMA yet. Please eat melon
一线工程师告诉你嵌入式真实现状与发展前景「建议收藏」
The relevant person in charge of the state Internet Information Office answered reporters' questions on the decision to impose administrative penalties related to network security review on didi Globa
Dynamics crm: how to search entity records associated with connection through advanced search
微信小程序 wx.request的简单封装