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

> 字典键与属性配置

# 字典属性

<Tip>
  如果您在 ClickHouse Cloud 中使用字典，请使用 DDL 查询选项创建字典，并以 `default` 用户创建。
  此外，请在 [Cloud 兼容性指南](/zh/products/cloud/guides/cloud-compatibility) 中确认受支持的字典源列表。
</Tip>

`structure` 子句用于描述可供查询的字典键和各个字段。

XML 描述：

```xml theme={null}
<dictionary>
    <structure>
        <id>
            <name>Id</name>
        </id>

        <attribute>
            <!-- 属性参数 -->
        </attribute>

        ...

    </structure>
</dictionary>
```

属性由以下元素描述：

* `<id>` — 键列
* `<attribute>` — 数据列：属性可以有多个。

DDL 查询：

```sql theme={null}
CREATE DICTIONARY dict_name (
    Id UInt64,
    -- 属性
)
PRIMARY KEY Id
...
```

属性在查询语句主体中说明：

* `PRIMARY KEY` — 键列
* `AttrName AttrType` — 数据列。属性可以有多个。

<div id="key">
  ## 键
</div>

ClickHouse 支持以下几种键类型：

* 数值键。`UInt64`。在 `<id>` 标签中定义，或通过 `PRIMARY KEY` 关键字定义。
* 复合键。由不同类型的值组成的集合。在 `<key>` 标签中定义，或通过 `PRIMARY KEY` 关键字定义。

XML 结构中只能包含 `<id>` 或 `<key>` 其中之一。DDL 查询中必须且只能包含一个 `PRIMARY KEY`。

<Note>
  不得将键描述为属性。
</Note>

<div id="numeric-key">
  ### 数值键
</div>

类型：`UInt64`。

配置示例：

```xml theme={null}
<id>
    <name>Id</name>
</id>
```

配置字段：

* `name` – 存放键的列名。

对于 DDL 查询：

```sql theme={null}
CREATE DICTIONARY (
    Id UInt64,
    ...
)
PRIMARY KEY Id
...
```

* `PRIMARY KEY` – 键所在列的名称。

<div id="composite-key">
  ### 复合键
</div>

键可以是由任意类型的字段组成的 `tuple`。在这种情况下，[布局](/zh/reference/statements/create/dictionary/layouts/overview)必须为 `complex_key_hashed` 或 `complex_key_cache`。

<Tip>
  复合键也可以只包含一个元素。例如，这样就可以使用字符串作为键。
</Tip>

