In the same transaction, it is not possible to 100% obtain the data updates from the previous SQL commit

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 同一个事务中,不能百分百获得上一个sql提交的数据更新

| username: leo_zxl

[TiDB Usage Environment] Production
[TiDB Version] 5.2.1
[Encountered Problem] There is a transaction in the code that updates data in one of the tables. Later in the transaction, it references this table to perform a sum and updates the result into another table. Previously, there were no issues on MySQL. After fully migrating to TiDB, some data discrepancies were found. The transaction cannot always sum the latest data. Currently, TiDB is using pessimistic transactions with the isolation level set to repeatable read. It is unclear where the problem lies.

| username: ddhe9527 | Original post link

What isolation level are you using in MySQL?

| username: xfworld | Original post link

Try switching the isolation level to commit read and see if it works.

| username: ShawnYan | Original post link

| username: leo_zxl | Original post link

repeatable

| username: leo_zxl | Original post link

In a production environment, we don’t dare to switch easily.

| username: ddhe9527 | Original post link

There are still some differences in behavior between TiDB and MySQL under the RR isolation level. TiDB approximates SI as RR. In MySQL, a snapshot is not created immediately after START TRANSACTION; it is created when the first SQL statement is executed. In TiDB, a consistent snapshot is created immediately upon START TRANSACTION, equivalent to START TRANSACTION WITH CONSISTENT SNAPSHOT in MySQL. Therefore, in your scenario, you need to check when the sum operation starts the transaction. It might be that TiDB creates the snapshot earlier, causing the sum to not see the updated data.

| username: xfworld | Original post link

You can switch at the session level by yourself.

After switching, it will be effective for your session. Once the session is over, it will default to the global parameters and will not change the original settings. However, it can help you verify the issue~

| username: system | Original post link

This topic was automatically closed 1 minute after the last reply. No new replies are allowed.