docs/en/sql-reference/statements/create/dictionary/layouts/overview.md
import CloudDetails from '@site/docs/sql-reference/statements/create/dictionary/_snippet_dictionary_in_cloud.md'; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
There are a variety of ways to store dictionaries in memory, each with CPU and RAM-usage trade-offs.
| Layout | Description |
|---|---|
| flat | Stores data in flat arrays indexed by key. Fastest layout, but keys must be UInt64 and bounded by max_array_size. |
| hashed | Stores data in a hash table. No key size limit, supports any number of elements. |
| sparse_hashed | Like hashed, but trades CPU for lower memory usage. |
| complex_key_hashed | Like hashed, for composite keys. |
| complex_key_sparse_hashed | Like sparse_hashed, for composite keys. |
| hashed_array | Attributes stored in arrays with a hash table mapping keys to array indices. Memory-efficient for many attributes. |
| complex_key_hashed_array | Like hashed_array, for composite keys. |
| range_hashed | Hash table with ordered ranges. Supports lookups by key + date/time range. |
| complex_key_range_hashed | Like range_hashed, for composite keys. |
| cache | Fixed-size in-memory cache. Only frequently accessed keys are stored. |
| complex_key_cache | Like cache, for composite keys. |
| ssd_cache | Like cache, but stores data on SSD with an in-memory index. |
| complex_key_ssd_cache | Like ssd_cache, for composite keys. |
| direct | No in-memory storage — queries the source directly for each request. |
| complex_key_direct | Like direct, for composite keys. |
| ip_trie | Trie structure for fast IP prefix lookups (CIDR-based). |
:::tip Recommended layouts flat, hashed, and complex_key_hashed provide the best query performance. Caching layouts are not recommended due to potentially poor performance and difficulty tuning parameters — see cache for details. :::
You can configure a dictionary layout with the LAYOUT clause (for DDL) or the layout setting for configuration file definitions.
CREATE DICTIONARY (...)
...
LAYOUT(LAYOUT_TYPE(param value)) -- layout settings
...
<clickhouse>
<dictionary>
...
<layout>
<layout_type>
<!-- layout settings -->
</layout_type>
</layout>
...
</dictionary>
</clickhouse>
See also CREATE DICTIONARY for the full DDL syntax.
Dictionaries without word complex-key* in a layout have a key with UInt64 type, complex-key* dictionaries have a composite key (complex, with arbitrary types).
Numeric key example (column key_column has UInt64 type):
<Tabs> <TabItem value="ddl" label="DDL" default>CREATE DICTIONARY dict_name (
key_column UInt64,
...
)
PRIMARY KEY key_column
<structure>
<id>
<name>key_column</name>
</id>
...
</structure>
Composite key example (key has one element with String type):
<Tabs> <TabItem value="ddl" label="DDL" default>CREATE DICTIONARY dict_name (
country_code String,
...
)
PRIMARY KEY country_code
<structure>
<key>
<attribute>
<name>country_code</name>
<type>String</type>
</attribute>
</key>
...
</structure>
There are several ways to improve dictionary performance:
GROUP BY.GROUP BY uses a function that fetches an attribute value by the key, this function is automatically taken out of GROUP BY.ClickHouse generates an exception for errors with dictionaries. Examples of errors can be:
cached dictionary.You can view the list of dictionaries and their statuses in the system.dictionaries table.