About MySQL's SLEEP (N) function, mysqlsleep Function

Source: Internet
Author: User

About MySQL's SLEEP (N) function, mysqlsleep Function
We all know that you can run this statement for N seconds by executing select sleep (N) in MySQL:

mysql> select sleep(1);+----------+| sleep(1) |+----------+|        0 |+----------+1 row in set (1.00 sec)
The execution time returned to the client shows that the wait time is 1 second.

With the sleep (N) function, we can capture statements that are not easy to be viewed quickly in MySQL Server PROCESSLIST to determine whether our program has initiated the Statement on the Server. For example, if we want to determine whether the program actually initiates a request to execute SQL statements to the Server during debugging, we can check whether the statements appear by executing the show processlist or the information_schema.processlist table. However, the statement execution speed may be very fast, so it is difficult to determine whether the statement is actually executed through the above method. For example, if the execution time of the following statement is 0.00 seconds, the thread information will flash and be invisible.
mysql> select name from animals where name='tiger';+-------+| name  |+-------+| tiger |+-------+1 row in set (0.00 sec)
In this case, you can add a sleep (N) function in the statement to force the statement to stay for N seconds to view the background thread. For example:
mysql> select sleep(1),name from animals where name='tiger';+----------+-------+| sleep(1) | name  |+----------+-------+|        0 | tiger |+----------+-------+1 row in set (1.00 sec)
In the same condition, the execution time returned by this statement is 1.0 seconds.

However, this method has the precondition that the specified number of seconds will be stopped only when the specified condition record exists. For example, if the query condition is name = 'pig', the result indicates that the record does not exist, the execution time is 0.
mysql> select name from animals where name='pig';Empty set (0.00 sec)
Under such a condition, even if the sleep (N) function is added, the execution of the statement will flash, for example:
mysql> select sleep(1),name from animals where name='pig';Empty set (0.00 sec)
Note that after the sleep (N) function is added, the specific duration of statement execution depends on the number of records meeting the conditions, mySQL will stay for N seconds for each matching record.
For example, there are three records for name like '% ger '.
mysql> select name from animals where name like '%ger';+-------+| name  |+-------+| ger   || iger  || tiger |+-------+3 rows in set (0.00 sec)
Then, after the sleep (1) function is added to the statement, the total execution time of the statement is 3.01 seconds. It can be concluded that MySQL has stayed for 1 second for each record meeting the condition.
mysql> select sleep(1),name from animals where name like '%ger';+----------+-------+| sleep(1) | name  |+----------+-------+|        0 | ger   ||        0 | iger  ||        0 | tiger |+----------+-------+3 rows in set (3.01 sec)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.