You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/06/11 10:11:06 UTC
[shardingsphere] branch master updated: optimize select statement
that in same datasource route logic (#10762)
This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 861d075 optimize select statement that in same datasource route logic (#10762)
861d075 is described below
commit 861d0756ed2932290814d5fa95cf21a636510e71
Author: Zhengqiang Duan <st...@gmail.com>
AuthorDate: Fri Jun 11 18:10:27 2021 +0800
optimize select statement that in same datasource route logic (#10762)
* optimize select statement that in same datasource route logic
* optimize broadcast tables route logic
---
.../route/engine/type/ShardingRouteEngineFactory.java | 3 +++
.../engine/type/single/SingleTablesRoutingEngine.java | 6 +++++-
.../shardingsphere/sharding/rule/ShardingRule.java | 18 ++++++++++++++++++
.../type/single/SingleTablesRoutingEngineTest.java | 8 ++++----
4 files changed, 30 insertions(+), 5 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
index 0c08599..6ab73d4 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
@@ -187,6 +187,9 @@ public final class ShardingRouteEngineFactory {
if (!select.isContainsJoinQuery() && !select.isContainsSubquery()) {
return false;
}
+ if (shardingRule.isAllTablesInSameDataSource(tableNames)) {
+ return false;
+ }
return shardingRule.isAllShardingTables(tableNames) || (shardingRule.tableRuleExists(tableNames) && shardingRule.singleTableRuleExists(tableNames));
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTablesRoutingEngine.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTablesRoutingEngine.java
index c8a79c9..4ce1b63 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTablesRoutingEngine.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTablesRoutingEngine.java
@@ -48,7 +48,7 @@ public final class SingleTablesRoutingEngine implements ShardingRouteEngine {
@Override
public void route(final RouteContext routeContext, final ShardingRule shardingRule) {
- if (sqlStatement instanceof CreateTableStatement || sqlStatement instanceof AlterTableStatement || sqlStatement instanceof DropTableStatement) {
+ if (isDDLTableStatement() || shardingRule.isAllTablesInSameDataSource(logicTables)) {
Set<String> existSingleTables = Sets.intersection(shardingRule.getSingleTableRules().keySet(), Sets.newHashSet(logicTables));
if (!existSingleTables.isEmpty()) {
fillRouteContext(shardingRule, routeContext, existSingleTables);
@@ -63,6 +63,10 @@ public final class SingleTablesRoutingEngine implements ShardingRouteEngine {
}
}
+ private boolean isDDLTableStatement() {
+ return sqlStatement instanceof CreateTableStatement || sqlStatement instanceof AlterTableStatement || sqlStatement instanceof DropTableStatement;
+ }
+
private RouteUnit getRandomRouteUnit(final ShardingRule shardingRule) {
Collection<String> dataSourceNames = shardingRule.getDataSourceNames();
String dataSource = Lists.newArrayList(dataSourceNames).get(ThreadLocalRandom.current().nextInt(dataSourceNames.size()));
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index 0c6facb..9cc01e6 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -21,6 +21,7 @@ import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
+import com.google.common.collect.Sets;
import com.google.common.eventbus.Subscribe;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
@@ -62,6 +63,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
@@ -332,6 +334,22 @@ public final class ShardingRule implements FeatureRule, SchemaRule, DataNodeCont
}
/**
+ * Judge whether all tables are in same data source or not.
+ *
+ * @param logicTableNames logic table names
+ * @return whether all tables are in same data source or not
+ */
+ public boolean isAllTablesInSameDataSource(final Collection<String> logicTableNames) {
+ Set<String> tableNames = Sets.newHashSet(logicTableNames);
+ Set<String> dataSourceNames = Sets.newHashSet();
+ dataSourceNames.addAll(tableRules.stream().filter(each -> tableNames.contains(each.getLogicTable())).flatMap(each
+ -> each.getActualDataNodes().stream()).map(DataNode::getDataSourceName).collect(Collectors.toSet()));
+ dataSourceNames.addAll(broadcastTables.stream().filter(tableNames::contains).flatMap(each -> getDataSourceNames().stream()).collect(Collectors.toSet()));
+ dataSourceNames.addAll(singleTableRules.values().stream().filter(each -> tableNames.contains(each.getTableName())).map(SingleTableRule::getDataSourceName).collect(Collectors.toSet()));
+ return 1 == dataSourceNames.size();
+ }
+
+ /**
* Judge if there is at least one table rule for logic tables.
*
* @param logicTableNames logic table names
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTablesRoutingEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTablesRoutingEngineTest.java
index 6996b28..4138c11 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTablesRoutingEngineTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/single/SingleTablesRoutingEngineTest.java
@@ -57,11 +57,11 @@ public final class SingleTablesRoutingEngineTest {
assertThat(routeUnits.get(0).getTableMappers().size(), is(2));
Iterator<RouteMapper> tableMappers = routeUnits.get(0).getTableMappers().iterator();
RouteMapper tableMapper0 = tableMappers.next();
- assertThat(tableMapper0.getActualName(), is("t_order"));
- assertThat(tableMapper0.getLogicName(), is("t_order"));
+ assertThat(tableMapper0.getActualName(), is("t_order_item"));
+ assertThat(tableMapper0.getLogicName(), is("t_order_item"));
RouteMapper tableMapper1 = tableMappers.next();
- assertThat(tableMapper1.getActualName(), is("t_order_item"));
- assertThat(tableMapper1.getLogicName(), is("t_order_item"));
+ assertThat(tableMapper1.getActualName(), is("t_order"));
+ assertThat(tableMapper1.getLogicName(), is("t_order"));
assertThat(routeContext.isFederated(), is(false));
}