Query optimizations
Real-time analytics isn’t just about raw speed—it’s about executing queries efficiently, reducing unnecessary work, and maximizing performance. TimescaleDB optimizes every step of the query lifecycle to ensure that queries scan only what’s necessary, make use of data locality, and execute in parallel for sub-second response times over large datasets.
Skip unnecessary data
TimescaleDB minimizes the amount of data a query touches, reducing I/O and improving execution speed:
Primary partition exclusion (row and columnar)
Queries automatically skip irrelevant partitions (chunks) based on the primary partitioning key (usually a timestamp), ensuring they only scan relevant data.
Secondary partition exclusion (columnar)
Min/max metadata allows queries filtering on correlated dimensions (e.g., order_id or secondary timestamps) to exclude chunks that don’t contain relevant data.
Postgres indexes (row and columnar)
Unlike many databases, TimescaleDB supports sparse indexes on columnstore data, allowing queries to efficiently locate specific values within both row-based and compressed columnar storage. These indexes enable fast lookups, range queries, and filtering operations that further reduce unnecessary data scans.
Batch-level filtering (columnar)
Within each chunk, compressed columnar batches are organized using SEGMENTBY keys and ordered by ORDERBY columns. Indexes and min/max metadata can be used to quickly exclude batches that don’t match the query criteria.
Maximize locality
Organizing data for efficient access ensures queries are read in the most optimal order, reducing unnecessary random reads and reducing scans of unneeded data.
- Segmentation: Columnar batches are grouped using
SEGMENTBYto keep related data together, improving scan efficiency. - Ordering: Data within each batch is physically sorted using
ORDERBY, increasing scan efficiency (and reducing I/O operations), enabling efficient range queries, and minimizing post-query sorting. - Column selection: Queries read only the necessary columns, reducing disk I/O, decompression overhead, and memory usage.
Parallelize execution
Once a query is scanning only the required columnar data in the optimal order, TimescaleDB is able to maximize performance through parallel execution. As well as using multiple workers, TimescaleDB accelerates columnstore query execution by using Single Instruction, Multiple Data (SIMD) vectorization, allowing modern CPUs to process multiple data points in parallel.
The TimescaleDB implementation of SIMD vectorization currently allows:
- Vectorized decompression, which efficiently restores compressed data into a usable form for analysis.
- Vectorized filtering, which rapidly applies filter conditions across data sets.
- Vectorized aggregation, which performs aggregate calculations, such as sum or average, across multiple data points concurrently.