Skip to content

Query cache is not isolated across different databases #64136

@realyota

Description

@realyota

Tested on 24.3.2.23 (official build).

Describe the unexpected behaviour
Query cache is used for the same tables created in different databases when no database alias is used.

How to reproduce

Prepare debug environment and data:

CREATE DATABASE test ENGINE = Atomic;

CREATE TABLE default.test
(
    `a` UInt64,
    `b` UUID,
    `c` UInt32 DEFAULT xxHash32(b),
    `d` DateTime,
    `error_count` UInt8,
    INDEX idx_error error_count TYPE minmax GRANULARITY 1
)
ENGINE = ReplacingMergeTree(d)
PARTITION BY toYYYYMM(d)
ORDER BY (a, c)
TTL d + toIntervalMonth(1)
SETTINGS index_granularity = 6, ttl_only_drop_parts = 1;

CREATE TABLE test.test AS default.test;

INSERT INTO test.test SELECT number, generateUUIDv4(), number, '2024-05-20 14:00:00'::DateTime, 2 FROM numbers(10e7);

ALTER TABLE default.test ATTACH PARTITION '202405' FROM test.test;

Now test query cache:

USE default;

SELECT a % 100 ids, max(b) uuids, sum(error_count) ec FROM test GROUP BY ids ORDER BY ids SETTINGS use_query_cache=1;

100 rows in set. Elapsed: 11.742 sec. Processed 75.49 million rows, 1.89 GB (6.43 million rows/s., 160.73 MB/s.)

SELECT * FROM system.query_cache;
      ┌─query───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬─result_size─┬─stale─┬─shared─┬─compressed─┬──────────expires_at─┬────────────key_hash─┐
1. │ SELECT a % 100 AS ids, max(b) AS uuids, sum(error_count) AS ec FROM test GROUP BY ids ORDER BY ids ASC SETTINGS use_query_cache = 1 │        3328 │     0 │      0 │          1 │ 2024-05-20 14:09:44 │ 5831134195536845086 │
   └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴─────────────┴───────┴────────┴────────────┴─────────────────────┴─────────────────────┘

USE test;

SELECT a % 100 ids, max(b) uuids, sum(error_count) ec FROM test GROUP BY ids ORDER BY ids SETTINGS use_query_cache=1;

100 rows in set. Elapsed: 0.001 sec.

SELECT * FROM system.query_cache;
   
┌─query───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬─result_size─┬─stale─┬─shared─┬─compressed─┬──────────expires_at─┬────────────key_hash─┐
1. │ SELECT a % 100 AS ids, max(b) AS uuids, sum(error_count) AS ec FROM test GROUP BY ids ORDER BY ids ASC SETTINGS use_query_cache = 1 │        3328 │     0 │      0 │          1 │ 2024-05-20 14:09:44 │ 5831134195536845086 │
   └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴─────────────┴───────┴────────┴────────────┴─────────────────────┴─────────────────────┘

Expected behavior
I expect the query cache to be aware that I'm selecting the data from another table (same table name in another database).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions