简体   繁体   English

MySQL 5.6在运行查询或提交事务时,是否会设置插入/更新上的时间戳记值?

[英]MySQL 5.6 Does value of timestamp on insert/update get set when query was ran or when transaction was committed?

I was just curious and I checked the documentation: https://dev.mysql.com/doc/refman/5.6/en/datetime.html 我只是好奇而已,我查看了文档: https : //dev.mysql.com/doc/refman/5.6/en/datetime.html

But it doesn't say specifically if the value of a timestamp is set in terms of when a query was ran or when the transaction it was a part of was committed: 但是并没有具体说明是否根据查询的运行时间或提交事务的时间设置了时间戳的值:

CREATE TABLE some_schema.some_table (
    id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    some_field VARCHAR(50) DEFAULT NULL,
    last_changed_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
);

START TRANSACTION;
INSERT INTO some_schema.some_table (some_field) VALUES ("Foo");
INSERT INTO some_schema.some_table (some_field) VALUES ("Bar");
COMMIT;

And while obviously this would be the same time because it would run so fast (and microseconds are stripped off from what i understand), imagine running 1 trillion or something inserts, would their last_changed_ts value be the same since they are all committed in one transaction, or would there be differences between them based on when the INSERT statement was ran? 而且虽然显然这是同一时间,因为它运行得如此之快(我理解的时间last_changed_ts为微秒),想象一下运行1万亿或插入某些内容,它们的last_changed_ts值是否相同,因为它们都在一个事务中提交,或者基于INSERT语句的运行时间,它们之间会有区别吗?

-- actually, after writing this all, I realize it's trivial to just set up a simple python script that starts a transaction, inserts, sleeps, and inserts again and check myself. -实际上,在写完所有内容之后,我意识到仅设置一个简单的python脚本来启动事务,插入,休眠,再次插入并再次检查自己是微不足道的。 I already wrote this all out and couldn't find a quick answer when googling so i'm leaving it and maybe someone else wants to throw their interesting two-cents in but if not i'll post my findings of the simple test in the coming day. 我已经把所有内容写完了,在谷歌搜索时找不到快速答案,所以我离开了,也许其他人想把他们有趣的2美分投入其中,如果没有,我会将我的简单测试结果发布到即将到来的一天。

The timestamp is the timestamp of when query execution began on each insert query. 时间戳是每个插入查询开始执行查询的时间戳。 If you insert a large number of rows in a single insert statement, the timestamp will be the same for all rows, and will be the timestamp of when the query began executing. 如果在单个插入语句中插入大量行,则所有行的时间戳都将相同,并将成为查询开始执行的时间戳。

Committing a transaction doesn't change the values, as should be obvious from the fact that the inserted rows can be read back before commit, and it would not make sense if their values subsequently changed because you committed the insert. 提交事务不会更改值,这很明显,因为可以在提交之前读回插入的行,并且由于提交了插入而随后更改了它们的值也没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM