当在where子句中使用sleep()时,MySql查询需要太长时间

时间:2022-11-12 03:59:07

I have read about sleep(n) function in MySql, which is supposed to sleep for n seconds and return 0 if uninterrupted or 1 if interrupted.

我已经阅读了MySql中的sleep(n)函数,该函数应该睡眠n秒,如果不中断则返回0,如果中断则返回1。

This works well if I use sleep() in select clause. For example, following query returns result after 10 seconds.

如果我在select子句中使用sleep(),这很有效。例如,以下查询在10秒后返回结果。

SELECT id, sleep(10) FROM versions WHERE id = 123

SELECT id,sleep(10)FROM WHERE id = 123

However, the query takes too long if I use sleep(10) in the where clause.

但是,如果我在where子句中使用sleep(10),查询会花费太长时间。

SELECT id FROM versions WHERE id = 123 OR sleep(10)=1

SELECT id FROM versions WHERE id = 123 OR sleep(10)= 1

Any idea about why is it behaving like this?

任何关于它为什么会这样的想法?

2 个解决方案

#1


2  

In the first query it takes just 10 second sleep time while in the second one every id is being checked if it is 123 or not and if not it sleeps for 10 seconds.

在第一个查询中,它只需要10秒的休眠时间,而在第二个查询中,每个id都被检查是否为123,如果不是,则它会休眠10秒。

In where clause using sleep is like checking it against each row in the database except the one where it matches the value 123 in your case.

在where子句中,使用sleep就像检查数据库中的每一行,除了它匹配你的情况下的值123的那一行。

#2


2  

It will sleep at every row for 10 seconds when your first condition is false.

当您的第一个条件为假时,它将在每一行睡10秒钟。

#1


2  

In the first query it takes just 10 second sleep time while in the second one every id is being checked if it is 123 or not and if not it sleeps for 10 seconds.

在第一个查询中,它只需要10秒的休眠时间,而在第二个查询中,每个id都被检查是否为123,如果不是,则它会休眠10秒。

In where clause using sleep is like checking it against each row in the database except the one where it matches the value 123 in your case.

在where子句中,使用sleep就像检查数据库中的每一行,除了它匹配你的情况下的值123的那一行。

#2


2  

It will sleep at every row for 10 seconds when your first condition is false.

当您的第一个条件为假时,它将在每一行睡10秒钟。