
相對于單例資料庫的查詢操作,分布式資料查詢會有很多技術難題,本文記錄 Mysql 分庫分表 和 Elasticsearch Join 查詢的實作思路,了解分布式場景資料處理的設計方案,
一、Mysql 分庫分表 Join 查詢場景
sharding-jdbc
-
sharding-jdbc 代理了原始的 datasource, 實作 jdbc 規范來完成分庫分表的分發和組裝,應用層無感知, -
執行流程:SQL決議 => 執行器優化 => SQL路由 => SQL改寫 => SQL執行 => 結果歸并 io.shardingsphere.core.executor.ExecutorEngine#execute -
Join 陳述句的決議,決定了要分發 SQL 到哪些實體節點上,對應SQL路由, -
SQL 改寫就是要把原始(邏輯)表名,改為實際分片的表名, -
復雜情況下,Join 查詢分發的最多執行的次數 = 資料庫實體 × 表A分片數 × 表B分片數
Code Insight
/**
* 執行查詢 SQL 切入點,從這里可以完整 debug 執行流程
* @see ShardingPreparedStatement#execute()
* @see ParsingSQLRouter#route(String, List, SQLStatement) Join 查詢實際涉及哪些表,就是在路由規則里匹配得出來的,
*/
public boolean execute() throws SQLException {
try {
// 根據引數(決定分片)和具體的SQL 來匹配相關的實際 Table,
Collection<PreparedStatementUnit> preparedStatementUnits = route();
// 使用執行緒池,分發執行和結果歸并,
return new PreparedStatementExecutor(getConnection().getShardingContext().getExecutorEngine(), routeResult.getSqlStatement().getType(), preparedStatementUnits).execute();
} finally {
JDBCShardingRefreshHandler.build(routeResult, connection).execute();
clearBatch();
}
}
SQL 路由策略
# 列印的代碼,就是在上述route 得出 ExecutionUnits 后,列印的
sharding.jdbc.config.sharding.props.sql.show=true
-
StandardRoutingEngine binding-tables 模式 -
ComplexRoutingEngine 最復雜的情況,笛卡爾組合關聯關系,
-- 引數不明,不能定位分片的情況
select * from order o inner join order_item oi on o.order_id = oi.order_id
-- 路由結果
-- Actual SQL: db1 ::: select * from order_1 o inner join order_item_1 oi on o.order_id = oi.order_id
-- Actual SQL: db1 ::: select * from order_1 o inner join order_item_0 oi on o.order_id = oi.order_id
-- Actual SQL: db1 ::: select * from order_0 o inner join order_item_1 oi on o.order_id = oi.order_id
-- Actual SQL: db1 ::: select * from order_0 o inner join order_item_0 oi on o.order_id = oi.order_id
-- Actual SQL: db0 ::: select * from order_1 o inner join order_item_1 oi on o.order_id = oi.order_id
-- Actual SQL: db0 ::: select * from order_1 o inner join order_item_0 oi on o.order_id = oi.order_id
-- Actual SQL: db0 ::: select * from order_0 o inner join order_item_1 oi on o.order_id = oi.order_id
-- Actual SQL: db0 ::: select * from order_0 o inner join order_item_0 oi on o.order_id = oi.order_id
elasticsearch-sql
-
這是個elasticsearch 插件,通過提供http 服務實作類 SQL 查詢的功能,高版本的elasticsearch 已經具備該功能? -
因為 elasticsearch 沒有 Join 查詢的特性,所以實作 SQL Join 功能,需要提供更加底層的功能,涉及到 Join 演算法,
Code Insight
/**
* Execute the ActionRequest and returns the REST response using the channel.
* @see ElasticDefaultRestExecutor#execute
* @see ESJoinQueryActionFactory#createJoinAction Join 演算法選擇
*/
@Override
public void execute(Client client, Map<String, String> params, QueryAction queryAction, RestChannel channel) throws Exception{
// sql parse
SqlElasticRequestBuilder requestBuilder = queryAction.explain();
// join 查詢
if(requestBuilder instanceof JoinRequestBuilder){
// join 演算法選擇,包括:HashJoinElasticExecutor、NestedLoopsElasticExecutor
// 如果關聯條件為等值(Condition.OPEAR.EQ),則使用 HashJoinElasticExecutor
ElasticJoinExecutor executor = ElasticJoinExecutor.createJoinExecutor(client,requestBuilder);
executor.run();
executor.sendResponse(channel);
}
// 其他型別查詢 ...
}
三、More Than Join
Join 演算法
-
常用三種 Join 演算法:Nested Loop Join,Hash Join、 Merge Join -
MySQL 只支持 NLJ 或其變種,8.0.18 版本后支持 Hash Join -
NLJ 相當于兩個嵌套回圈,用第一張表做 Outter Loop,第二張表做 Inner Loop,Outter Loop 的每一條記錄跟 Inner Loop 的記錄作比較,最終符合條件的就將該資料記錄, -
Hash Join 分為兩個階段; build 構建階段和 probe 探測階段, -
可以使用Explain 查看 MySQL 使用哪種 Join 演算法,需要的語法關鍵字:FORMAT=JSON or FORMAT=Tree
EXPLAIN FORMAT=JSON
SELECT * FROM
sale_line_info u
JOIN sale_line_manager o ON u.sale_line_code = o.sale_line_code;
{
"query_block": {
"select_id": 1,
// 使用的join 演算法:nested_loop
"nested_loop": [
// 涉及join 的表以及對應的 key,其他的資訊與常用explain 類似
{
"table": {
"table_name": "o",
"access_type": "ALL"
}
},
{
"table": {
"table_name": "u",
"access_type": "ref"
}
}
]
}
}
我們現在有個業務功能正好使用到 Nested型別, 在查詢和優化程序中,解決了非常大的難題,
總結
參考資料:
[1] 如何在分布式資料庫中實作 Hash Join:https://zhuanlan.zhihu.com/p/35040231
[2] 一文詳解MySQL——Join的使用優化:https://juejin.cn/post/7224046762200154172
作者|楊攀
本文來自博客園,作者:古道輕風,轉載請注明原文鏈接:https://www.cnblogs.com/88223100/p/Design-and-Implementation-of-Join-Query-in-Distributed-database.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/556653.html
標籤:MySQL
上一篇:Spark的一些重要概念
下一篇:返回列表
