ESTIMATED_QUERY_RUNTIME

Returns an estimate of the elapsed time, in seconds, required to run a query.

The elapsed time includes the execution time for the query as well as the time required by any blocking operations that delay the query from executing.

This function is intended to be used inside a user-defined scalar function (UDF) that selects a resource pool.

Note

At this time, ESTIMATED_QUERY_RUNTIME is an experimental function. It is not formally supported. However, feedback and suggestions on this function are welcome.

Syntax

ESTIMATED_QUERY_RUNTIME()

Return Type

Double

Remarks

  • For ESTIMATED_QUERY_RUNTIME to provide an accurate estimate, statistics must first be collected on the query that this function operates on. After an initial run of the query, statistics are automatically collected at ten minute intervals. You can collect statistics immediately by running ANALYZE MEMORY.

  • Returns -1 if SingleStore Helios cannot estimate the time required to run the query, because statistics on the query have not been collected.

Examples

Example 2: Using ESTIMATED_QUERY_RUNTIME() Outside a Resource Pool Selector Function

While ESTIMATED_QUERY_RUNTIME() is intended to be used inside a resource pool selector function, you can use it outside the function for testing. The following example estimates the elapsed time required to run three simple queries.

First, do an initial run of the three queries, followed by ANALYZE MEMORY to collect statistics on the queries. The initial run returns -1 because statistics have not yet been collected.

SELECT SLEEP(0.5), ESTIMATED_QUERY_RUNTIME();
+------------+---------------------------+
| SLEEP(0.5) | ESTIMATED_QUERY_RUNTIME() |
+------------+---------------------------+
|          0 |                        -1 |
+------------+---------------------------+
SELECT SLEEP(1.5), ESTIMATED_QUERY_RUNTIME();
+------------+---------------------------+
| SLEEP(1.5) | ESTIMATED_QUERY_RUNTIME() |
+------------+---------------------------+
|          0 |                        -1 |
+------------+---------------------------+
SELECT SLEEP(3.0), ESTIMATED_QUERY_RUNTIME();
+------------+---------------------------+
| SLEEP(3.0) | ESTIMATED_QUERY_RUNTIME() |
+------------+---------------------------+
|          0 |                        -1 |
+------------+---------------------------+

Next, collect statistics on the query. You must have SUPER privileges to run the ANALYZE MEMORY command or an error will be generated. This command also runs in the background every 10 minutes, so you can wait 10 minutes and then proceed to the next step:

ANALYZE MEMORY;

Re-run the previous query that sleeps for 0.5 seconds.

SELECT SLEEP(0.5), ESTIMATED_QUERY_RUNTIME();
+------------+---------------------------+
| SLEEP(0.5) | ESTIMATED_QUERY_RUNTIME() |
+------------+---------------------------+
|          0 |                     0.507 |
+------------+---------------------------+

Re-run the previous query that takes 1.5 seconds.

SELECT SLEEP(1.5), ESTIMATED_QUERY_RUNTIME();
+------------+---------------------------+
| SLEEP(1.5) | ESTIMATED_QUERY_RUNTIME() |
+------------+---------------------------+
|          0 |                     1.508 |
+------------+---------------------------+

Re-run the previous query that takes 3.0 seconds.

SELECT SLEEP(3.0), ESTIMATED_QUERY_RUNTIME();
+------------+---------------------------+
| SLEEP(3.0) | ESTIMATED_QUERY_RUNTIME() |
+------------+---------------------------+
|          0 |                     3.009 |
+------------+---------------------------+

Last modified: August 21, 2024

Was this article helpful?