apache / shardingsphere

Distributed SQL transaction & query engine for data sharding, scaling, encryption, and more - on any database.
Apache License 2.0
19.89k stars 6.73k forks source link

LAST_INSERT_ID() is wrong #18837

Closed yunmengmeng closed 2 months ago

yunmengmeng commented 2 years ago

I set two databases.The second is sharding database. When i use INSERT INTO .... SELECT LAST_INSERT_ID() AS ... to the first dabase,the tables's primary key type is auto-incrment,it saves in the first database,but when querying insert id,it's return value is wrong.Just retuns 0.

Which version of ShardingSphere did you use?

5.1.2

Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?

ShardingSphere-JDBC

Expected behavior

LAST_INSERT_ID()

Actual behavior

0

Reason analyze (If you can)

It chooses the second database.

image

PartialSQLRouteExecutor

image

Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

INSERT INTO tree1 (pid) VALUES (1); SELECT LAST_INSERT_ID() AS id;

yunmengmeng commented 2 years ago

now i use useGeneratedKeys="true" keyProperty="xxx" to replace SELECT LAST_INSERT_ID()

strongduanmu commented 2 years ago

Hi @yunmengmeng, thank you for your feedback. If you are using sharding feature, then I recommend using distributed id instead of mysql incrementing id.

chenzhikun12 commented 2 years ago

你好@yunmengmeng, 感谢您的反馈意见。如果您使用分片功能,那么我建议使用分布式 id 而不是 mysql 递增 id。

我配置了分布式id,数据能够正常insert进数据库,并且数据库中有雪花算法生成的id,但当我想从内存中获取id时很像发现没有回写id,我想知道支持主键回写吗?

terrymanu commented 3 months ago

The sharding is improved in each version, can you test the latest version?

github-actions[bot] commented 2 months ago

There hasn't been any activity on this issue recently, and in order to prioritize active issues, it will be marked as stale.