sleep¶
sleep(seconds)
Description¶
Sleep during seconds
seconds
Usage¶
Call or Deploy sleep
?
Call sleep
directly
The easiest way to use bigfunctions
sleep
function is deployed in 39 public datasets for all of the 39 BigQuery regions.- It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
- (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)
Public BigFunctions Datasets
Region | Dataset |
---|---|
eu |
bigfunctions.eu |
us |
bigfunctions.us |
europe-west1 |
bigfunctions.europe_west1 |
asia-east1 |
bigfunctions.asia_east1 |
... | ... |
Deploy sleep
in your project
Why deploy?
- You may prefer to deploy
sleep
in your own project to build and manage your own catalog of functions. - This is particularly useful if you want to create private functions (for example calling your internal APIs).
- Get started by reading the framework page
Deployment
sleep
function can be deployed with:
pip install bigfunctions
bigfun get sleep
bigfun deploy sleep
Examples¶
Wait for 10 seconds
select bigfunctions.eu.sleep(10)
select bigfunctions.us.sleep(10)
select bigfunctions.europe_west1.sleep(10)
+----------+
| response |
+----------+
| ok |
+----------+
Use cases¶
The sleep
function in BigQuery can be useful in a few scenarios, primarily related to testing and managing dependencies within scripts or workflows:
-
Testing BigQuery function performance: You can use
sleep
to introduce controlled delays and measure the execution time of other BigQuery functions or queries. This allows you to benchmark performance and identify bottlenecks. -
Simulating latency: In testing scenarios, you might want to simulate real-world conditions where there are delays in data processing or availability.
sleep
can help mimic these latencies. -
Managing dependencies in scripts: If you have a BigQuery script where one part needs to complete before another begins, you can use
sleep
to ensure a certain time has passed before the dependent part executes. However, this is generally not the ideal way to handle dependencies within a BigQuery script. BigQuery scripting features likeWAIT
clauses forMERGE
statements or explicitly checking for job completion status offer more robust solutions.sleep
would be a less reliable approach as execution times can vary. -
Rate limiting: If you're interacting with an external API or service via BigQuery and need to adhere to rate limits,
sleep
can be used to pause execution for a specified duration between calls. However, dedicated rate limiting libraries or built-in functionality within the API or service itself would be preferable for more precise control. -
Troubleshooting and debugging: In some cases, introducing a delay with
sleep
can be helpful for debugging timing-related issues or examining intermediate states within a complex BigQuery script.
Example (Testing performance):
-- Measure the time taken to execute a complex query
SELECT bigfunctions.eu.sleep(5); -- Introduce a delay to clear the cache (less reliable, better alternatives exist)
DECLARE start_time TIMESTAMP;
SET start_time = CURRENT_TIMESTAMP();
-- Your complex query here
SELECT * FROM large_table WHERE some_condition;
DECLARE end_time TIMESTAMP;
SET end_time = CURRENT_TIMESTAMP();
SELECT TIMESTAMP_DIFF(end_time, start_time, SECOND) AS execution_time;
Caveats: While sleep
can be useful in limited cases, relying heavily on it within production BigQuery scripts is generally discouraged. For dependency management, error handling, and performance optimization, using BigQuery's built-in features and best practices is more appropriate. Using sleep
for rate limiting is also suboptimal; dedicated rate-limiting mechanisms are more robust. It's primarily useful for simple testing and debugging scenarios.
Need help or Found a bug?
Get help using sleep
The community can help! Engage the conversation on Slack
We also provide professional suppport.
Report a bug about sleep
If the function does not work as expected, please
- report a bug so that it can be improved.
- or open the discussion with the community on Slack.
We also provide professional suppport.