Skip to content

Commit

Permalink
added to cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
atovpeko committed Dec 13, 2024
1 parent 5dd7ed7 commit 605e472
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions _partials/_cookbook-hypertables.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,34 @@ FROM deduplicate_chunks('nudge_events', 'bot_id, session_id, nudge_id, time', 25

Shoutout to **Mathias Ose** and **Christopher Piggott** for this recipe.

### Get faster JOIN queries with Common Table Expressions

Imagine there is a query that joins a hypertable to another table on a shared key:

```sql
SELECT timestamp,
FROM hypertable as h
JOIN related_table as rt
ON rt.id = h.related_table_id
WHERE h.timestamp BETWEEN '2024-10-10 00:00:00' AND '2024-10-17 00:00:00'
```

If you run `EXPLAIN` on this query, you see that the query planner performs a `NestedJoin` between these two tables, which means querying the hypertable multiple times. Even if the hypertable is well indexed, if it is also large, the query will be slow. How do you force a once-only lookup? Use materialized Common Table Expressions (CTEs).

If you split the query into two parts using CTEs, you can `materialize` the hypertable lookup and force PostgreSQL to perform it only once.

```sql
WITH cached_query AS materialized (
SELECT *
FROM hypertable
WHERE BETWEEN '2024-10-10 00:00:00' AND '2024-10-17 00:00:00'
)
SELECT *
FROM cached_query as c
JOIN related_table as rt
ON rt.id = h.related_table_id
```

Now if you run `EXPLAIN` once again, you see that this query performs only one lookup. Depending on the size of your hypertable, this could result in a multi-hour query taking mere seconds.

Shoutout to **Rowan Molony** for this recipe.

0 comments on commit 605e472

Please sign in to comment.