zclllyybb commented on code in PR #2071: URL: https://github.com/apache/doris-website/pull/2071#discussion_r1959161782
########## i18n/zh-CN/docusaurus-plugin-content-docs/current/query-acceleration/dictionary.md: ########## @@ -0,0 +1,434 @@ +--- +{ + "title": "字典表(实验性功能)", + "language": "zh-CN" +} +--- + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +## 概述 + +字典表(Dictionary)是 Doris 提供的一种用于加速 JOIN 操作的特殊数据结构。它通过将常用的键值对预先加载到内存中,实现快速的查找操作,从而提升查询性能。字典表特别适用于需要频繁进行键值查找的场景。 + +## 使用场景 + +字典表主要适用于以下场景: + +1. 需要频繁进行键值查找的场景 +2. 维度表较小,可以完全加载到内存中 +3. 数据更新频率相对较低的场景 +4. 需要优化 JOIN 操作性能的场景 + +## 字典表定义 + +### 基本语法 + +```sql +CREATE DICTIONARY <dict_name> USING <source_table> +( + <key_column> KEY[, + ..., + <key_columns> VALUE] + <value_column> VALUE[, + ..., + <value_columns> VALUE] +) +LAYOUT(<layout_type>) +PROPERTIES( + "<priority_item_key>" = "<priority_item_value>"[, + ..., + "<priority_item_key>" = "<priority_item_value>"] +); +``` + +其中: + +- `<dict_name>`:字典表的名字 +- `<source_table>`:源数据表 +- `<key_column>`:作为键的列在源表中的列名 +- `<value_column>`:作为值的列在源表中的列名 +- `<layout_type>`:字典表的存储布局类型,详见后文。 +- `<priority_item_key>`:表的某项属性名 +- `<priority_item_value>`:表的某项属性取值 + +`<key_column>` 不必出现在 `<value_column>` 前。 + +### 布局类型 + +目前支持两种布局类型: + +- `HASH_MAP`:基于哈希表的实现,适用于一般的键值查找场景 +- `IP_TRIE`:基于 Trie 树的实现,专门优化用于 IP 地址类型的查找。Key 列需要为 CIDR 表示法表示的 IP 地址,查询时依 CIDR 表示法匹配。 + +### 属性 + +当前字典仅有一项允许且必须出现的属性: + +|属性名|值类型|含义| +|-|-|-| +|`date_lifetime`|整数,单位为秒|数据有效期。当该字典上次更新距今时间超过该值时,将会自动发起重新导入| + +### 示例 + +```sql +-- 创建源数据表 +CREATE TABLE source_table ( + id INT NOT NULL, + city VARCHAR(32) NOT NULL, + code VARCHAR(32) NOT NULL +) ENGINE=OLAP +DISTRIBUTED BY HASH(id) BUCKETS 1 +PROPERTIES("replication_num" = "1"); + +-- 创建字典表 +CREATE DICTIONARY city_dict USING source_table +( + city KEY, + id VALUE +) +LAYOUT(HASH_MAP) +PROPERTIES('data_lifetime' = '600'); +``` + +基于该表,我们可以使用字典 `city_dict` 通过 `dict_get` 函数,基于 `source_table` 的 `city` 值查询对应的 `id`。 + +### 使用限制 + +1. Key 列 + + - IP_TRIE 类型字典的 Key 列必须为 Varchar 或 String 类型,**Key 列中的值必须为 CIDR 格式**。 + - HASH_MAP 类型字典的 Key 列支持所有简单类型(即排除所有 Map、Array 等嵌套类型)。 + - 作为 Key 列的列,**在源表中不得存在重复值**,否则字典导入数据时将报错。 + +2. Nullable 属性 + + - 所有 Key 列必须为 NOT NULLABLE, Value 列无限制。 Review Comment: @Mryange -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org