MySQL - ON UPDATE CURRENT_TIMESTAMP

Problem Description

For the UPDATE_TIME field in MySQL, we sometimes set ON UPDATE CURRENT_TIMESTAMP, which means that the UPDATE_TIME time will be automatically updated when the database data is updated (if the database data value does not change, UPDATE_TIME will not be automatically updated). So suppose a scenario, we have a long transaction for 10 seconds, we enter an update operation in the second second of the transaction, and then continue to execute until the 10th second, the transaction commits. At this time, the time recorded in the database is the second second when executing update? Or is it the 10th second after the transaction is committed?

 

Validation results

After the transaction is submitted, the time saved to the database is the second point in time when the update is executed.

 

Scene limitation

If UPDATE_TIME is only used to record the update time, then this automatic update time point has no effect. But if you want to use UPDATE_TIME as the basis for data synchronization (such as synchronization to another library, or es or the like), then you can't define it this way. Because we use UPDATE_TIME as the update, generally also require quasi-real-time synchronization. If a transaction is relatively long, we have recorded the update time before the transaction has been submitted. After the transaction is submitted, we have not synchronized the UPDATE_TIME time. .

Let's take a chestnut: the start time of a long transaction is 12:00:00 and the end time is 12:00:10. The update operation was performed at 12:00:02, and the UPDATE_TIME time of the database is still 12:00 : 00, because the transaction has not been submitted yet.

At the same time, there is a scheduled task that scans the data in this period of time, such as 12:00:00 to 12:00:03, there is no data, so record 12:00:03, and continue from this time point next time After the scan, wait for the transaction to be submitted at 12:00:10. At this time, the UPDATE_TIME value of this piece of data in the database is 12:00:02, but the scheduled task scan will not scan this piece of data again, which will cause synchronous data complete. In this case, we can only set the UPDATE_TIME value in the program, and should not set ON UPDATE CURRENT_TIMESTAMP through the database itself.

Published 952 original articles · praised 1820 · 890,000 views

Guess you like

Origin blog.csdn.net/Dream_Weave/article/details/105370409