CACHE_RESULT is a subquery execution policy supported by the SUBQUERY Hint. It marks the results of a correlated subquery as cacheable. Upon receiving this Hint, the optimizer builds a Partial Result Cache for the subquery based on the correlation columns. When the outer query invokes the subquery multiple times with the same correlation key, the cache is hit, and the result is returned directly. This avoids repeated computation and scanning of the inner table.subquery_cache_enabled. Whether a subquery enters the cache is also influenced by the cost threshold subquery_cache_cost_threshold. Even if subquery_cache_enabled is OFF, the optimizer still enables caching for that subquery as long as the CACHE_RESULT Hint is explicitly specified in the SQL. The cache hit status can be observed through the status variables subquery_cache_hit and subquery_cache_miss.CACHE_RESULT must be used as a policy parameter of the SUBQUERY Hint and cannot be used independently./*+ SUBQUERY([@query_block_name] CACHE_RESULT) */
@query_block_name (Optional): The name of the query block where the target subquery resides. If @query_block_name is omitted, the Hint is inlined after the SELECT statement of the subquery and applies to that subquery itself. If @query_block_name is specified, you must name the query block using the QB_NAME Hint within the target subquery.SELECT keyword of the subquery.@query_block_name in the outer query.Variable Name | Type | Default Value | Scope | Description |
subquery_cache_enabled | System variable | OFF | Global,Session | Global switch for subquery result caching. After the CACHE_RESULT Hint is specified, the corresponding subquery still enables caching even if this switch is OFF. |
subquery_cache_cost_threshold | System variable | 4000 | Global,Session | Cost threshold for enabling subquery caching. The subquery goes to the caching path only when the estimated cost is higher than this threshold. |
subquery_cache_hit | Status Variables | - | Global,Session | Number of subquery result cache hits. |
subquery_cache_miss | Status Variables | - | Global,Session | Number of subquery result cache misses. |
CACHE_RESULT Hint when subquery result caching is disabled globally.CREATE TABLE t1 (a INT, b INT, c VARCHAR(32));CREATE TABLE t2 (a INT, b INT);INSERT INTO t1 VALUES(1, 10, 'row1'), (2, 20, 'row2'), (3, 30, 'row3'),(4, 40, 'row4'), (5, 50, 'row5');INSERT INTO t2 VALUES(1, 100), (1, 110), (2, 200), (2, 210),(3, 300), (3, 310), (4, 400), (5, 500);ANALYZE TABLE t1, t2;SET subquery_cache_enabled = OFF;FLUSH STATUS;
EXPLAIN FORMAT=TREESELECT *FROM t1WHERE b > (SELECT /*+ SUBQUERY(CACHE_RESULT) */ AVG(b)FROM t2WHERE t2.a = t1.a);
Partial result cache on keys(t1.a), which indicates that a cache has been built for the subquery based on the correlation column t1.a.EXPLAIN FORMAT=TREESELECT /*+ SUBQUERY(@subq1 CACHE_RESULT) */ *FROM t1WHERE b > (SELECT /*+ QB_NAME(subq1) */ AVG(b)FROM t2WHERE t2.a = t1.a);
SHOW STATUS LIKE 'subquery_cache%';
Variable_name Valuesubquery_cache_hit <hit count>subquery_cache_miss <miss count>
CACHE_RESULT takes effect only for correlated subqueries. Non-correlated subqueries are processed according to other subquery execution policies, and the Hint does not build a result cache.CACHE_RESULT cannot be used in conflict with mutually exclusive policies (such as MATERIALIZATION and INTOEXISTS) within the same SUBQUERY Hint. Otherwise, the Hint will not take effect.フィードバック