shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

分片列 必须写在左侧的 和在多表的时候必须指定分片列属于哪一个表 问题

Open cwpgh opened this issue 8 years ago • 4 comments

//1.如果分片字段 存在于多个表规则中必须指定,存在于多个无法区分属于哪一个表,sql语句也会报错 //2.如果只存在一个表规则中,可以不指定,根据分片列名能查找出来属于哪一个分片表,sql语句是可以执行的

private void parseEqualCondition(final ShardingRule shardingRule, final SQLStatement sqlStatement, final SQLExpression left) { SQLExpression right = expressionClauseParser.parse(sqlStatement); // TODO if have more tables, and cannot find column belong to, should not add to condition, should parse binding table rule. if ((sqlStatement.getTables().isSingleTable() || left instanceof SQLPropertyExpression) && (right instanceof SQLNumberExpression || right instanceof SQLTextExpression || right instanceof SQLPlaceholderExpression)) { Optional column = find(sqlStatement.getTables(), left); if (column.isPresent()) { sqlStatement.getConditions().add(new Condition(column.get(), right), shardingRule); } } }

cwpgh avatar Jan 22 '18 07:01 cwpgh

我没有理解你的意思,能否提供版本号,报错信息,sql,表结构,表数据,配置信息以及执行代码等详细信息。

terrymanu avatar Jan 22 '18 13:01 terrymanu

一。分片列写在左侧问题 1。分片列在左侧 查询语句 SELECT * FROM t_order o where o.order_id=12 limit 10 分片结果 SELECT * FROM t_order_0 o where o.order_id=12 limit 10
2.分片列写在右侧 查询语句 SELECT * FROM t_order o where 12=o.order_id limit 10 分片结果 SELECT * FROM t_order_0 o where 12=o.order_id limit 10
SELECT * FROM t_order_1 o where 12=o.order_id limit 10

第一个是分片列写在左侧,能正确识别 第二个分片列写在右侧,识别不了

================================================================ 二。多表指定列名问题 1.指定名称 查询语句:SELECT * FROM t_order o,t_order_item t where o.order_id=1 and t.item_id=1 limit 10 分片结果:SELECT * FROM t_order_1 o,t_order_item_1 t where o.order_id=1 and t.item_id=1 limit 10
2.未指定名称 查询语句:SELECT * FROM t_order o,t_order_item t where order_id=1 and item_id=1 limit 10 分片结果: SELECT * FROM t_order_0 o,t_order_item_0 t where order_id=1 and item_id=1 limit 10 SELECT * FROM t_order_0 o,t_order_item_1 t where order_id=1 and item_id=1 limit 10 SELECT * FROM t_order_1 o,t_order_item_0 t where order_id=1 and item_id=1 limit 10 SELECT * FROM t_order_1 o,t_order_item_1 t where order_id=1 and item_id=1 limit 10 分片配置: t_order: actualDataNodes: ds.t_order_${0..1} tableStrategy: inline: shardingColumn: order_id algorithmExpression: t_order_${order_id % 2} keyGeneratorColumnName: order_id t_order_item: actualDataNodes: ds.t_order_item_${0..1} #绑定表中其余的表的策略与第一张表的策略相同 tableStrategy: inline: shardingColumn: item_id algorithmExpression: t_order_item_${item_id % 2}

问题2,根据分片order_id能唯一确定到表t_order的,item_id也可以确定到表t_order_item

cwpgh avatar Jan 23 '18 02:01 cwpgh

这确实是问题,但是由于精力有限,我们无法将这样的SQL解析全部实现。 请标准化自己的SQL,我们目前暂时无计划修改。

terrymanu avatar Jan 24 '18 05:01 terrymanu

如果有兴趣,也欢迎提交pr

terrymanu avatar Jan 24 '18 05:01 terrymanu