More intuitive active/passive enablement of query result cache

This commit is contained in:
Robert Schulze 2022-12-15 11:38:45 +00:00
parent f1e367179e
commit 973d4f75de
No known key found for this signature in database
GPG Key ID: 26703B55FB13728A
25 changed files with 155 additions and 135 deletions

View File

@ -31,18 +31,21 @@ moved to the server side. This reduces maintenance effort and avoids duplication
## Usage Examples and Configuration Settings
The two query/user/profile-level parameters [experimental_query_result_cache_active_usage](../../operations/settings/settings.md#setting-experimental_query_result_cache_active_usage)
and [experimental_query_result_cache_passive_usage](../../operations/settings/settings.md#setting-experimental_query_result_cache_passive_usage) control whether query results are
inserted or retrieved from the cache. For example, the first execution of query
The query/user/profile-level parameter [enable_experimental_query_result_cache](../../operations/settings/settings.md#enable-experimental-enable-query-result-cache)
controls whether query results are inserted or retrieved from the cache. For example, the first execution of query
``` sql
SELECT expensive_calculation(A, B, C)
FROM T
SETTINGS experimental_query_result_cache_active_usage = true, experimental_query_result_cache_passive_usage = false;
SETTINGS enable_experimental_query_result_cache = true;
```
will store the query result into the query result cache and subsequent executions will retrieve the result directly from the cache.
It is sometimes convenient to use the query result cache passively, i.e. to read from it but not write in it. Parameter
[enable_experimental_query_result_cache_passive_uage](../../operations/settings/settings.md#enable-experimental-enable-query-result-cache-passive-usage)
instead of 'enable_experimental_query_result_cache' can be used for that.
For maximum control, it is generally recommended to enable caching on a per-query basis. It is also possible to activate caching at
user/profile level if the user keeps in mind that all SELECT queries may return outdated results then.

View File

@ -1170,9 +1170,9 @@ Possible values:
Default value: `3`.
## experimental_query_result_cache_active_usage {#experimental-query-result-cache-active-usage}
## enable_experimental_query_result_cache {#experimental-query-result-cache}
If turned on, results of SELECT queries are stored in the query result cache.
If turned on, results of SELECT queries are stored in and (if available) retrieved from the query result cache.
Possible values:
@ -1181,9 +1181,9 @@ Possible values:
Default value: `0`.
## experimental_query_result_cache_passive_usage {#experimental-query-result-cache-passive-usage}
## enable_experimental_query_result_cache_passive_usage {#experimental-query-result-cache-passive-usage}
If turned on, the database tries to answer SELECT queries using a lookup in the query result cache.
If turned on, results of SELECT queries are (if available) retrieved from the query result cache.
Possible values:

View File

@ -665,8 +665,8 @@ static constexpr UInt64 operator""_GiB(unsigned long long value)
M(Bool, multiple_joins_try_to_keep_original_names, false, "Do not add aliases to top level expression list on multiple joins rewrite", 0) \
M(Bool, optimize_distinct_in_order, true, "Enable DISTINCT optimization if some columns in DISTINCT form a prefix of sorting. For example, prefix of sorting key in merge tree or ORDER BY statement", 0) \
M(Bool, optimize_sorting_by_input_stream_properties, true, "Optimize sorting by sorting properties of input stream", 0) \
M(Bool, experimental_query_result_cache_active_usage, false, "Store results of SELECT queries in the query result cache", 0) \
M(Bool, experimental_query_result_cache_passive_usage, false, "Lookup results of SELECT queries in the query result cache", 0) \
M(Bool, enable_experimental_query_result_cache, false, "Store and retrieve results of SELECT queries in/from the query result cache", 0) \
M(Bool, enable_experimental_query_result_cache_passive_usage, false, "Only retrieve results of SELECT queries from the query result cache", 0) \
M(Bool, query_result_cache_ignore_nondeterministic_functions, true, "Queries with non-deterministic functions (e.g. rand(), now()) are not stored in the query result cache", 0) \
M(UInt64, query_result_cache_size, (1ull << 30), "Maximum size of the query result cache in bytes. 0 means disabled.", 0) \
M(UInt64, query_result_cache_min_query_runs, 0, "Minimum number a SELECT query must run before its result is stored in the query result cache", 0) \

View File

@ -713,7 +713,7 @@ static std::tuple<ASTPtr, BlockIO> executeQueryImpl(
auto query_result_cache = context->getQueryResultCache();
if (settings.experimental_query_result_cache_passive_usage && query_result_cache != nullptr && res.pipeline.pulling())
if (settings.enable_experimental_query_result_cache && query_result_cache != nullptr && res.pipeline.pulling())
{
QueryResultCache::Key key{
ast, context->getUserName(), settings.query_result_cache_partition_key, res.pipeline.getHeader(),
@ -723,7 +723,8 @@ static std::tuple<ASTPtr, BlockIO> executeQueryImpl(
res.pipeline = QueryPipeline(reader.getPipe());
}
if (settings.experimental_query_result_cache_active_usage && query_result_cache != nullptr && res.pipeline.pulling() && !hasNonCacheableFunctions(ast, context))
if ((settings.enable_experimental_query_result_cache_passive_usage || settings.enable_experimental_query_result_cache)
&& query_result_cache != nullptr && res.pipeline.pulling() && !hasNonCacheableFunctions(ast, context))
{
QueryResultCache::Key key{
ast, context->getUserName(), settings.query_result_cache_partition_key, res.pipeline.getHeader(),

View File

@ -1,7 +1,7 @@
-- { echoOn }
-- run query and cache query result
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
1
SELECT count(*) FROM system.queryresult_cache;
1

View File

@ -1,7 +1,7 @@
-- { echoOn }
-- run query and cache query result
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
SELECT count(*) FROM system.queryresult_cache;
-- query results are no longer in cache after drop

View File

@ -1,20 +0,0 @@
-- { echoOn }
SYSTEM DROP QUERY RESULT CACHE;
-- don't write query result into cache
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = false;
1
SELECT count(*) FROM system.queryresult_cache;
0
-- try to retrieve from cache (which is empty)
SELECT 1 SETTINGS experimental_query_result_cache_passive_usage = true;
1
-- put query result into cache
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true;
1
SELECT count(*) FROM system.queryresult_cache;
1
-- put query result in cache and access it in further queries
SELECT 1 SETTINGS experimental_query_result_cache_passive_usage = true;
1
SYSTEM DROP QUERY RESULT CACHE;

View File

@ -1,21 +0,0 @@
-- { echoOn }
SYSTEM DROP QUERY RESULT CACHE;
-- don't write query result into cache
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = false;
SELECT count(*) FROM system.queryresult_cache;
-- try to retrieve from cache (which is empty)
SELECT 1 SETTINGS experimental_query_result_cache_passive_usage = true;
-- put query result into cache
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true;
SELECT count(*) FROM system.queryresult_cache;
-- put query result in cache and access it in further queries
SELECT 1 SETTINGS experimental_query_result_cache_passive_usage = true;
SYSTEM DROP QUERY RESULT CACHE;
-- { echoOff }

View File

@ -2,11 +2,12 @@
SYSTEM DROP QUERY RESULT CACHE;
-- rand() is non-deterministic, with default settings no entry in the query result cache should be created
SELECT count(rand(1)) SETTINGS experimental_query_result_cache_active_usage = true;
SELECT count(rand(1)) SETTINGS enable_experimental_query_result_cache = true;
1
SELECT count(*) FROM system.queryresult_cache;
0
SELECT count(rand(1)) SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_ignore_nondeterministic_functions = false;
-- force an entry
SELECT count(rand(1)) SETTINGS enable_experimental_query_result_cache = true, query_result_cache_ignore_nondeterministic_functions = false;
1
SELECT count(*) FROM system.queryresult_cache;
1

View File

@ -3,10 +3,11 @@
SYSTEM DROP QUERY RESULT CACHE;
-- rand() is non-deterministic, with default settings no entry in the query result cache should be created
SELECT count(rand(1)) SETTINGS experimental_query_result_cache_active_usage = true;
SELECT count(rand(1)) SETTINGS enable_experimental_query_result_cache = true;
SELECT count(*) FROM system.queryresult_cache;
SELECT count(rand(1)) SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_ignore_nondeterministic_functions = false;
-- force an entry
SELECT count(rand(1)) SETTINGS enable_experimental_query_result_cache = true, query_result_cache_ignore_nondeterministic_functions = false;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;

View File

@ -2,7 +2,7 @@
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_keep_seconds_alive = 3;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_keep_seconds_alive = 3;
-- expect one non-stale cache entry
SELECT count(*) FROM system.queryresult_cache;
@ -15,7 +15,7 @@ SELECT sleep(3);
SELECT stale FROM system.queryresult_cache;
-- same query as before
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_keep_seconds_alive = 3;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_keep_seconds_alive = 3;
-- expect one non-stale cache entry
SELECT count(*) FROM system.queryresult_cache;

View File

@ -1,14 +1,14 @@
-- { echoOn }
SYSTEM DROP QUERY RESULT CACHE;
-- store one entry into query result cache
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true;
-- store an entry into query result cache
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
1
SELECT count(*) FROM system.queryresult_cache;
1
-- restrict entry count to 1, other queries create no further entries
SET query_result_cache_max_entries = 1;
SELECT 2 SETTINGS experimental_query_result_cache_active_usage = true;
SELECT 2 SETTINGS enable_experimental_query_result_cache = true;
2
SELECT count(*) FROM system.queryresult_cache;
1

View File

@ -2,13 +2,13 @@
SYSTEM DROP QUERY RESULT CACHE;
-- store one entry into query result cache
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true;
-- store an entry into query result cache
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
SELECT count(*) FROM system.queryresult_cache;
-- restrict entry count to 1, other queries create no further entries
SET query_result_cache_max_entries = 1;
SELECT 2 SETTINGS experimental_query_result_cache_active_usage = true;
SELECT 2 SETTINGS enable_experimental_query_result_cache = true;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;

View File

@ -2,13 +2,15 @@
SYSTEM DROP QUERY RESULT CACHE;
-- this does not create a cache entry ...
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_max_entry_records = 0;
SET query_result_cache_max_entry_records = 0;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
1
SELECT count(*) FROM system.queryresult_cache;
0
SYSTEM DROP QUERY RESULT CACHE;
-- ... but this does
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_max_entry_records = 999999999;
SET query_result_cache_max_entry_records = 9999999;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
1
SELECT count(*) FROM system.queryresult_cache;
1

View File

@ -3,13 +3,15 @@
SYSTEM DROP QUERY RESULT CACHE;
-- this does not create a cache entry ...
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_max_entry_records = 0;
SET query_result_cache_max_entry_records = 0;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;
-- ... but this does
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_max_entry_records = 999999999;
SET query_result_cache_max_entry_records = 9999999;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;

View File

@ -2,13 +2,15 @@
SYSTEM DROP QUERY RESULT CACHE;
-- this does not create a cache entry ...
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_max_entry_size = 0;
SET query_result_cache_max_entry_size = 0;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
1
SELECT count(*) FROM system.queryresult_cache;
0
SYSTEM DROP QUERY RESULT CACHE;
-- ... but this does
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_max_entry_size = 999999999;
SET query_result_cache_max_entry_size = 9999999;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
1
SELECT count(*) FROM system.queryresult_cache;
1

View File

@ -3,13 +3,15 @@
SYSTEM DROP QUERY RESULT CACHE;
-- this does not create a cache entry ...
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_max_entry_size = 0;
SET query_result_cache_max_entry_size = 0;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;
-- ... but this does
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_max_entry_size = 999999999;
SET query_result_cache_max_entry_size = 9999999;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;

View File

@ -0,0 +1,30 @@
-- { echoOn }
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 0;
1
SELECT count(*) FROM system.queryresult_cache;
1
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 1;
1
SELECT count(*) FROM system.queryresult_cache;
0
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 1;
1
SELECT count(*) FROM system.queryresult_cache;
1
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 2;
1
SELECT count(*) FROM system.queryresult_cache;
0
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 2;
1
SELECT count(*) FROM system.queryresult_cache;
0
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 2;
1
SELECT count(*) FROM system.queryresult_cache;
1
SYSTEM DROP QUERY RESULT CACHE;

View File

@ -0,0 +1,26 @@
-- { echoOn }
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 0;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 1;
SELECT count(*) FROM system.queryresult_cache;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 1;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 2;
SELECT count(*) FROM system.queryresult_cache;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 2;
SELECT count(*) FROM system.queryresult_cache;
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_min_query_runs = 2;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;
-- { echoOff }

View File

@ -3,11 +3,11 @@
SYSTEM DROP QUERY RESULT CACHE;
-- the same query with two different partition keys is written twice into the query result cache
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_partition_key = 'key 1';
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_partition_key = 'key 1';
1
SELECT count(*) FROM system.queryresult_cache;
1
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_partition_key = 'key 2';
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_partition_key = 'key 2';
1
SELECT count(*) FROM system.queryresult_cache;
2

View File

@ -4,10 +4,10 @@ SYSTEM DROP QUERY RESULT CACHE;
-- the same query with two different partition keys is written twice into the query result cache
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_partition_key = 'key 1';
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_partition_key = 'key 1';
SELECT count(*) FROM system.queryresult_cache;
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_partition_key = 'key 2';
SELECT 1 SETTINGS enable_experimental_query_result_cache = true, query_result_cache_partition_key = 'key 2';
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;

View File

@ -0,0 +1,24 @@
-- { echoOn }
SYSTEM DROP QUERY RESULT CACHE;
-- by default don't write query result into cache
SELECT 1;
1
SELECT count(*) FROM system.queryresult_cache;
0
-- try to retrieve from empty cache, cache should still be empty
SELECT 1 SETTINGS enable_experimental_query_result_cache_passive_usage = true;
1
SELECT count(*) FROM system.queryresult_cache;
1
-- put query result into cache
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
1
SELECT count(*) FROM system.queryresult_cache;
2
-- read from query result cache
SELECT 1 SETTINGS enable_experimental_query_result_cache_passive_usage = true;
1
SELECT count(*) FROM system.queryresult_cache;
2
SYSTEM DROP QUERY RESULT CACHE;

View File

@ -0,0 +1,23 @@
-- { echoOn }
SYSTEM DROP QUERY RESULT CACHE;
-- by default don't write query result into cache
SELECT 1;
SELECT count(*) FROM system.queryresult_cache;
-- try to retrieve from empty cache, cache should still be empty
SELECT 1 SETTINGS enable_experimental_query_result_cache_passive_usage = true;
SELECT count(*) FROM system.queryresult_cache;
-- put query result into cache
SELECT 1 SETTINGS enable_experimental_query_result_cache = true;
SELECT count(*) FROM system.queryresult_cache;
-- read from query result cache
SELECT 1 SETTINGS enable_experimental_query_result_cache_passive_usage = true;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;
-- { echoOff }

View File

@ -1,30 +0,0 @@
-- { echoOn }
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 0;
1
SELECT count(*) FROM system.queryresult_cache;
1
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 1;
1
SELECT count(*) FROM system.queryresult_cache;
0
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 1;
1
SELECT count(*) FROM system.queryresult_cache;
1
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 2;
1
SELECT count(*) FROM system.queryresult_cache;
0
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 2;
1
SELECT count(*) FROM system.queryresult_cache;
0
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 2;
1
SELECT count(*) FROM system.queryresult_cache;
1
SYSTEM DROP QUERY RESULT CACHE;

View File

@ -1,26 +0,0 @@
-- { echoOn }
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 0;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 1;
SELECT count(*) FROM system.queryresult_cache;
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 1;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 2;
SELECT count(*) FROM system.queryresult_cache;
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 2;
SELECT count(*) FROM system.queryresult_cache;
SELECT 1 SETTINGS experimental_query_result_cache_active_usage = true, query_result_cache_min_query_runs = 2;
SELECT count(*) FROM system.queryresult_cache;
SYSTEM DROP QUERY RESULT CACHE;
-- { echoOff }