shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

two tables left join with different routing rules are not supported

Open fantacy2001 opened this issue 6 years ago • 16 comments

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

fantacy2001 avatar Mar 20 '20 08:03 fantacy2001

b.col is sharding key:

b.col = '123'

kimmking avatar Mar 20 '20 13:03 kimmking

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

fantacy2001 avatar Mar 20 '20 15:03 fantacy2001

What is your sharding rule configuration?

terrymanu avatar Mar 25 '20 08:03 terrymanu

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.

fantacy2001 avatar Mar 26 '20 02:03 fantacy2001

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.

ssxlulu avatar Mar 26 '20 06:03 ssxlulu

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

fantacy2001 avatar Mar 26 '20 06:03 fantacy2001

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.

ssxlulu avatar Mar 26 '20 07:03 ssxlulu

@fantacy2001 The current route result is correct. What is your expected?

terrymanu avatar Mar 26 '20 09:03 terrymanu

@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

fantacy2001 avatar Mar 26 '20 10:03 fantacy2001

Can you use SQL to explain your expected behaviour?

terrymanu avatar Mar 26 '20 16:03 terrymanu

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

fantacy2001 avatar Mar 27 '20 03:03 fantacy2001

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:

  1. Use sharding-key literally with ... b.col = 123
  2. Or split two sql, query from a with 123, and then query from b with sharding-key

kimmking avatar Mar 27 '20 04:03 kimmking

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?

terrymanu avatar Mar 27 '20 04:03 terrymanu

@fantacy2001 any more questions?

kimmking avatar Mar 27 '20 11:03 kimmking

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 avatar Mar 28 '20 03:03 fantacy2001

@fantacy2001 any more questions?

no more ,thanks !

fantacy2001 avatar Mar 28 '20 03:03 fantacy2001