ESTIMATED_ QUERY_ LEAF_ MEMORY
On this page
Returns an estimate of the amount of memory, in megabytes, required to run a query.
The estimate is the total amount of memory across all leaf nodes.
This function is intended to be used inside a user-defined scalar function (UDF) that selects a resource pool.
Note
At this time, ESTIMATED_
is an experimental function.
Syntax
ESTIMATED_QUERY_LEAF_MEMORY()
Return Type
Integer
Remarks
-
For
ESTIMATED_
to provide an accurate estimate, statistics must first be collected on the query that this function operates on.QUERY_ LEAF_ MEMORY() 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 cannot estimate the amount of memory required to run the query because statistics on the query have not been collected.
Examples
Using ESTIMATED_ QUERY_ LEAF_ MEMORY()
Inside a Resource Pool Selector Function
The topic Setting Resource Limits provides an example of using ESTIMATED_
inside of a user-defined scalar function (UDF).
Using ESTIMATED_ QUERY_ LEAF_ MEMORY()
Outside a Resource Pool Selector Function
While ESTIMATED_
is intended to be used inside a resource pool selector function, you can use it outside the function for testing.
First define a procedure that populates a table with many rows.
DROP DATABASE IF EXISTS singlestore_docs_example;CREATE DATABASE singlestore_docs_example;USE singlestore_docs_example;CREATE TABLE t(num INT);DELIMITER //CREATE PROCEDURE populate_table() ASDECLAREcounter INT = 0;BEGINWHILE counter < 500000 LOOPINSERT INTO t(num) VALUES (counter);counter = counter + 1;END LOOP;END//DELIMITER ;
Call the procedure to populate the table.
CALL populate_table();
Next, do an initial run of the query SELECT COUNT(DISTINCT num) FROM t
, followed by ANALYZE MEMORY
to collect statistics on the query.WITH
clause; this allows ESTIMATED_
to be included as a column in the query.
The initial run of the query returns -1
because statistics have not yet been collected.
WITH cte AS (SELECT COUNT(DISTINCT num) FROM t)SELECT ESTIMATED_QUERY_LEAF_MEMORY() FROM cte;
+-------------------------------+
| ESTIMATED_QUERY_LEAF_MEMORY() |
+-------------------------------+
| -1 |
+-------------------------------+
Next, collect statistics on the query:
ANALYZE MEMORY;
Finally, re-run the previous query to get the estimate:
WITH cte AS (SELECT COUNT(DISTINCT num) FROM t)SELECT ESTIMATED_QUERY_LEAF_MEMORY() FROM cte;
+-------------------------------+
| ESTIMATED_QUERY_LEAF_MEMORY() |
+-------------------------------+
| 4 |
+-------------------------------+
Last modified: February 16, 2024