键的结构在 `<key>` 元素中设置。键字段的指定格式与字典[属性](#attributes)相同。示例：

```xml theme={null}
<structure>
    <key>
        <attribute>
            <name>field1</name>
            <type>String</type>
        </attribute>
        <attribute>
            <name>field2</name>
            <type>UInt32</type>
        </attribute>
        ...
    </key>
...
```

或

```sql theme={null}
CREATE DICTIONARY (
    field1 String,
    field2 UInt32
    ...
)
PRIMARY KEY field1, field2
...
```

对于 `dictGet*` 函数的查询，键会以元组形式传入。示例：`dictGetString('dict_name', 'attr_name', tuple('string for field1', num_for_field2))`。

<div id="attributes">
  ## 属性
</div>

配置示例：

```xml theme={null}
<structure>
    ...
    <attribute>
        <name>Name</name>
        <type>ClickHouseDataType</type>
        <null_value></null_value>
        <expression>rand64()</expression>
        <hierarchical>true</hierarchical>
        <injective>true</injective>
        <is_object_id>true</is_object_id>
    </attribute>
</structure>
```

或

```sql theme={null}
CREATE DICTIONARY somename (
    Name ClickHouseDataType DEFAULT '' EXPRESSION rand64() HIERARCHICAL INJECTIVE IS_OBJECT_ID
)
```

配置字段：

| 标签                                                 | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | 必填 |
| -------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -- |
| `name`                                             | 列名。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | 是  |
| `type`                                             | ClickHouse 数据类型：[UInt8](/zh/reference/data-types/int-uint)、[UInt16](/zh/reference/data-types/int-uint)、[UInt32](/zh/reference/data-types/int-uint)、[UInt64](/zh/reference/data-types/int-uint)、[Int8](/zh/reference/data-types/int-uint)、[Int16](/zh/reference/data-types/int-uint)、[Int32](/zh/reference/data-types/int-uint)、[Int64](/zh/reference/data-types/int-uint)、[Float32](/zh/reference/data-types/float)、[Float64](/zh/reference/data-types/float)、[UUID](/zh/reference/data-types/uuid)、[Decimal32](/zh/reference/data-types/decimal)、[Decimal64](/zh/reference/data-types/decimal)、[Decimal128](/zh/reference/data-types/decimal)、[Decimal256](/zh/reference/data-types/decimal)、[Date](/zh/reference/data-types/date)、[Date32](/zh/reference/data-types/date32)、[DateTime](/zh/reference/data-types/datetime)、[DateTime64](/zh/reference/data-types/datetime64)、[String](/zh/reference/data-types/string)、[Array](/zh/reference/data-types/array)。<br />ClickHouse 会尝试将字典中的值转换为指定的数据类型。例如，对于 MySQL，字段在 MySQL 源表中可能是 `TEXT`、`VARCHAR` 或 `BLOB`，但在 ClickHouse 中可以作为 `String` 导入。<br />[Nullable](/zh/reference/data-types/nullable) 目前支持用于 [Flat](/zh/reference/statements/create/dictionary/layouts/flat)、[Hashed](/zh/reference/statements/create/dictionary/layouts/hashed)、[ComplexKeyHashed](/zh/reference/statements/create/dictionary/layouts/hashed#complex_key_hashed)、[Direct](/zh/reference/statements/create/dictionary/layouts/direct)、[ComplexKeyDirect](/zh/reference/statements/create/dictionary/layouts/direct#complex_key_direct)、[RangeHashed](/zh/reference/statements/create/dictionary/layouts/range-hashed)、Polygon、[Cache](/zh/reference/statements/create/dictionary/layouts/cache)、[ComplexKeyCache](/zh/reference/statements/create/dictionary/layouts/cache)、[SSDCache](/zh/reference/statements/create/dictionary/layouts/ssd-cache)、[SSDComplexKeyCache](/zh/reference/statements/create/dictionary/layouts/ssd-cache#complex_key_ssd_cache) 字典。在 [IPTrie](/zh/reference/statements/create/dictionary/layouts/ip-trie) 字典中，不支持 `Nullable` 类型。 | 是  |
| `null_value`                                       | 不存在元素的默认值。<br />在该示例中，它是空字符串。只有 `Nullable` 类型才能使用 [NULL](/zh/reference/syntax#null) 值 (请参见上一行的类型说明) 。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | 是  |
| `expression`                                       | ClickHouse 在该值上执行的 [Expression](/zh/reference/syntax#expressions)。<br />该表达式可以是远程 SQL 数据库中的列名。因此，你可以用它为远程列创建别名。<br /><br />默认值：无表达式。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | 否  |
| <a name="hierarchical-dict-attr" /> `hierarchical` | 如果为 `true`，则该属性包含当前键的父键值。请参见 [Hierarchical Dictionaries](/zh/reference/statements/create/dictionary/layouts/hierarchical)。<br /><br />默认值：`false`。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | 否  |
| `injective`                                        | 用于表明 `id -> attribute` 映射是否为 [injective](https://en.wikipedia.org/wiki/Injective_function) 的标志。<br />如果为 `true`，ClickHouse 可以自动将具有单射性质的字典请求移到 `GROUP BY` 子句之后。通常这会显著减少此类请求的数量。<br /><br />默认值：`false`。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | 否  |
| `is_object_id`                                     | 用于表明查询是否通过 `ObjectID` 针对 MongoDB 文档执行的标志。<br /><br />默认值：`false`。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |    |
