shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

The use of the 'Or'

Open weiduan opened this issue 8 years ago • 8 comments

Which version of Sharding-Sphere do you using?

sharding-jdbc 3.0.0.M1

Expected behavior

shardingConditions.getShardingConditions() should only one

Actual behavior

I get multiple of the same conditions

Steps to reproduce the behavior

sql:select * from table1 where shardingcolumn = *** and ( field1='test' or field2 ='test' )

Only one shardingCondition is met, but shardingConditions.getShardingConditions() contains multiple identical conditions The final routing result is correct, however ComplexKeysShardingAlgorithm.doSharding() is executed multiple times

weiduan avatar Jul 03 '18 05:07 weiduan

Sharding-sphere don't parse column if they are not sharding column

terrymanu avatar Jul 03 '18 05:07 terrymanu

yeah,But what seems to make this sharding column repeat,I can see the shardingConditions.getShardingConditions() really has duplicate ShardingCondition on 79 line of StandardRoutingEngine.java: qq20180703-145243 2x a517dcd5-c89b-404f-9dd4-37df7b02fa33

weiduan avatar Jul 03 '18 06:07 weiduan

@weiduan please give me a full sql, i will help you

maxiaoguang64 avatar Jul 03 '18 07:07 maxiaoguang64

@ma-xiao-guang-64 SELECT * FROM order WHERE business_id = ? AND ( member_discount = 1 OR order_discount = 1 OR order_derate = 1 OR order_derate_zero = 1 OR item_member_discount = 1 OR order_promotion = 1 OR coupon_promotion = 1 OR order_bargain = 1 OR timed_item_discount = 1 OR x_discount = 1 ) I deleted some unrelated fields, where business_id is shardingKey, and it repeats ten times

weiduan avatar Jul 03 '18 07:07 weiduan

@weiduan ok,repeats ten times is correct. in sharding parser this sql will change to WHERE business_id = ? AND member_discount = 1 OR business_id = ? AND order_discount = 1 OR business_id = ? AND order_derate = 1 OR business_id = ? AND order_derate_zero = 1 OR business_id = ? AND item_member_discount = 1 OR business_id = ? AND order_promotion = 1 OR business_id = ? AND coupon_promotion = 1 OR business_id = ? AND order_bargain = 1 OR business_id = ? AND timed_item_discount = 1 OR business_id = ? AND x_discount = 1 ) why you look shardingConditions size is 10! in doSharding work the ten shardingConditions will to doSharding

英文捉急以下为中文 sql解析阶段 将OR用类似乘法分配律的方法解析 所以会出现10个OR条件也就是10个sharidngConditions

路由阶段 分别对10个OR条件路由然后将路由结果去并集以上看到虽然doSharding执行了10次但是最终执行sql的时候只会去business_id = ?的分片去执行

关于解析后的Conditions 采用的是List<List>的结构 List代表的是执行了类似乘法分配律的解析之后的business_id = ? AND member_discount = 1其中已经不包含了OR 多个List组成了List<List>每个List<List>之间的关系是OR

maxiaoguang64 avatar Jul 03 '18 08:07 maxiaoguang64

@ma-xiao-guang-64 Oh,i see.The routing result was indeed correct. Is it possible to optimize the repeating sharidngConditions to one in the future? After all, doSharding() will be executed too many times.Thank you for your answer!

weiduan avatar Jul 03 '18 08:07 weiduan

@weiduan it is so hard, we possible merge it in future

maxiaoguang64 avatar Jul 03 '18 09:07 maxiaoguang64

can we close this issue?

terrymanu avatar Jul 04 '18 02:07 terrymanu