长时间运行的查询中的 SQL NOW()

发布于 2024-10-29 13:28:03 字数 240 浏览 1 评论 0原文

假设我有长时间运行的更新查询

update some_table 
set modification_time = now() 
where (something incredibly complex);

some_table 中的modification_time 的值是多少?它们是相同还是不同(例如,查询执行需要 2 天)。

如果它们不同,我该如何编写这个查询以使它们都相同?

Say I have long running update query

update some_table 
set modification_time = now() 
where (something incredibly complex);

What will be values of modification_time in some_table? Will they be same or different (say, it took 2 days for query to execute).

And if they will be different, how do I write this query so that they all are same?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞慾 2024-11-05 13:28:03

它们都是相同的,因为 NOW() 在查询开始时被锁定。

这个答案是否太短了?

好的,更多信息 NOW 的 MySQL 参考( )

NOW() 返回一个常数时间,指示语句开始执行的时间。 (在存储函数或触发器中,NOW() 返回函数或触发语句开始执行的时间。)这与 SYSDATE() 的行为不同,后者返回其执行的确切时间。

阅读 SYSDATE() 但是,其中包含此代码段

mysql> SELECT NOW(), SLEEP(2), NOW();
+---------------------+----------+---------------------+
| NOW()               | SLEEP(2) | NOW()               |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:36 |        0 | 2006-04-12 13:47:36 |
+---------------------+----------+---------------------+

mysql> SELECT SYSDATE(), SLEEP(2), SYSDATE();
+---------------------+----------+---------------------+
| SYSDATE()           | SLEEP(2) | SYSDATE()           |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:44 |        0 | 2006-04-12 13:47:46 |
+---------------------+----------+---------------------+

您问的有什么有趣的。注意到您可以在查询中SLEEP考虑此查询(子查询仅模拟 3 条记录的表)

select *, now(), sleep(2), sysdate()
from (select 1 N union all select 2 union all select 3) M

您将得到:

N   now()           sleep(2)  sysdate()
1   2011-04-02 23:55:27   0   2011-04-02 23:55:29
2   2011-04-02 23:55:27   0   2011-04-02 23:55:31
3   2011-04-02 23:55:27   0   2011-04-02 23:55:33

They will all be the same, since NOW() is locked in at the time of query start.

Is this too short as an answer?

Okay, more info MySQL reference for 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.

It is actually more interesting to read the manual entry for SYSDATE() however, which contains this snippet

mysql> SELECT NOW(), SLEEP(2), NOW();
+---------------------+----------+---------------------+
| NOW()               | SLEEP(2) | NOW()               |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:36 |        0 | 2006-04-12 13:47:36 |
+---------------------+----------+---------------------+

mysql> SELECT SYSDATE(), SLEEP(2), SYSDATE();
+---------------------+----------+---------------------+
| SYSDATE()           | SLEEP(2) | SYSDATE()           |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:44 |        0 | 2006-04-12 13:47:46 |
+---------------------+----------+---------------------+

What's so interesting you ask.. notice that you can SLEEP in a query?? Consider this query (the sub-query just emulates a 3-record table)

select *, now(), sleep(2), sysdate()
from (select 1 N union all select 2 union all select 3) M

You get:

N   now()           sleep(2)  sysdate()
1   2011-04-02 23:55:27   0   2011-04-02 23:55:29
2   2011-04-02 23:55:27   0   2011-04-02 23:55:31
3   2011-04-02 23:55:27   0   2011-04-02 23:55:33
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文