Back
Apr 23, 2021

ClickHouse tips #6: Filtering data in subqueries to avoid joins

Sometimes you can replace joins on ClickHouse using where clauses, having the same performance as with Join engines. Learn how here.
Xoel López
Founder at TheirStack

Imagine that you want to join two tables, and filter by a column that comes from the table in the right side of the join. On ClickHouse the query a bit different than what you’d do in other databases like Postgres, and it will result in a big performance improvement.

Let’s say one of the tables is this events table, with 100M rows:

And the other table is this products one, with ~2M rows

Start building with Tinybird
If you've read this far, you might want to use Tinybird as your analytics backend. Start for free with no time limit.
Subscribe to our newsletter
Links to our blog and other great reads sent every other Saturday.

If you will always filter the result after making the join as in the query above, you don’t need to make a join at all. ClickHouse saves data column-by-column, so filtering by the values in a column is a very fast operation. If you rewrite the query as follows, it would be just as fast. And you wouldn’t have create a Join table for it:

Tinybird lets you create real-time API endpoints on in minutes instead of hours of days, powered by ClickHouse. We’re still in private beta, but if you want to try out product, create an account here.

Do you like this post?

Related posts

ClickHouse tips #3: the transform function
Tinybird vs. ClickHouse®️: What's the difference?
Tinybird vs. ClickHouse®️: What's the difference?
Want a managed ClickHouse®️? Here are some options
Want a managed ClickHouse®️? Here are some options
Changelog: Snowflake connector, cancellable jobs and more

Tinybird

Team

Mar 23, 2021
We launched an open source ClickHouse Knowledge Base

Tinybird

Team

Oct 11, 2022
A big performance boost, adding columns and more

Tinybird

Team

May 31, 2021
Changelog #18: High-frequency ingestion, handling NDJSON files and more product enhancements

Tinybird

Team

Feb 01, 2022
ClickHouse tips #11: Best way to get query types

Tinybird

Team

Sep 22, 2021

Build fast data products, faster.

Try Tinybird and bring your data sources together and enable engineers to build with data in minutes. No credit card required, free to get started.
Tinybird is not affiliated with, associated with, or sponsored by ClickHouse, Inc. ClickHouse® is a registered trademark of ClickHouse, Inc.