QuestDB
QuestDB Time-Series Database
An open-source database specialized in ultra-fast time-series data processing. Supports the InfluxDB protocol and the PostgreSQL wire protocol, and can be queried with familiar SQL.
QuestDB Time-Series Database
An open-source database specialized in ultra-fast time-series data processing. Supports the InfluxDB protocol and the PostgreSQL wire protocol, and can be queried with familiar SQL.
QuestDB is a high-performance time-series database written in Java. Its core strength is speed. Using column-oriented storage, SIMD vectorized operations, and memory-mapped files, it can process millions of rows per second. In benchmarks it often outperforms InfluxDB and TimescaleDB.
It features a developer-friendly interface. It supports the PostgreSQL wire protocol, so existing tools such as pgAdmin and DBeaver can be used as they are, and time-series-specific functions (SAMPLE BY, LATEST ON, etc.) are added on top of standard SQL. It also supports the InfluxDB Line Protocol (ILP), so existing InfluxDB client code can be migrated without modification.
Main use cases are IoT sensor data, financial tick data, and infrastructure monitoring metrics. It excels especially in environments that need both high-frequency data ingestion (millisecond timestamps) and real-time dashboard queries at the same time. Integration with Grafana is good, so visualization is easy too.
It is open source under the Apache 2.0 license, and QuestDB Cloud is also offered. It delivers very high performance on a single node, but distributed clustering is provided as an enterprise feature. Table partitioning and automatic data retention policies manage large time-series data efficiently.
# QuestDB ์๊ณ์ด ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์์
import questdb.ingress as qdb
import psycopg2
from datetime import datetime, timedelta
import random
import time
# ==========================================
# 1. ILP (InfluxDB Line Protocol)๋ก ๋ฐ์ดํฐ ์์ง
# ==========================================
def ingest_sensor_data():
"""๊ณ ์ ๋ฐ์ดํฐ ์์ง (ILP over TCP)"""
with qdb.Sender('localhost', 9009) as sender:
# ๋ฐฐ์น๋ก ์ผ์ ๋ฐ์ดํฐ ์ ์ก
for i in range(100000):
sender.row(
'sensors',
symbols={'device_id': f'device_{i % 100}', 'location': 'seoul'},
columns={
'temperature': 20.0 + random.random() * 10,
'humidity': 40.0 + random.random() * 30,
'pressure': 1013.0 + random.random() * 20
},
at=datetime.utcnow()
)
# 1000ํ๋ง๋ค flush (์ฑ๋ฅ ์ต์ ํ)
if i % 1000 == 0:
sender.flush()
print("100,000 rows ingested")
# ==========================================
# 2. PostgreSQL ํ๋กํ ์ฝ๋ก ์ฟผ๋ฆฌ
# ==========================================
def query_with_psycopg2():
"""PostgreSQL ํด๋ผ์ด์ธํธ๋ก SQL ์ฟผ๋ฆฌ"""
conn = psycopg2.connect(
host='localhost',
port=8812,
user='admin',
password='quest',
database='qdb'
)
with conn.cursor() as cur:
# ํ
์ด๋ธ ์์ฑ (์๊ณ์ด ์ต์ ํ)
cur.execute("""
CREATE TABLE IF NOT EXISTS trades (
timestamp TIMESTAMP,
symbol SYMBOL,
price DOUBLE,
volume LONG
) timestamp(timestamp) PARTITION BY DAY WAL;
""")
# ์ต๊ทผ 1์๊ฐ ๋ฐ์ดํฐ ์กฐํ
cur.execute("""
SELECT * FROM trades
WHERE timestamp > dateadd('h', -1, now())
ORDER BY timestamp DESC
LIMIT 100;
""")
rows = cur.fetchall()
# SAMPLE BY๋ก ์๊ฐ ์ง๊ณ (์๊ณ์ด ํนํ ๊ธฐ๋ฅ)
cur.execute("""
SELECT
timestamp,
symbol,
avg(price) as avg_price,
sum(volume) as total_volume,
first(price) as open,
last(price) as close,
max(price) as high,
min(price) as low
FROM trades
WHERE timestamp > dateadd('d', -1, now())
SAMPLE BY 1h
ALIGN TO CALENDAR;
""")
ohlc_data = cur.fetchall()
print(f"OHLC candles: {len(ohlc_data)}")
# LATEST ON์ผ๋ก ๊ฐ ์ฌ๋ณผ์ ์ต์ ๋ฐ์ดํฐ ์กฐํ
cur.execute("""
SELECT * FROM trades
LATEST ON timestamp PARTITION BY symbol;
""")
latest = cur.fetchall()
print(f"Latest per symbol: {latest}")
conn.close()
# ==========================================
# 3. REST API๋ก ๋ฐ์ดํฐ ์กฐํ
# ==========================================
import requests
def query_via_rest():
"""HTTP REST API๋ก ์ฟผ๋ฆฌ"""
query = """
SELECT
timestamp,
device_id,
avg(temperature) as avg_temp
FROM sensors
WHERE timestamp > dateadd('h', -6, now())
SAMPLE BY 15m
"""
response = requests.get(
'http://localhost:9000/exec',
params={'query': query}
)
if response.status_code == 200:
data = response.json()
print(f"Columns: {data['columns']}")
print(f"Row count: {data['count']}")
for row in data['dataset'][:5]:
print(row)
# ==========================================
# 4. ๋ฐ์ดํฐ ๋ณด์กด ์ ์ฑ
์ค์
# ==========================================
def setup_retention_policy():
"""์ค๋๋ ๋ฐ์ดํฐ ์๋ ์ญ์ ์ค์ """
conn = psycopg2.connect(host='localhost', port=8812, user='admin', password='quest')
with conn.cursor() as cur:
# ํํฐ์
์ญ์ (30์ผ ์ด์ ๋ฐ์ดํฐ)
cur.execute("""
ALTER TABLE sensors DROP PARTITION
WHERE timestamp < dateadd('d', -30, now());
""")
# ํ
์ด๋ธ ์ค์ ๋ณ๊ฒฝ
cur.execute("""
ALTER TABLE sensors SET PARAM maxUncommittedRows = 100000;
""")
conn.close()
# ==========================================
# 5. Grafana ๋์๋ณด๋์ฉ ์ฟผ๋ฆฌ
# ==========================================
def grafana_compatible_queries():
"""Grafana์์ ์ฌ์ฉํ ์๊ณ์ด ์ฟผ๋ฆฌ ์์"""
# ์๊ฐ ๋ฒ์ ๋ณ์๋ฅผ ์ฌ์ฉํ ์ฟผ๋ฆฌ (Grafana์์ $__timeFilter ๋์ฒด)
grafana_query = """
SELECT
timestamp as time,
device_id,
temperature,
humidity
FROM sensors
WHERE timestamp BETWEEN '2024-01-01' AND '2024-01-02'
SAMPLE BY $__interval
"""
# ํํธ๋งต์ฉ ์ฟผ๋ฆฌ
heatmap_query = """
SELECT
timestamp as time,
floor(temperature) as temp_bucket,
count() as count
FROM sensors
WHERE timestamp > dateadd('d', -7, now())
SAMPLE BY 1h
"""
# ์๋ฆผ์ฉ ์ง๊ณ ์ฟผ๋ฆฌ
alert_query = """
SELECT
device_id,
avg(temperature) as avg_temp,
max(temperature) as max_temp
FROM sensors
WHERE timestamp > dateadd('m', -5, now())
GROUP BY device_id
HAVING avg(temperature) > 30
"""
return grafana_query, heatmap_query, alert_query
# ์คํ
if __name__ == "__main__":
ingest_sensor_data()
query_with_psycopg2()
query_via_rest()
Data engineer: "Our IoT sensor data is 100,000 records per second, and InfluxDB can't keep up."
Developer: "Take a look at QuestDB. It's compatible with the ILP protocol, so you barely have to change client code, and it can process much faster on the same hardware."
Data engineer: "We can use SQL, right? My teammates couldn't get used to InfluxQL..."
Developer: "Yes, it's standard SQL with just time-series functions like SAMPLE BY added. You use the PostgreSQL driver as is."
Interviewer: "What's the difference between QuestDB and TimescaleDB?"
Candidate: "TimescaleDB is a PostgreSQL extension, so you can use all PostgreSQL features (joins, transactions, etc.), but QuestDB is designed purely for time series and is faster for pure time-series workloads. QuestDB is append-only, so UPDATE/DELETE are limited, but its ingestion performance is overwhelming."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Reviewer: "Increase the batch size when sending data via ILP. If you flush on every row, you lose all the performance."
Author: "Oh, I'll change it to flush every 1000 rows and add the auto_flush_rows setting too."