2020-04-03 13:23:32 +00:00
---
toc_priority: 41
toc_title: Storing Dictionaries in Memory
---
2020-04-30 18:19:18 +00:00
# Storing Dictionaries in Memory {#dicts-external-dicts-dict-layout}
2017-12-28 15:13:23 +00:00
2018-12-12 17:28:00 +00:00
There are a variety of ways to store dictionaries in memory.
2017-12-28 15:13:23 +00:00
2021-04-21 16:25:04 +00:00
We recommend [flat ](#flat ), [hashed ](#dicts-external_dicts_dict_layout-hashed ) and [complex_key_hashed ](#complex-key-hashed ), which provide optimal processing speed.
2017-12-28 15:13:23 +00:00
2021-04-21 16:25:04 +00:00
Caching is not recommended because of potentially poor performance and difficulties in selecting optimal parameters. Read more in the section [cache ](#cache ).
2017-12-28 15:13:23 +00:00
There are several ways to improve dictionary performance:
2020-03-21 04:11:51 +00:00
- Call the function for working with the dictionary after `GROUP BY` .
- Mark attributes to extract as injective. An attribute is called injective if different attribute values correspond to different keys. So when `GROUP BY` uses a function that fetches an attribute value by the key, this function is automatically taken out of `GROUP BY` .
2017-12-28 15:13:23 +00:00
ClickHouse generates an exception for errors with dictionaries. Examples of errors:
2020-03-21 04:11:51 +00:00
- The dictionary being accessed could not be loaded.
- Error querying a `cached` dictionary.
2017-12-28 15:13:23 +00:00
You can view the list of external dictionaries and their statuses in the `system.dictionaries` table.
The configuration looks like this:
2020-03-20 10:10:48 +00:00
``` xml
2021-10-26 05:50:15 +00:00
< clickhouse >
2017-12-28 15:13:23 +00:00
< dictionary >
...
< layout >
2018-03-10 23:36:26 +00:00
< layout_type >
2017-12-28 15:13:23 +00:00
<!-- layout settings -->
< / layout_type >
< / layout >
...
< / dictionary >
2021-10-26 05:50:15 +00:00
< / clickhouse >
2017-12-28 15:13:23 +00:00
```
2020-07-09 15:10:35 +00:00
Corresponding [DDL-query ](../../../sql-reference/statements/create/dictionary.md ):
2019-11-13 15:50:09 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-11-13 15:50:09 +00:00
CREATE DICTIONARY (...)
...
LAYOUT(LAYOUT_TYPE(param value)) -- layout settings
...
```
2020-04-30 18:19:18 +00:00
## Ways to Store Dictionaries in Memory {#ways-to-store-dictionaries-in-memory}
2017-12-28 15:13:23 +00:00
2020-03-21 04:11:51 +00:00
- [flat ](#flat )
- [hashed ](#dicts-external_dicts_dict_layout-hashed )
2020-10-13 17:23:29 +00:00
- [sparse_hashed ](#dicts-external_dicts_dict_layout-sparse_hashed )
- [complex_key_hashed ](#complex-key-hashed )
2021-10-22 16:36:29 +00:00
- [complex_key_sparse_hashed ](#complex-key-sparse-hashed )
- [hashed_array ](#dicts-external_dicts_dict_layout-hashed-array )
- [complex_key_hashed_array ](#complex-key-hashed-array )
- [range_hashed ](#range-hashed )
2021-08-21 19:44:12 +00:00
- [complex_key_range_hashed ](#complex-key-range-hashed )
2021-10-22 16:36:29 +00:00
- [cache ](#cache )
2020-10-13 17:23:29 +00:00
- [complex_key_cache ](#complex-key-cache )
2020-11-04 00:47:42 +00:00
- [ssd_cache ](#ssd-cache )
2021-10-22 16:36:29 +00:00
- [complex_key_ssd_cache ](#complex-key-ssd-cache )
- [direct ](#direct )
2020-10-13 17:23:29 +00:00
- [complex_key_direct ](#complex-key-direct )
- [ip_trie ](#ip-trie )
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
### flat {#flat}
2017-12-28 15:13:23 +00:00
The dictionary is completely stored in memory in the form of flat arrays. How much memory does the dictionary use? The amount is proportional to the size of the largest key (in space used).
2021-04-23 08:40:05 +00:00
The dictionary key has the [UInt64 ](../../../sql-reference/data-types/int-uint.md ) type and the value is limited to `max_array_size` (by default — 500,000). If a larger key is discovered when creating the dictionary, ClickHouse throws an exception and does not create the dictionary. Dictionary flat arrays initial size is controlled by `initial_array_size` setting (by default — 1024).
2017-12-28 15:13:23 +00:00
2021-04-21 16:25:04 +00:00
All types of sources are supported. When updating, data (from a file or from a table) is read in it entirety.
2017-12-28 15:13:23 +00:00
This method provides the best performance among all available methods of storing the dictionary.
Configuration example:
2020-03-20 10:10:48 +00:00
``` xml
2017-12-28 15:13:23 +00:00
< layout >
2021-04-21 16:25:04 +00:00
< flat >
< initial_array_size > 50000< / initial_array_size >
< max_array_size > 5000000< / max_array_size >
< / flat >
2017-12-28 15:13:23 +00:00
< / layout >
```
2019-11-13 15:50:09 +00:00
or
2020-03-20 10:10:48 +00:00
``` sql
2021-04-21 16:25:04 +00:00
LAYOUT(FLAT(INITIAL_ARRAY_SIZE 50000 MAX_ARRAY_SIZE 5000000))
2019-11-13 15:50:09 +00:00
```
2020-03-22 09:14:59 +00:00
### hashed {#dicts-external_dicts_dict_layout-hashed}
2017-12-28 15:13:23 +00:00
2018-09-06 18:07:25 +00:00
The dictionary is completely stored in memory in the form of a hash table. The dictionary can contain any number of elements with any identifiers In practice, the number of keys can reach tens of millions of items.
2017-12-28 15:13:23 +00:00
2021-05-13 17:05:58 +00:00
If `preallocate` is `true` (default is `false` ) the hash table will be preallocated (this will make the dictionary load faster). But note that you should use it only if:
2021-05-13 17:26:03 +00:00
- The source support an approximate number of elements (for now it is supported only by the `ClickHouse` source).
- There are no duplicates in the data (otherwise it may increase memory usage for the hashtable).
2020-09-24 05:01:00 +00:00
2017-12-28 15:13:23 +00:00
All types of sources are supported. When updating, data (from a file or from a table) is read in its entirety.
Configuration example:
2020-03-20 10:10:48 +00:00
``` xml
2017-12-28 15:13:23 +00:00
< layout >
2021-05-09 09:12:21 +00:00
< hashed >
< preallocate > 0< / preallocate >
< / hashed >
2017-12-28 15:13:23 +00:00
< / layout >
```
2019-11-13 15:50:09 +00:00
or
2020-03-20 10:10:48 +00:00
``` sql
2021-05-09 09:12:21 +00:00
LAYOUT(HASHED(PREALLOCATE 0))
2019-11-13 15:50:09 +00:00
```
2020-10-13 17:23:29 +00:00
### sparse_hashed {#dicts-external_dicts_dict_layout-sparse_hashed}
2019-09-20 23:22:40 +00:00
Similar to `hashed` , but uses less memory in favor more CPU usage.
2021-05-09 09:12:21 +00:00
It will be also preallocated so as `hashed` (with `preallocate` set to `true` ), and note that it is even more significant for `sparse_hashed` .
2020-09-24 05:01:00 +00:00
2019-09-20 23:22:40 +00:00
Configuration example:
2020-03-20 10:10:48 +00:00
``` xml
2019-09-20 23:22:40 +00:00
< layout >
< sparse_hashed / >
< / layout >
```
2021-05-09 09:12:21 +00:00
or
2020-03-20 10:10:48 +00:00
``` sql
2021-05-09 09:12:21 +00:00
LAYOUT(SPARSE_HASHED([PREALLOCATE 0]))
2019-11-13 15:50:09 +00:00
```
2017-12-28 15:13:23 +00:00
2020-10-13 17:23:29 +00:00
### complex_key_hashed {#complex-key-hashed}
2017-12-28 15:13:23 +00:00
2020-06-18 08:24:31 +00:00
This type of storage is for use with composite [keys ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-structure.md ). Similar to `hashed` .
2017-12-28 15:13:23 +00:00
Configuration example:
2020-03-20 10:10:48 +00:00
``` xml
2017-12-28 15:13:23 +00:00
< layout >
< complex_key_hashed / >
< / layout >
```
2021-10-19 18:51:46 +00:00
or
2020-03-20 10:10:48 +00:00
``` sql
2019-11-13 15:50:09 +00:00
LAYOUT(COMPLEX_KEY_HASHED())
```
2017-12-28 15:13:23 +00:00
2021-10-16 10:41:56 +00:00
### complex_key_sparse_hashed {#complex-key-sparse-hashed}
2021-10-19 18:51:46 +00:00
This type of storage is for use with composite [keys ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-structure.md ). Similar to [sparse_hashed ](#dicts-external_dicts_dict_layout-sparse_hashed ).
2021-10-16 10:41:56 +00:00
Configuration example:
``` xml
< layout >
< complex_key_sparse_hashed / >
< / layout >
```
2021-10-19 18:51:46 +00:00
or
2021-10-16 10:41:56 +00:00
``` sql
LAYOUT(COMPLEX_KEY_SPARSE_HASHED())
```
### hashed_array {#dicts-external_dicts_dict_layout-hashed-array}
2021-10-19 18:51:46 +00:00
The dictionary is completely stored in memory. Each attribute is stored in an array. The key attribute is stored in the form of a hashed table where value is an index in the attributes array. The dictionary can contain any number of elements with any identifiers. In practice, the number of keys can reach tens of millions of items.
2021-10-16 10:41:56 +00:00
All types of sources are supported. When updating, data (from a file or from a table) is read in its entirety.
Configuration example:
``` xml
< layout >
< hashed_array >
< / hashed_array >
< / layout >
```
or
``` sql
LAYOUT(HASHED_ARRAY())
```
### complex_key_hashed_array {#complex-key-hashed-array}
2021-10-19 18:51:46 +00:00
This type of storage is for use with composite [keys ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-structure.md ). Similar to [hashed_array ](#dicts-external_dicts_dict_layout-hashed-array ).
2021-10-16 10:41:56 +00:00
Configuration example:
``` xml
< layout >
< complex_key_hashed_array / >
< / layout >
```
2021-10-19 18:51:46 +00:00
or
2021-10-16 10:41:56 +00:00
``` sql
LAYOUT(COMPLEX_KEY_HASHED_ARRAY())
```
2020-10-13 17:23:29 +00:00
### range_hashed {#range-hashed}
2017-12-28 15:13:23 +00:00
The dictionary is stored in memory in the form of a hash table with an ordered array of ranges and their corresponding values.
2019-08-26 01:44:13 +00:00
This storage method works the same way as hashed and allows using date/time (arbitrary numeric type) ranges in addition to the key.
2017-12-28 15:13:23 +00:00
Example: The table contains discounts for each advertiser in the format:
2020-03-20 10:10:48 +00:00
``` text
2020-04-03 13:23:32 +00:00
+---------|-------------|-------------|------+
2018-04-23 06:20:21 +00:00
| advertiser id | discount start date | discount end date | amount |
+===============+=====================+===================+========+
| 123 | 2015-01-01 | 2015-01-15 | 0.15 |
2020-04-03 13:23:32 +00:00
+---------|-------------|-------------|------+
2018-04-23 06:20:21 +00:00
| 123 | 2015-01-16 | 2015-01-31 | 0.25 |
2020-04-03 13:23:32 +00:00
+---------|-------------|-------------|------+
2018-04-23 06:20:21 +00:00
| 456 | 2015-01-01 | 2015-01-15 | 0.05 |
2020-04-03 13:23:32 +00:00
+---------|-------------|-------------|------+
2017-12-28 15:13:23 +00:00
```
2021-09-15 09:23:31 +00:00
To use a sample for date ranges, define the `range_min` and `range_max` elements in the [structure ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-structure.md ). These elements must contain elements `name` and `type` (if `type` is not specified, the default type will be used - Date). `type` can be any numeric type (Date / DateTime / UInt64 / Int32 / others).
!!! warning "Warning"
Values of `range_min` and `range_max` should fit in `Int64` type.
2017-12-28 15:13:23 +00:00
Example:
2020-03-20 10:10:48 +00:00
``` xml
2017-12-28 15:13:23 +00:00
< structure >
< id >
< name > Id< / name >
< / id >
< range_min >
< name > first< / name >
2019-08-26 01:44:13 +00:00
< type > Date< / type >
2017-12-28 15:13:23 +00:00
< / range_min >
< range_max >
< name > last< / name >
2019-08-26 01:44:13 +00:00
< type > Date< / type >
2017-12-28 15:13:23 +00:00
< / range_max >
...
```
2019-11-13 15:50:09 +00:00
or
2020-03-20 10:10:48 +00:00
``` sql
2019-11-13 15:50:09 +00:00
CREATE DICTIONARY somedict (
id UInt64,
first Date,
last Date
)
PRIMARY KEY id
LAYOUT(RANGE_HASHED())
RANGE(MIN first MAX last)
```
2019-08-26 01:44:13 +00:00
To work with these dictionaries, you need to pass an additional argument to the `dictGetT` function, for which a range is selected:
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2018-09-06 18:07:25 +00:00
dictGetT('dict_name', 'attr_name', id, date)
```
2017-12-28 15:13:23 +00:00
This function returns the value for the specified `id` s and the date range that includes the passed date.
Details of the algorithm:
2020-03-21 04:11:51 +00:00
- If the `id` is not found or a range is not found for the `id` , it returns the default value for the dictionary.
2021-02-09 18:47:11 +00:00
- If there are overlapping ranges, it returns value for any (random) range.
- If the range delimiter is `NULL` or an invalid date (such as 1900-01-01), the range is open. The range can be open on both sides.
2017-12-28 15:13:23 +00:00
Configuration example:
2020-03-20 10:10:48 +00:00
``` xml
2021-10-26 05:50:15 +00:00
< clickhouse >
2021-09-15 09:23:31 +00:00
< dictionary >
...
< layout >
< range_hashed / >
< / layout >
< structure >
< id >
< name > Abcdef< / name >
< / id >
< range_min >
< name > StartTimeStamp< / name >
< type > UInt64< / type >
< / range_min >
< range_max >
< name > EndTimeStamp< / name >
< type > UInt64< / type >
< / range_max >
< attribute >
< name > XXXType< / name >
< type > String< / type >
< null_value / >
< / attribute >
< / structure >
< / dictionary >
2021-10-26 05:50:15 +00:00
< / clickhouse >
2017-12-28 15:13:23 +00:00
```
2019-11-13 15:50:09 +00:00
or
2020-03-20 10:10:48 +00:00
``` sql
2019-11-13 15:50:09 +00:00
CREATE DICTIONARY somedict(
Abcdef UInt64,
StartTimeStamp UInt64,
EndTimeStamp UInt64,
XXXType String DEFAULT ''
)
PRIMARY KEY Abcdef
RANGE(MIN StartTimeStamp MAX EndTimeStamp)
```
2017-12-28 15:13:23 +00:00
2021-08-21 19:44:12 +00:00
### complex_key_range_hashed {#complex-key-range-hashed}
The dictionary is stored in memory in the form of a hash table with an ordered array of ranges and their corresponding values (see [range_hashed ](#range-hashed )). This type of storage is for use with composite [keys ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-structure.md ).
2021-08-26 16:47:21 +00:00
Configuration example:
2021-08-21 19:44:12 +00:00
``` sql
CREATE DICTIONARY range_dictionary
(
CountryID UInt64,
CountryKey String,
StartDate Date,
EndDate Date,
Tax Float64 DEFAULT 0.2
)
PRIMARY KEY CountryID, CountryKey
SOURCE(CLICKHOUSE(TABLE 'date_table'))
LIFETIME(MIN 1 MAX 1000)
LAYOUT(COMPLEX_KEY_RANGE_HASHED())
RANGE(MIN StartDate MAX EndDate);
```
2020-03-20 10:10:48 +00:00
### cache {#cache}
2017-12-28 15:13:23 +00:00
The dictionary is stored in a cache that has a fixed number of cells. These cells contain frequently used elements.
2020-03-20 10:10:48 +00:00
When searching for a dictionary, the cache is searched first. For each block of data, all keys that are not found in the cache or are outdated are requested from the source using `SELECT attrs... FROM db.table WHERE id IN (k1, k2, ...)` . The received data is then written to the cache.
2017-12-28 15:13:23 +00:00
2021-08-02 12:18:05 +00:00
If keys are not found in dictionary, then update cache task is created and added into update queue. Update queue properties can be controlled with settings `max_update_queue_size` , `update_queue_push_timeout_milliseconds` , `query_wait_timeout_milliseconds` , `max_threads_for_updates` .
2021-09-07 17:07:27 +00:00
For cache dictionaries, the expiration [lifetime ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-lifetime.md ) of data in the cache can be set. If more time than `lifetime` has passed since loading the data in a cell, the cell’ s value is not used and key becomes expired. The key is re-requested the next time it needs to be used. This behaviour can be configured with setting `allow_read_expired_keys` .
This is the least effective of all the ways to store dictionaries. The speed of the cache depends strongly on correct settings and the usage scenario. A cache type dictionary performs well only when the hit rates are high enough (recommended 99% and higher). You can view the average hit rate in the [system.dictionaries ](../../../operations/system-tables/dictionaries.md ) table.
2017-12-28 15:13:23 +00:00
2021-08-02 21:53:43 +00:00
If setting `allow_read_expired_keys` is set to 1, by default 0. Then dictionary can support asynchronous updates. If a client requests keys and all of them are in cache, but some of them are expired, then dictionary will return expired keys for a client and request them asynchronously from the source.
2021-08-02 12:18:05 +00:00
2020-03-20 10:10:48 +00:00
To improve cache performance, use a subquery with `LIMIT` , and call the function with the dictionary externally.
2017-12-28 15:13:23 +00:00
2021-08-02 21:53:43 +00:00
Supported [sources ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-sources.md ): MySQL, ClickHouse, executable, HTTP.
2017-12-28 15:13:23 +00:00
Example of settings:
2020-03-20 10:10:48 +00:00
``` xml
2017-12-28 15:13:23 +00:00
< layout >
< cache >
2018-03-26 13:16:59 +00:00
<!-- The size of the cache, in number of cells. Rounded up to a power of two. -->
2018-04-23 06:20:21 +00:00
< size_in_cells > 1000000000< / size_in_cells >
2021-08-02 12:18:05 +00:00
<!-- Allows to read expired keys. -->
< allow_read_expired_keys > 0< / allow_read_expired_keys >
<!-- Max size of update queue. -->
< max_update_queue_size > 100000< / max_update_queue_size >
<!-- Max timeout in milliseconds for push update task into queue. -->
< update_queue_push_timeout_milliseconds > 10< / update_queue_push_timeout_milliseconds >
<!-- Max wait timeout in milliseconds for update task to complete. -->
< query_wait_timeout_milliseconds > 60000< / query_wait_timeout_milliseconds >
<!-- Max threads for cache dictionary update. -->
< max_threads_for_updates > 4< / max_threads_for_updates >
2017-12-28 15:13:23 +00:00
< / cache >
< / layout >
```
2019-11-13 15:50:09 +00:00
or
2020-03-20 10:10:48 +00:00
``` sql
2019-11-13 15:50:09 +00:00
LAYOUT(CACHE(SIZE_IN_CELLS 1000000000))
```
2017-12-28 15:13:23 +00:00
Set a large enough cache size. You need to experiment to select the number of cells:
2020-03-20 10:10:48 +00:00
1. Set some value.
2. Run queries until the cache is completely full.
3. Assess memory consumption using the `system.dictionaries` table.
4. Increase or decrease the number of cells until the required memory consumption is reached.
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
!!! warning "Warning"
2018-09-06 18:07:25 +00:00
Do not use ClickHouse as a source, because it is slow to process queries with random reads.
2017-12-28 15:13:23 +00:00
2020-10-13 17:23:29 +00:00
### complex_key_cache {#complex-key-cache}
2017-12-28 15:13:23 +00:00
2020-06-18 08:24:31 +00:00
This type of storage is for use with composite [keys ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-structure.md ). Similar to `cache` .
2018-03-25 03:08:08 +00:00
2020-10-13 17:23:29 +00:00
### ssd_cache {#ssd-cache}
2020-05-23 10:41:27 +00:00
2021-08-02 12:18:05 +00:00
Similar to `cache` , but stores data on SSD and index in RAM. All cache dictionary settings related to update queue can also be applied to SSD cache dictionaries.
2020-05-23 10:41:27 +00:00
``` xml
< layout >
< ssd_cache >
<!-- Size of elementary read block in bytes. Recommended to be equal to SSD's page size. -->
< block_size > 4096< / block_size >
<!-- Max cache file size in bytes. -->
< file_size > 16777216< / file_size >
<!-- Size of RAM buffer in bytes for reading elements from SSD. -->
< read_buffer_size > 131072< / read_buffer_size >
<!-- Size of RAM buffer in bytes for aggregating elements before flushing to SSD. -->
< write_buffer_size > 1048576< / write_buffer_size >
<!-- Path where cache file will be stored. -->
2021-10-17 19:21:21 +00:00
< path > /var/lib/clickhouse/user_files/test_dict< / path >
2020-05-23 10:41:27 +00:00
< / ssd_cache >
< / layout >
```
or
``` sql
2021-03-18 09:55:17 +00:00
LAYOUT(SSD_CACHE(BLOCK_SIZE 4096 FILE_SIZE 16777216 READ_BUFFER_SIZE 1048576
2021-10-17 19:21:21 +00:00
PATH '/var/lib/clickhouse/user_files/test_dict'))
2020-05-23 10:41:27 +00:00
```
2020-10-13 17:23:29 +00:00
### complex_key_ssd_cache {#complex-key-ssd-cache}
2020-05-23 10:41:27 +00:00
2020-10-13 17:23:29 +00:00
This type of storage is for use with composite [keys ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-structure.md ). Similar to `ssd_cache` .
2020-05-23 10:41:27 +00:00
2020-05-03 12:55:22 +00:00
### direct {#direct}
The dictionary is not stored in memory and directly goes to the source during the processing of a request.
The dictionary key has the `UInt64` type.
2020-06-18 08:24:31 +00:00
All types of [sources ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-sources.md ), except local files, are supported.
2020-05-03 12:55:22 +00:00
Configuration example:
``` xml
< layout >
< direct / >
< / layout >
```
or
``` sql
LAYOUT(DIRECT())
```
2020-10-13 17:23:29 +00:00
### complex_key_direct {#complex-key-direct}
2020-05-13 16:05:05 +00:00
2020-06-18 08:24:31 +00:00
This type of storage is for use with composite [keys ](../../../sql-reference/dictionaries/external-dictionaries/external-dicts-dict-structure.md ). Similar to `direct` .
2020-05-13 16:05:05 +00:00
2020-10-13 17:23:29 +00:00
### ip_trie {#ip-trie}
2018-03-25 03:08:08 +00:00
2018-04-23 06:20:21 +00:00
This type of storage is for mapping network prefixes (IP addresses) to metadata such as ASN.
2018-03-25 03:08:08 +00:00
2018-04-23 06:20:21 +00:00
Example: The table contains network prefixes and their corresponding AS number and country code:
2018-03-25 03:08:08 +00:00
2020-03-20 10:10:48 +00:00
``` text
2020-04-03 13:23:32 +00:00
+-----------|-----|------+
2018-03-25 03:08:08 +00:00
| prefix | asn | cca2 |
+=================+=======+========+
| 202.79.32.0/20 | 17501 | NP |
2020-04-03 13:23:32 +00:00
+-----------|-----|------+
2018-03-25 03:08:08 +00:00
| 2620:0:870::/48 | 3856 | US |
2020-04-03 13:23:32 +00:00
+-----------|-----|------+
2018-03-25 03:08:08 +00:00
| 2a02:6b8:1::/48 | 13238 | RU |
2020-04-03 13:23:32 +00:00
+-----------|-----|------+
2018-03-25 03:08:08 +00:00
| 2001:db8::/32 | 65536 | ZZ |
2020-04-03 13:23:32 +00:00
+-----------|-----|------+
2018-03-25 03:08:08 +00:00
```
2018-04-23 06:20:21 +00:00
When using this type of layout, the structure must have a composite key.
2018-03-25 03:08:08 +00:00
Example:
2020-03-20 10:10:48 +00:00
``` xml
2018-03-25 03:08:08 +00:00
< structure >
< key >
< attribute >
< name > prefix< / name >
< type > String< / type >
< / attribute >
< / key >
< attribute >
< name > asn< / name >
< type > UInt32< / type >
< null_value / >
< / attribute >
< attribute >
< name > cca2< / name >
< type > String< / type >
< null_value > ??< / null_value >
< / attribute >
...
2020-12-24 15:46:45 +00:00
< / structure >
< layout >
< ip_trie >
<!-- Key attribute `prefix` can be retrieved via dictGetString. -->
<!-- This option increases memory usage. -->
< access_to_key_from_attributes > true< / access_to_key_from_attributes >
< / ip_trie >
< / layout >
2018-03-25 03:08:08 +00:00
```
2019-11-13 15:50:09 +00:00
or
2020-03-20 10:10:48 +00:00
``` sql
2019-11-13 15:50:09 +00:00
CREATE DICTIONARY somedict (
prefix String,
asn UInt32,
cca2 String DEFAULT '??'
)
PRIMARY KEY prefix
```
2018-09-06 18:07:25 +00:00
The key must have only one String type attribute that contains an allowed IP prefix. Other types are not supported yet.
2018-03-25 03:08:08 +00:00
2018-04-23 06:20:21 +00:00
For queries, you must use the same functions (`dictGetT` with a tuple) as for dictionaries with composite keys:
2018-03-25 03:08:08 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2018-04-23 06:20:21 +00:00
dictGetT('dict_name', 'attr_name', tuple(ip))
```
2018-03-25 03:08:08 +00:00
2018-09-06 18:07:25 +00:00
The function takes either `UInt32` for IPv4, or `FixedString(16)` for IPv6:
2018-03-25 03:08:08 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2018-04-23 06:20:21 +00:00
dictGetString('prefix', 'asn', tuple(IPv6StringToNum('2001:db8::1')))
```
2018-03-25 03:08:08 +00:00
2018-04-23 06:20:21 +00:00
Other types are not supported yet. The function returns the attribute for the prefix that corresponds to this IP address. If there are overlapping prefixes, the most specific one is returned.
2018-03-25 03:08:08 +00:00
2020-12-24 15:46:45 +00:00
Data must completely fit into RAM.