The SLEEP statement is used to introduce a delay or pause in the execution of a query or a batch of queries for a specific amount of time.
Statement syntax
SLEEP @duration;Example usage
The following query shows example usage of this statement.
-- Sleep one second
SLEEP 1s;
-- Sleep 100 milliseconds
SLEEP 100ms;For more dynamic usage of sleep, see SurrealDB's sleep function.
SLEEP during parallel operations
A SLEEP statement does not interfere with operations that are underway in the background, such as a DEFINE INDEX statement using the CONCURRENTLY clause.
CREATE |user:1..=50000| RETURN NONE;
UPDATE user SET name = id.id() RETURN NONE;
DEFINE INDEX unique_name ON TABLE user FIELDS name UNIQUE CONCURRENTLY;
INFO FOR INDEX unique_name ON TABLE user;
SLEEP 50ms;
INFO FOR INDEX unique_name ON TABLE user;
SLEEP 50ms;
INFO FOR INDEX unique_name ON TABLE user;
SLEEP 50ms;
INFO FOR INDEX unique_name ON TABLE user;-------- Query 1 --------
{
building: {
compacting: false,
status: 'started'
}
}
-------- Query 2 --------
{
building: {
compacting: false,
initial: 9016,
pending: 0,
status: 'indexing'
}
}
-------- Query 3 --------
{
building: {
compacting: false,
initial: 17266,
pending: 0,
status: 'indexing'
}
}
-------- Query 4 --------
{
building: {
compacting: false,
initial: 25766,
pending: 0,
status: 'indexing'
}
}Use cases
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