> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-detect-table-modification.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 反序列化列缓存中当前存储的每个条目对应一行记录。该缓存会将先前已读取并反序列化的 `MergeTree` 数据分区片段的列保留在内存中，这样在重复读取同一行范围时，就无需再次承担解压缩和反序列化的开销。

# system.columns_cache

<h2 id="description">
  描述
</h2>

当前存储在反序列化列缓存中的每个条目对应一行。该缓存会将 `MergeTree` 数据分区片段中先前已读取并反序列化的列保留在内存中，这样重复读取同一行范围时，就无需再次付出解压缩和反序列化的开销。

每个条目对应单个数据分区片段中单个列的一段连续粒度范围 `[row_begin, row_end)`，该范围位于该数据分区片段内一个约 65536 行的固定 stripe 之中。读取时按粒度逐个从缓存中获取数据，且对同一数据分区片段的每次读取而言，stripe 的划分都是相同的，因此条目与查询如何将数据分区片段切分为 mark ranges 无关：仅涉及某个 stripe 中部分粒度的读取只会缓存这些粒度，而由不同读取写入的相邻范围会被合并。

缓存通过表 UUID 标识条目，因此仅对会分配 UUID 的数据库中的表生效，例如 `Atomic`、`Replicated` 和 `Shared` (ClickHouse Cloud 中的默认数据库引擎) 。旧版 `Ordinary` 数据库中的表没有 UUID，会被静默排除在缓存之外。只有 wide parts 会参与缓存；compact parts 同样会被静默排除。

该缓存由服务器设置 `columns_cache_size` (默认为服务器可用内存的 `columns_cache_size_to_ram_ratio` 比例) 和 `columns_cache_size_ratio` 控制，同时受查询级别设置 `use_columns_cache`、`enable_reads_from_columns_cache`、`enable_writes_to_columns_cache`、`columns_cache_max_estimated_bytes_to_write_to_cache` 和 `columns_cache_max_bytes_to_write_to_cache` 控制。可以使用 [`SYSTEM DROP COLUMNS CACHE`](/zh/reference/statements/system) 手动清除该缓存。

各行会按访问权限进行过滤：只有同时有权查看条目所属表及其列的用户才能看到该条目，即需要对该表拥有 `SHOW TABLES` 权限，并对该列拥有 `SHOW COLUMNS` 权限。在 `*.*` 上授予的 `SHOW COLUMNS` 权限无法绕过针对单个表或列的权限撤销，因为此表公开的不仅是 schema，还包括运行数据 (parts 名称、行范围和缓存大小) 。对于已不存在的表，其条目没有可供校验的名称，因此只有拥有全局 `SHOW COLUMNS` 权限的用户才能看到这些条目。

<h2 id="columns">
  列
</h2>

* `database` ([String](/zh/reference/data-types/string)) — 该数据分区片段所属表的数据库名称。
* `table` ([String](/zh/reference/data-types/string)) — 表名。
* `table_uuid` ([UUID](/zh/reference/data-types/uuid)) — 表的 UUID。表重命名后保持不变。
* `part` ([String](/zh/reference/data-types/string)) — 数据分区片段的名称。
* `column` ([String](/zh/reference/data-types/string)) — 列名。
* `row_begin` ([UInt64](/zh/reference/data-types/int-uint)) — 缓存范围的起始行索引 (包含) 。
* `row_end` ([UInt64](/zh/reference/data-types/int-uint)) — 缓存范围的结束行索引 (不包含) 。
* `rows` ([UInt64](/zh/reference/data-types/int-uint)) — 缓存范围内的行数 (`row_end - row_begin`) 。
* `bytes` ([UInt64](/zh/reference/data-types/int-uint)) — 该条目占用的内存 (以字节为单位) ，即已缓存列的分配大小加上每个条目少量的额外开销。缓存容量正是按此数值来限制的，因此所有条目的该列之和始终不超过 `columns_cache_size`。该值可能大于这些行的逻辑大小，因为列会保留其分配时的容量。

<h2 id="examples">
  示例
</h2>

按表统计缓存列占用的内存总量：

```sql theme={null}
SELECT
    database,
    table,
    formatReadableSize(sum(bytes)) AS size,
    sum(rows) AS rows,
    count() AS entries
FROM system.columns_cache
GROUP BY database, table
ORDER BY sum(bytes) DESC
```

占用空间最大的单个缓存条目：

```sql theme={null}
SELECT database, table, part, column, row_begin, row_end, rows, formatReadableSize(bytes) AS size
FROM system.columns_cache
ORDER BY bytes DESC
LIMIT 10
```

<h2 id="see-also">
  另请参见
</h2>

* [SYSTEM DROP COLUMNS CACHE](/zh/reference/statements/system) — 用于清除列缓存中所有条目的语句。
