two tables left join with different routing rules are not supported
two tables left join, with different routing rules are not config binding tables, Will lead to full routing。 Table a and table b are divided into tables according to col field. Table b is divided into tables according to col field on each sharding database. Table a is not divided into tables. The sharding database route can be found, but sharding table rule of table b will not be found,Cause table b full routing like this
select a.*,b.* from a left join b on a.col = b.col where a.col = '123'
sharding rule:
<sharding:table-rule logic-table="a" actual-data-nodes="ds_ms$->{0..1}.a" database-strategy-ref="databaseStrategy" />
<sharding:table-rule logic-table="b" actual-data-nodes="ds_ms0.b_000,ds_ms$->{0..1}.b_00$->{1..3}" database-strategy-ref="databaseStrategy" table-strategy-ref="tableStrategy" />
<sharding:standard-strategy id="databaseStrategy" sharding-column="col" precise-algorithm-ref="commonDBShardingAlgorithm"/>
<sharding:standard-strategy id="tableStrategy" sharding-column="col" precise-algorithm-ref="tableShardingAlgorithm"/>
actual sql:
select a.*,b.* from db0.a left join db0.b_001 on a.col = b.col where a.col = '123';
select a.*,b.* from db0.a left join db0.b_002 on a.col = b.col where a.col = '123';
select a.*,b.* from db0.a left join db0.b_003 on a.col = b.col where a.col = '123';
How to deal with this situation?
version: 4.0.0 release
b.col is sharding key:
b.col = '123'
b.col is sharding key:
b.col = '123'
However, by convention, two tables join, only the main table write value(a.col='123') in the where condition, The sub table obtains the value of the where condition through the ’on’ condition in the join. if both main table and sub table write value in the where condition, feel very uncomfortable
What is your sharding rule configuration?
What is your sharding rule configuration?
sharding rule:
<sharding:table-rule logic-table="a" actual-data-nodes="ds_ms$->{0..1}.a" database-strategy-ref="databaseStrategy" />
<sharding:table-rule logic-table="b" actual-data-nodes="ds_ms0.b_000,ds_ms$->{0..1}.b_00$->{1..3}" database-strategy-ref="databaseStrategy" table-strategy-ref="tableStrategy" />
<sharding:standard-strategy id="databaseStrategy" sharding-column="col" precise-algorithm-ref="commonDBShardingAlgorithm"/>
<sharding:standard-strategy id="tableStrategy" sharding-column="col" precise-algorithm-ref="tableShardingAlgorithm"/>
in the db and table ShardingAlgorithm, i mapping the dbs and tables by columns 'col', like:
Map dbMap = new HashMap();
static{
dbMap.put("123","db_01");
dbMap.put("125","db_02")
}
Map tableMap = new HashMap();
static{
tableMap.put("123","table_001");
tableMap.put("125","table_002")
}
i mean on the join condition, Whether the sharding key after "on" should be transitive? I didn't find it in the source code.
When the shardingsphere route the sql, it does not find the table b's sharding key, so it will cause a full route. To avoid it, you may add the table b's sharding key after where condition.And now shardingsphere may not use the condition after 'on' clause.
When the shardingsphere route the sql, it does not find the table b's sharding key, so it will cause a full route. To avoid it, you may add the table b's sharding key after where condition.And now shardingsphere may not use the condition after 'on' clause.
In the future,Whether shardingsphere can be considered ‘on’ after join? because by convention, For old system upgrades, only write main table's sharding value after 'where' 。 or add config like bindtables ,to achieve this function
When the shardingsphere route the sql, it does not find the table b's sharding key, so it will cause a full route. To avoid it, you may add the table b's sharding key after where condition.And now shardingsphere may not use the condition after 'on' clause.
In the future,Whether shardingsphere can be considered ‘on’ after join? because by convention, For old system upgrades, only write main table's sharding value after 'where' 。 or add config like bindtables ,to achieve this function
It depends on requirement and the implementation of the route algorithm. In my opinion, shardingsphere may not considered the 'on' clause for routing.
@fantacy2001 The current route result is correct. What is your expected?
@fantacy2001 The current route result is correct. What is your expected?
i expected the sharding key in the condition 'on' after 'join', able to transmit sharding value in where condition ,Instead of writing it again in where condition
Can you use SQL to explain your expected behaviour?
Can you use SQL to explain your expected behaviour?
shardingsphere's route is no problem. i mean a convention is :
select a.*,b.* from a left join b on a.col = b.col where a.col = '123'
but not :
select a.*,b.* from a left join b on a.col = b.col where a.col = '123' and b.col = '123'
t does not affect the function, but talk about whether it is possible to optimize it
ShardingSphere can support literal sharding-key conditions explicitly. Implicit semantic conditions are not in our current scope (other query optimizations too). So we can discuss about how to go further for it. And if you are insterested in this topic, making an investigation and some proposals here is welcome.
In your case above, my suggestion is to modify your sql:
- Use sharding-key literally with
... b.col = 123 - Or split two sql, query from a with 123, and then query from b with sharding-key
Can you use SQL to explain your expected behaviour?
shardingsphere's route is no problem. i mean a convention is :
select a.*,b.* from a left join b on a.col = b.col where a.col = '123'but not :
select a.*,b.* from a left join b on a.col = b.col where a.col = '123' and b.col = '123'
What is your original SQL, does the original SQL include and b.col = '123'?
t does not affect the function, but talk about whether it is possible to optimize it
What is t?
@fantacy2001 any more questions?
Can you use SQL to explain your expected behaviour?
shardingsphere's route is no problem. i mean a convention is :
select a.*,b.* from a left join b on a.col = b.col where a.col = '123'but not :
select a.*,b.* from a left join b on a.col = b.col where a.col = '123' and b.col = '123'What is your original SQL, does the original SQL include
and b.col = '123'?t does not affect the function, but talk about whether it is possible to optimize it
What is t?
sorry ,Should be ‘it’ ,‘t’ is my fault
@fantacy2001 any more questions?
no more ,thanks !