简体   繁体   English

当在where子句中使用sleep()时,MySql查询花费的时间太长

[英]MySql query takes too long when sleep() is used in where clause

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. 如果我在select子句中使用sleep(),这很有效。 For example, following query returns result after 10 seconds. 例如,以下查询在10秒后返回结果。

SELECT id, sleep(10) FROM versions 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

Any idea about why is it behaving like this? 任何关于它为什么表现如此的想法?

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匹配的行。

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

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

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