dictionary.md 3.2 KB
Newer Older
1
# Dictionary {#dictionary}
A
Andrey Dudin 已提交
2

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

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

7
``` xml
A
Andrey Dudin 已提交
8 9 10 11 12 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
<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>
```

38
Запрос данных словаря:
39

40
``` sql
B
BayoNet 已提交
41 42 43 44 45 46 47 48
SELECT
    name,
    type,
    key,
    attribute.names,
    attribute.types,
    bytes_allocated,
    element_count,
A
Andrey Dudin 已提交
49
    source
B
BayoNet 已提交
50
FROM system.dictionaries
A
Andrey Dudin 已提交
51
WHERE name = 'products'
52
```
53 54

``` text
A
Andrey Dudin 已提交
55 56 57 58 59
┌─name─────┬─type─┬─key────┬─attribute.names─┬─attribute.types─┬─bytes_allocated─┬─element_count─┬─source──────────┐
│ products │ Flat │ UInt64 │ ['title']       │ ['String']      │        23065376 │        175032 │ ODBC: .products │
└──────────┴──────┴────────┴─────────────────┴─────────────────┴─────────────────┴───────────────┴─────────────────┘
```

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

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

64
Синтаксис:
A
Andrey Dudin 已提交
65

66
``` sql
67 68 69 70
CREATE TABLE %table_name% (%fields%) engine = Dictionary(%dictionary_name%)`
```

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

72
``` sql
73
create table products (product_id UInt64, title String) Engine = Dictionary(products);
A
Andrey Dudin 已提交
74 75 76
```

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

78
``` sql
79 80
select * from products limit 1;
```
B
BayoNet 已提交
81

82
``` text
A
Andrey Dudin 已提交
83
┌────product_id─┬─title───────────┐
B
BayoNet 已提交
84
│        152689 │ Some item       │
A
Andrey Dudin 已提交
85 86
└───────────────┴─────────────────┘
```
I
Ivan Blinkov 已提交
87

I
Ivan Blinkov 已提交
88
[Оригинальная статья](https://clickhouse.tech/docs/ru/operations/table_engines/dictionary/) <!--hide-->