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.
API DEFINITIONsleep(duration) -> none
The following example shows this function, and its output, when used in a RETURN
statement:
RETURN sleep(1s); RETURN sleep(500ms);
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.
-- Create 3 `person` records CREATE |person:3|; LET $now = time::now(); SELECT *, sleep(100ms) AS _, time::now() - $now AS elapsed FROM person;
Response[ { _: NONE, elapsed: 101ms457µs, id: person:fkgvriz1kl2tcgv6yqfq }, { _: NONE, elapsed: 203ms599µs, id: person:lgibwdgtvx4v8ck60guk }, { _: NONE, elapsed: 305ms728µs, id: person:pr0uby896y1az2p44wtw } ]
Putting a database to sleep can be useful in a small number of situations, such as: