CREATE DICTIONARY
A dictionary is a mapping (key -> attributes) that is convenient for various types of reference lists.
ClickHouse supports special functions for working with dictionaries that can be used in queries. It is easier and more efficient to use dictionaries with functions than a JOIN with reference tables.
Dictionaries can be created in two ways:
- With a DDL query (recommended)
- With a configuration file
Creating a dictionary with a DDL query
Dictionaries can be created with DDL queries. This is the recommended method because with DDL created dictionaries:
- No additional records are added to server configuration files.
- Dictionaries can be used like first-class entities such as tables or views.
- Data can be read directly, using familiar
SELECTsyntax rather than dictionary table functions. Note that when accessing a dictionary directly via aSELECTstatement, cached dictionary will return only cached data, while for a non-cached dictionary it will return all the data that it stores. - Dictionaries can be easily renamed.
Syntax
| Clause | Description |
|---|---|
| Attributes | Dictionary attributes are specified similarly to table columns. The only required property is the type, all others may have default values. |
| PRIMARY KEY | Defines the key column(s) for dictionary lookups. Depending on the layout, one or more attributes can be specified as keys. |
SOURCE | Defines the data source for the dictionary (e.g. ClickHouse table, HTTP, PostgreSQL). |
LAYOUT | Controls how the dictionary is stored in memory (e.g. FLAT, HASHED, CACHE). |
LIFETIME | Sets the refresh interval for the dictionary. |
ON CLUSTER | Creates the dictionary on a cluster. Optional. |
SETTINGS | Additional dictionary settings. Optional. |
COMMENT | Adds a text comment to the dictionary. Optional. |
Creating a dictionary with a configuration file
Creating a dictionary with a configuration file is not applicable to ClickHouse Cloud. Please use DDL (see above), and create your dictionary as the default user.
The dictionary configuration file has the following format:
You can configure any number of dictionaries in the same file.
Related content
- Layouts — How dictionaries are stored in memory
- Sources — Connecting to data sources
- Lifetime — Automatic refresh configuration
- Attributes — Key and attribute configuration
- Embedded Dictionaries — Built-in geobase dictionaries
- system.dictionaries — System table with dictionary information