Skip to main content
Beta feature Advice on use of Beta features This topic describes how to spill intermediate computation results of large operators to local disks and object storage.

Overview

For database systems that rely on in-memory computing for query execution, like the database, they can consume substantial memory resources when processing queries with aggregate, sort, and join operators on a big dataset. When memory limits are reached, these queries are forcibly terminated due to out-of-memory (OOM). However, there are still chances that you want certain memory-intensive tasks to be completed stably and performance is not your top priority, for example, building a materialized view, or performing a lightweight ETL with INSERT INTO SELECT. These tasks can easily exhaust your memory resources and thereby block other queries running in your cluster. Usually, to address this issue, you can only fine-tune these tasks individually, and rely on your resource isolation strategy to control the query concurrency. This could be particularly inconvenient and likely to fail under some extreme scenarios. From StarRocks v3.0.1, the database supports spilling the intermediate results of some memory-intensive operators to disks. With this feature, you can trade a tolerable drop in performance for a significant reduction in memory usage, thereby improving system availability. Currently, the database’ spilling feature supports the following operators:
  • Aggregate operators
  • Sort operators
  • Hash join (LEFT JOIN, RIGHT JOIN, FULL JOIN, OUTER JOIN, SEMI JOIN, and INNER JOIN) operators
  • CTE operators (Supported from v3.3.4 onwards)
  • Nested loop join (CROSS JOIN) operators (Supported from v3.1.0 onwards, and only when spill_mode is set to force)
  • Analytic (window) operators, for whole-partition window frames only, that is, the window either has no frame clause or specifies ROWS/RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, and each window function in the operator is one of sum, avg, count, min, max, first_value, and last_value
NOTE Spilling is enabled for all the supported operators by default. You can control the scope with the session variable spillable_operator_mask, a bitmask in which each operator type takes one bit: hash join (1), aggregate (2), distinct aggregate (4), sort (8), nest-loop join (16), multicast local exchange (32), and analytic (64). The default value -1 enables all of them. For example, to keep spilling enabled for the other operators but disable it for analytic operators, set spillable_operator_mask to -65.

Enable intermediate result spilling

Follow these steps to enable intermediate result spilling:
  1. (Optional) Specify the local spill directory spill_local_storage_dir, which stores the spilled intermediate result on the local disk, in the BE configuration file be.conf or the CN configuration file cn.conf, and restart the cluster to allow the modification to take effect. If you do not specify it, the default directory ${STARROCKS_HOME}/spill is used.
    NOTE
    • You can specify multiple spill_local_storage_dir by separating them with semicolons (;).
    • In a production environment, we strongly recommend you use different disks for data storage and spilling. When intermediate results are spilled to disk, there could be a significant increase in both writing load and disk usage. If the same disk is used, this surge can impact other queries or tasks running in the cluster.
  2. Execute the following statement to enable intermediate result spilling:
  3. Configure the mode of intermediate result spilling using the session variable spill_mode:
    NOTE Each time a query with spilling completes, the database automatically clears the spilled data the query produces. If BE crashes before clearing the data, the database clears it when the BE is restarted.

[Experimental] Adaptive spill column encoding

From this version onwards, BE supports an experimental adaptive column-codec selector for spilled data. When enabled, each spilled column samples a set of type-appropriate encodings (RLE, delta, frame-of-reference, patched FOR, dictionary, front coding, block compression, ALP for decimal-like doubles, and more) and picks the one minimizing an I/O-plus-CPU cost model instead of the fixed streamvbyte/LZ4 encoding. Both stages are on by default whenever enable_spill is true and are controlled per session: The cost model and the stage-2 compressor are tuned cluster-wide via BE configuration items:

[Preview] Spill intermediate result to object storage

From v3.3.0 onwards, the database supports spilling intermediate results to object storage. Tip Before enabling spilling to object storage, you must create a storage volume to define the object storage you want to use. For detailed instruction on creating a storage volume, see CREATE STORAGE VOLUME. After you have enabled spilling in the previous step, you can further set these system variables to allow the intermediate results to be spilled to object storage:
After spilling to object storage has been enabled, the intermediate results of queries that triggered spilling will be first stored in the local disks of the BE or CN nodes, and then the object storage if the capacity limit of the local disks is reached. Please note that, if the storage volume you specified for spill_storage_volume does not exist, spilling to object storage will not be enabled.

Limitations

  • Not all OOM issues can be resolved by spilling. For example, the database cannot release the memory used for expression evaluation.
  • Usually, queries with spilling involved indicate a tenfold increase in query latency. We recommend you extend the query timeout for these queries by setting the session variable query_timeout.
  • There is a significant performance drop in spilling to object storage compared to spilling to local disks.
  • spill_local_storage_dir of each BE or CN node is shared among all queries running on the node. Currently, the database does not support setting a size limit of spilled data to local disks individually for each query. Therefore, concurrent queries involved spilling may impact one another.