UNIX_TIMESTAMP() 是否为大型插入查询计算一次?
Is UNIX_TIMESTAMP() calculated once for large insert queries?
我做了一个测试。这是一个 innodb table:
CREATE TABLE `test_UNIX_TIMESTAMP` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`datefrom` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
这是一个插页:
insert into test_UNIX_TIMESTAMP (datefrom) values
(UNIX_TIMESTAMP())
,(UNIX_TIMESTAMP())
,(UNIX_TIMESTAMP())
....many many
,(UNIX_TIMESTAMP());
这是一个查询,用于检查时间戳是否唯一:
select distinct(datefrom) from test_UNIX_TIMESTAMP;
结果是:
After Affected rows: 106 400, we have 1 unique timestamp value in the table.
After Affected rows: 1 170 400 , we still have 1 unique timestamp value in the table.
MySQL 服务器 - 它是远程 SQL 服务器(至强 1270v3,32 内存,mysql 有 4Gb 缓存)。
大型插入查询的 "UNIX_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 for SYSDATE(), which returns the exact time at which it executes.
遗憾的是,没有关于此案例的其他功能的具体文档。如果你想确保始终具有相同的值,只需 NOW()
和 UNIX_TIMESTAMP()
,如
SELECT UNIX_TIMESTAMP(NOW());
MySQL 文档对此不明确。但是,它确实区分了 CURRENT_TIMESTAMP()
(或 NOW()
)和 SYSDATE()
——前者在每次查询时计算一次,而后者在每次调用时计算(大概有点慢)。
您可以通过调用 SLEEP
指令的两边来为自己演示:
mysql> select NOW() "a", SYSDATE() "b", SLEEP(2) "_", NOW() "c", SYSDATE() "d";
+---------------------+---------------------+---+---------------------+---------------------+
| a | b | _ | c | d |
+---------------------+---------------------+---+---------------------+---------------------+
| 2015-11-04 11:18:55 | 2015-11-04 11:18:55 | 0 | 2015-11-04 11:18:55 | 2015-11-04 11:18:57 |
+---------------------+---------------------+---+---------------------+---------------------+
1 row in set (2.01 sec)
如您所见,SYSDATE()
在查询期间增加,而 NOW()
保持不变。事实证明,UNIX_TIMESTAMP()
也会在每个查询中计算一次:
mysql> select UNIX_TIMESTAMP(), SLEEP(2), UNIX_TIMESTAMP();
+------------------+----------+------------------+
| UNIX_TIMESTAMP() | SLEEP(2) | UNIX_TIMESTAMP() |
+------------------+----------+------------------+
| 1446635945 | 0 | 1446635945 |
+------------------+----------+------------------+
1 row in set (2.00 sec)
我做了一个测试。这是一个 innodb table:
CREATE TABLE `test_UNIX_TIMESTAMP` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`datefrom` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
这是一个插页:
insert into test_UNIX_TIMESTAMP (datefrom) values
(UNIX_TIMESTAMP())
,(UNIX_TIMESTAMP())
,(UNIX_TIMESTAMP())
....many many
,(UNIX_TIMESTAMP());
这是一个查询,用于检查时间戳是否唯一:
select distinct(datefrom) from test_UNIX_TIMESTAMP;
结果是:
After Affected rows: 106 400, we have 1 unique timestamp value in the table.
After Affected rows: 1 170 400 , we still have 1 unique timestamp value in the table.
MySQL 服务器 - 它是远程 SQL 服务器(至强 1270v3,32 内存,mysql 有 4Gb 缓存)。
大型插入查询的 "UNIX_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 for SYSDATE(), which returns the exact time at which it executes.
遗憾的是,没有关于此案例的其他功能的具体文档。如果你想确保始终具有相同的值,只需 NOW()
和 UNIX_TIMESTAMP()
,如
SELECT UNIX_TIMESTAMP(NOW());
MySQL 文档对此不明确。但是,它确实区分了 CURRENT_TIMESTAMP()
(或 NOW()
)和 SYSDATE()
——前者在每次查询时计算一次,而后者在每次调用时计算(大概有点慢)。
您可以通过调用 SLEEP
指令的两边来为自己演示:
mysql> select NOW() "a", SYSDATE() "b", SLEEP(2) "_", NOW() "c", SYSDATE() "d";
+---------------------+---------------------+---+---------------------+---------------------+
| a | b | _ | c | d |
+---------------------+---------------------+---+---------------------+---------------------+
| 2015-11-04 11:18:55 | 2015-11-04 11:18:55 | 0 | 2015-11-04 11:18:55 | 2015-11-04 11:18:57 |
+---------------------+---------------------+---+---------------------+---------------------+
1 row in set (2.01 sec)
如您所见,SYSDATE()
在查询期间增加,而 NOW()
保持不变。事实证明,UNIX_TIMESTAMP()
也会在每个查询中计算一次:
mysql> select UNIX_TIMESTAMP(), SLEEP(2), UNIX_TIMESTAMP();
+------------------+----------+------------------+
| UNIX_TIMESTAMP() | SLEEP(2) | UNIX_TIMESTAMP() |
+------------------+----------+------------------+
| 1446635945 | 0 | 1446635945 |
+------------------+----------+------------------+
1 row in set (2.00 sec)