dictionary.md 3.1 KB
Newer Older
1 2 3 4 5
---
toc_priority: 35
toc_title: Dictionary
---

6
# Dictionary {#dictionary}
A
Andrey Dudin 已提交
7

8
Движок `Dictionary` отображает данные [словаря](../../../sql-reference/dictionaries/external-dictionaries/external-dicts.md) как таблицу ClickHouse.
A
Andrey Dudin 已提交
9

10
Рассмотрим для примера словарь `products` со следующей конфигурацией:
A
Andrey Dudin 已提交
11

12
``` xml
A
Andrey Dudin 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<dictionaries>
<dictionary>
        <name>products</name>
        <source>
            <odbc>
                <table>products</table>
                <connection_string>DSN=some-db-server</connection_string>
            </odbc>
        </source>
        <lifetime>
            <min>300</min>
            <max>360</max>
        </lifetime>
        <layout>
            <flat/>
        </layout>
        <structure>
            <id>
                <name>product_id</name>
            </id>
            <attribute>
                <name>title</name>
                <type>String</type>
                <null_value></null_value>
            </attribute>
        </structure>
</dictionary>
</dictionaries>
```

43
Запрос данных словаря:
44

45
``` sql
B
BayoNet 已提交
46 47 48 49 50 51 52 53
SELECT
    name,
    type,
    key,
    attribute.names,
    attribute.types,
    bytes_allocated,
    element_count,
A
Andrey Dudin 已提交
54
    source
B
BayoNet 已提交
55
FROM system.dictionaries
A
Andrey Dudin 已提交
56
WHERE name = 'products'
57
```
58 59

``` text
A
Andrey Dudin 已提交
60 61 62 63 64
┌─name─────┬─type─┬─key────┬─attribute.names─┬─attribute.types─┬─bytes_allocated─┬─element_count─┬─source──────────┐
│ products │ Flat │ UInt64 │ ['title']       │ ['String']      │        23065376 │        175032 │ ODBC: .products │
└──────────┴──────┴────────┴─────────────────┴─────────────────┴─────────────────┴───────────────┴─────────────────┘
```

65
В таком виде данные из словаря можно получить при помощи функций [dictGet\*](../../../engines/table-engines/special/dictionary.md#ext_dict_functions).
A
Andrey Dudin 已提交
66

67
Такое представление неудобно, когда нам необходимо получить данные в чистом виде, а также при выполнении операции `JOIN`. Для этих случаев можно использовать движок `Dictionary`, который отобразит данные словаря в таблицу.
A
Andrey Dudin 已提交
68

69
Синтаксис:
A
Andrey Dudin 已提交
70

71
``` sql
72 73 74 75
CREATE TABLE %table_name% (%fields%) engine = Dictionary(%dictionary_name%)`
```

Пример использования:
A
Andrey Dudin 已提交
76

77
``` sql
78
create table products (product_id UInt64, title String) Engine = Dictionary(products);
A
Andrey Dudin 已提交
79 80 81
```

Проверим что у нас в таблице?
82

83
``` sql
84 85
select * from products limit 1;
```
B
BayoNet 已提交
86

87
``` text
A
Andrey Dudin 已提交
88
┌────product_id─┬─title───────────┐
B
BayoNet 已提交
89
│        152689 │ Some item       │
A
Andrey Dudin 已提交
90 91
└───────────────┴─────────────────┘
```
I
Ivan Blinkov 已提交
92