Dictionary 테이블 엔진

Dictionary 테이블 엔진

Dictionary 엔진은 사전(dictionary) 데이터를 ClickHouse 테이블 형태로 보여줘요. 사전의 원시 데이터가 필요하거나 JOIN을 수행해야 할 때 유용하답니다. dictGet* 함수를 쓰지 않고도 사전 내용을 테이블처럼 조회할 수 있어요.

출처: 문서

본문

Dictionary 엔진은 사전 데이터를 ClickHouse 테이블로 표시합니다.

예시 (Example)

예를 들어 다음과 같은 설정을 가진 제품(products) 사전을 생각해 볼게요:

<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>

사전 데이터를 조회해 볼게요:

SELECT
    name,
    type,
    key,
    attribute.names,
    attribute.types,
    bytes_allocated,
    element_count,
    source
FROM system.dictionaries
WHERE name = 'products'
┌─name─────┬─type─┬─key────┬─attribute.names─┬─attribute.types─┬─bytes_allocated─┬─element_count─┬─source──────────┐
│ products │ Flat │ UInt64 │ ['title']       │ ['String']      │        23065376 │        175032 │ ODBC: .products │
└──────────┴──────┴────────┴─────────────────┴─────────────────┴─────────────────┴───────────────┴─────────────────┘

이 형식으로 사전 데이터를 얻으려면 dictGet* 함수를 사용할 수 있어요. 하지만 원시 데이터를 가져와야 하거나 JOIN 연산을 수행할 때는 이 뷰가 도움이 되지 않아요. 그런 경우에는 사전 데이터를 테이블로 표시하는 Dictionary 엔진을 사용할 수 있습니다.

구문:

CREATE TABLE %table_name% (%fields%) engine = Dictionary(%dictionary_name%)`

사용 예시:

CREATE TABLE products (product_id UInt64, title String) ENGINE = Dictionary(products);

정상적으로 생성됐어요. 이제 테이블에 무엇이 있는지 살펴볼게요.

SELECT * FROM products LIMIT 1;
┌────product_id─┬─title───────────┐
│        152689 │ Some item       │
└───────────────┴─────────────────┘

더 알아보기 (Learn more)