Why is Clickhouse slower than PostgreSQL?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
ClickHouse and PostgreSQL are two popular database management systems that excel at different types of tasks. ClickHouse is designed as a columnar database specifically tailored for Online Analytical Processing (OLAP) and is known for blazing-fast query performance on structured, time-series, and analytical data. PostgreSQL, on the other hand, is a staple in the world of Online Transaction Processing (OLTP), offering a robust, versatile SQL interface with strong support for transactions and complex queries.
Despite these distinct strengths, there are scenarios where ClickHouse might underperform compared to PostgreSQL. While not common, understanding these cases provides valuable insights into each system's architecture and use cases.
Architectural Differences
Storage Models
ClickHouse:
- ClickHouse employs a columnar storage model, designed for efficiently processing large volumes of data for analytical queries. Columns are stored together on disk, allowing for significant speedups when querying large datasets with certain types of aggregation or filtering.
PostgreSQL:
- PostgreSQL uses a row-based storage model, organizing all of a row’s data together. This structure is beneficial for OLTP workloads, which involve a high number of short online transactions.
Indexing and Data Access
ClickHouse:
- ClickHouse's primary focus is on disk throughput and minimizing I/O for query execution. It uses sparse primary indexes, which means it can access data much faster for certain types of queries that involve scanning.
PostgreSQL:
- PostgreSQL offers a more traditional B-tree or hash indexing, which can be more efficient for point queries and range scans. Its index types and optimization might be more suitable for transactions involving quick lookups or small updates.
Reasons for ClickHouse Being Slower
- OLTP Workloads:
- ClickHouse is not optimized for OLTP-like transactions, which are the bread and butter for PostgreSQL. Frequent updates, transactional integrity, and consistency checks increase overhead in ClickHouse, as it lacks true ACID (Atomicity, Consistency, Isolation, Durability) transactional support.
- Complex Join Operations:
- While ClickHouse does support joins, the complexity and execution time of these operations can escalate, especially when dealing with non-trivial joins between large tables. PostgreSQL, with its mature query planner and optimizer, may handle these use cases more efficiently.
- Concurrent Read/Write Access:
- PostgreSQL is adept at managing multiple concurrent operations, thanks in part to its MVCC (Multi-Version Concurrency Control) system. ClickHouse concurrency management might not be as robust in scenarios involving heavy concurrent inserts alongside reads.
- High Throughput of Individual Queries:
- In scenarios where a single, small query is processed, PostgreSQL may be faster due to its ability to quickly handle small, random-access transactional queries without the need for wide-scale data scanning optimizations.
- Data Model Complexity:
- PostgreSQL supports complex data types and constraints, such as JSONB, GIS applications, and full-text search. Handling such data types efficiently can be a challenge for ClickHouse or might require more computational resources.
Technical Example
Consider a scenario where the workload includes numerous small transactions with high update rates:
- In PostgreSQL: The row-based and transactional nature allows small, frequent updates and quick lookups through efficient indexing strategies.
- In ClickHouse: An append-only model can add latency to update operations, as updates must be handled in a less straightforward manner than PostgreSQL's in-place updates.
Performance Comparisons
In a real-world example involving lots of transactional data:
| Feature/Scenario | ClickHouse | PostgreSQL |
| OLTP Transaction Rate | Slower: High overhead for small updates | Faster: Optimized for quick transactions |
| Complex Joins | Slower: Joins on large datasets | Faster: Efficient query planner |
| Single Query Throughput | Slower: High single-query overhead | Faster: Handles lightweight queries well |
| Concurrent Operations | Moderate: Some inconsistencies | Faster: Consistent concurrency control |
| Data Model Complexity | Slower: Basic constraint checks and data types | Faster: Advanced data types and constraints |
Conclusion
The difference in performance between ClickHouse and PostgreSQL comes largely down to their architecture and design goals. While ClickHouse shines brightly in scenarios requiring fast analytical processing of large datasets, PostgreSQL remains superior for traditional transactional workloads due to its well-rounded and transaction-oriented architecture. Recognizing these differences allows developers and database administrators to choose the right tool for their specific needs, enhancing performance and efficiency.

