Sleep function
This function can be used to introduce a delay or pause in the execution of a query or a batch of queries for a specific amount of time.
| Function | Description |
|---|---|
sleep() | Delays or pauses in the execution of a query or a batch of queries. |
sleep
The sleep function delays or pauses the execution of a query or a set of statements.
The following example shows this function, and its output, when used in a RETURN statement:
SurrealDB also has a SLEEP statement statement that accepts a datetime; however, the sleep function can be used in more dynamic ways such as the following example that simulates a 100ms delay between each record in a query.
SLEEP during parallel operations
The sleep() function does not interfere with operations that are underway in the background, such as a DEFINE INDEX statement using the CONCURRENTLY clause.
Use cases
Putting a database to sleep can be useful in a small number of situations, such as:
Testing and debugging: can be used to understand how concurrent transactions interact, test how systems handle timeouts and delays, simulate behaviour in more distant regions with longer latency
Throttling: can be used to throttle the execution of operations to prevent the database from being overwhelmed by too many requests at once
Security measures: can be used to slow down the response rate of login attempts to mitigate the risk of brute force attacks