> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> Log 文档

# Log 表引擎

export const CloudNotSupportedBadge = () => {
  return <div className="cloudNotSupportedBadge">
            <div className="cloudNotSupportedIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.5" d="M6.33366 12.6666L12.3739 12.6667C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00003 12.3739 8.00003C12.3739 8.00003 12.3337 7.66659 12.0003 7.33325M10.667 5.33322C8.00033 2.33325 4.45395 4.78537 4.14195 6.68203C2.55728 6.7627 1.29395 8.06203 1.29395 9.6667C1.29395 11.3234 2.66699 12.6666 4.00033 12.6666" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.5" d="M2.66699 14L12.0003 4.66663" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>

        </div>
            ClickHouse Cloud 不支持此功能
        </div>;
};

<CloudNotSupportedBadge />

该引擎属于 `Log` 引擎家族。有关 `Log` 引擎的通用特性及其差异，请参阅 [Log Engine Family](/docs/zh/reference/engines/table-engines/log-family/index) 一文。

`Log` 与 [TinyLog](/docs/zh/reference/engines/table-engines/log-family/tinylog) 的不同之处在于，列文件旁边会有一个较小的“标记”文件。这些标记会在每个数据块处写入，包含偏移量，用于指示从文件的何处开始读取，以跳过指定数量的行。因此，可以使用多个线程读取表数据。
在并发数据访问场景下，读取操作可以同时执行，而写入操作会阻塞读取操作以及其他写入操作。
`Log` 引擎不支持索引。同样，如果向表写入失败，该表就会损坏，读取时会返回错误。`Log` 引擎适用于临时数据、只写入一次的表，以及测试或演示用途。

<div id="table_engines-log-creating-a-table">
  ## 创建表
</div>

```sql theme={null}
CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
    column1_name [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
    column2_name [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
    ...
) ENGINE = Log
```

请参阅 [CREATE TABLE](/docs/zh/reference/statements/create/table) 查询的详细描述。

<div id="table_engines-log-writing-the-data">
  ## 写入数据
</div>

`Log` 引擎通过将每一列分别写入各自的文件来高效存储数据。对于每个表，Log 引擎会将以下文件写入指定的存储路径：

* `<column>.bin`：每一列对应的数据文件，包含序列化并压缩后的数据。
  `__marks.mrk`：标记文件，存储每个插入数据块的偏移量和行数。标记可让引擎在读取时跳过无关的数据块，从而提高查询执行效率。

<div id="writing-process">
  ### 写入过程
</div>

当数据写入 `Log` 表时：

1. 数据会被序列化并压缩为块。
2. 对于每一列，压缩后的数据都会追加到对应的 `<column>.bin` 文件中。
3. `__marks.mrk` 文件中会添加相应的条目，用于记录新插入数据的偏移量和行数。

<div id="table_engines-log-reading-the-data">
  ## 读取数据
</div>

标记文件使 ClickHouse 能够并行读取数据。这意味着 `SELECT` 查询返回的行顺序是不可预测的。请使用 `ORDER BY` 子句对行进行排序。

<div id="table_engines-log-example-of-use">
  ## 使用示例
</div>

创建表：

```sql theme={null}
CREATE TABLE log_table
(
    timestamp DateTime,
    message_type String,
    message String
)
ENGINE = Log
```

插入数据：

```sql theme={null}
INSERT INTO log_table VALUES (now(),'REGULAR','The first regular message')
INSERT INTO log_table VALUES (now(),'REGULAR','The second regular message'),(now(),'WARNING','The first warning message')
```

我们使用了两条 `INSERT` 查询，在 `<column>.bin` 文件中创建了两个数据块。

ClickHouse 在查询数据时会使用多个线程。每个线程会读取一个单独的数据块，并在完成后独立返回相应的结果行。因此，输出中各个行块的顺序可能与输入中这些块的顺序不一致。例如：

```sql theme={null}
SELECT * FROM log_table
```

```text theme={null}
┌───────────timestamp─┬─message_type─┬─message────────────────────┐
│ 2019-01-18 14:27:32 │ REGULAR      │ The second regular message │
│ 2019-01-18 14:34:53 │ WARNING      │ The first warning message  │
└─────────────────────┴──────────────┴────────────────────────────┘
┌───────────timestamp─┬─message_type─┬─message───────────────────┐
│ 2019-01-18 14:23:43 │ REGULAR      │ The first regular message │
└─────────────────────┴──────────────┴───────────────────────────┘
```

对结果进行排序 (默认按升序) ：

```sql theme={null}
SELECT * FROM log_table ORDER BY timestamp
```

```text theme={null}
┌───────────timestamp─┬─message_type─┬─message────────────────────┐
│ 2019-01-18 14:23:43 │ REGULAR      │ The first regular message  │
│ 2019-01-18 14:27:32 │ REGULAR      │ The second regular message │
│ 2019-01-18 14:34:53 │ WARNING      │ The first warning message  │
└─────────────────────┴──────────────┴────────────────────────────┘
```
