ClickHouse/docs/en/engines/table-engines/special/dictionary.md

98 lines
2.9 KiB
Markdown
Raw Normal View History

2020-04-03 13:23:32 +00:00
---
toc_priority: 35
toc_title: Dictionary
---
2020-06-10 20:20:54 +00:00
# Dictionary Table Engine {#dictionary}
2018-03-25 02:04:22 +00:00
The `Dictionary` engine displays the [dictionary](../../../sql-reference/dictionaries/external-dictionaries/external-dicts.md) data as a ClickHouse table.
2018-03-25 02:04:22 +00:00
## Example {#example}
2020-06-10 20:20:54 +00:00
2018-03-25 02:04:22 +00:00
As an example, consider a dictionary of `products` with the following configuration:
2020-03-20 10:10:48 +00:00
``` xml
2018-03-25 02:04:22 +00:00
<dictionaries>
2020-06-10 20:20:54 +00:00
<dictionary>
2018-03-25 02:04:22 +00:00
<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>
2020-06-10 20:20:54 +00:00
</dictionary>
2018-03-25 02:04:22 +00:00
</dictionaries>
```
Query the dictionary data:
2020-03-20 10:10:48 +00:00
``` sql
2018-05-08 11:50:38 +00:00
SELECT
name,
type,
key,
attribute.names,
attribute.types,
bytes_allocated,
element_count,
2018-03-25 02:04:22 +00:00
source
2018-05-08 11:50:38 +00:00
FROM system.dictionaries
2018-03-25 02:04:22 +00:00
WHERE name = 'products'
```
2018-04-23 06:20:21 +00:00
2020-03-20 10:10:48 +00:00
``` text
2018-03-25 02:04:22 +00:00
┌─name─────┬─type─┬─key────┬─attribute.names─┬─attribute.types─┬─bytes_allocated─┬─element_count─┬─source──────────┐
│ products │ Flat │ UInt64 │ ['title'] │ ['String'] │ 23065376 │ 175032 │ ODBC: .products │
└──────────┴──────┴────────┴─────────────────┴─────────────────┴─────────────────┴───────────────┴─────────────────┘
```
You can use the [dictGet\*](../../../sql-reference/functions/ext-dict-functions.md#ext_dict_functions) function to get the dictionary data in this format.
2018-03-25 02:04:22 +00:00
2020-03-20 10:10:48 +00:00
This view isnt helpful when you need to get raw data, or when performing a `JOIN` operation. For these cases, you can use the `Dictionary` engine, which displays the dictionary data in a table.
2018-03-25 02:04:22 +00:00
Syntax:
2020-03-20 10:10:48 +00:00
``` sql
2018-03-25 02:04:22 +00:00
CREATE TABLE %table_name% (%fields%) engine = Dictionary(%dictionary_name%)`
```
Usage example:
2020-03-20 10:10:48 +00:00
``` sql
2018-03-25 02:04:22 +00:00
create table products (product_id UInt64, title String) Engine = Dictionary(products);
```
Ok
2018-03-25 02:04:22 +00:00
2020-03-20 10:10:48 +00:00
Take a look at whats in the table.
``` sql
2018-03-25 02:04:22 +00:00
select * from products limit 1;
```
2018-05-08 11:50:38 +00:00
2020-03-20 10:10:48 +00:00
``` text
2018-03-25 02:04:22 +00:00
┌────product_id─┬─title───────────┐
2018-05-08 11:50:38 +00:00
│ 152689 │ Some item │
2018-03-25 02:04:22 +00:00
└───────────────┴─────────────────┘
```
2020-01-30 10:34:55 +00:00
[Original article](https://clickhouse.tech/docs/en/operations/table_engines/dictionary/) <!--hide-->