본문 바로가기

IT

[TimescaleDB] hyper table에 indexing을 걸면?

Intro

환경설정

docker run -d --name timescaledb -p 5432:5432 -e POSTGRES_PASSWORD=password timescale/timescaledb:latest-pg15
create table realtime_datas (
	id serial,
	event_time timestamptz not null,
	data integer not null
);
DO $$
DECLARE
    _current timestamptz;
    random_interval INTERVAL;
    random_value INT;
    start_date timestamptz := '2024-01-01';
    end_date timestamptz := '2024-07-30';
BEGIN
    _current := start_date;

    WHILE _current <= end_date + INTERVAL '1 day' LOOP
        
        random_interval := (1 + floor(random() * 3))::INT * INTERVAL '1 minute';
        random_value := 10 + floor(random() * 11)::INT;

        INSERT INTO realtime_datas(event_time, data)
        VALUES (_current, random_value);

        _current := _current + random_interval;
    END LOOP;
END;
$$ LANGUAGE plpgsql;
SELECT create_hypertable('realtime_datas', 'event_time', migrate_data=>true);

Contents

hyper table을 설정하면, 기본적으로 설정한 column에 대해서 desc 정렬로 index가 자동 생성된다.

 

_timescaledb_internal에 생성되는 chunk table의 index를 확인해보면, event_time column에 대해서 desc 정렬 index가 자동으로 걸렸다는 것을 확인할 수 있다.

 

 

그럼, 만약 이 hyper table에 내가 별도로 index를 걸면 어떻게 되는가?

 

realtime_datas table의 id 부분에 index를 걸어보겠다.

 

create index on realtime_datas (id);

 

 

보이는 바와 같이, 원본 테이블에 indexing이 걸리면서, 모든 chunk table에 동일한 index가 걸리는 것을 확인 할 수 있다.

 

원본 hyper table
chunk table 중 1

 

그래서 timescaleDB의 hyper table은 다음과 같은 조회 순서를 가진다.

 

1. 어떤 chunk table를 조회해야하는지 선정

2. 해당 chunk들에 postgresql이 어떤 스캔을 할지 선정(index, bitmap, seq)