我想让我的 MySQL 数据库表与相应的 elasticsearch 索引保持同步。我正在尝试编写自己的 cron 作业。 MySQL表有InnoDB引擎。我添加了 updated_at
列,即 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
。我使用此查询来执行同步:
SELECT * FROM table WHERE updated_at>=bookmark;
我测试了一下,发现有些插入已经丢失了。
CURRENT_TMESTAMP 是否在 COMMIT 之前计算?
最佳答案
CURRENT_TIMESTAMP
——NOW()
的同义词——在调用它的查询执行开始时捕获它的值。无论查询运行多长时间,这些函数都会保持一个恒定值 - 因此在长时间运行更新等情况下,所有受影响的行都将具有相同的值,并且该值应该是每个单独的查询开始执行时的值 - 而不是提交时间。
NOW()
returns a constant time that indicates the time at which the statement began to execute. (Within a stored function or trigger,NOW()
returns the time at which the function or triggering statement began to execute.) This differs from the behavior forSYSDATE()
, which returns the exact time at which it executes.https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_now
关于Mysql InnoDB表到elasticsearch : ON UPDATE CURRENT TIMESTAMP inside transaction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40252077/