Running CTE scan

In this recipe, we will be discussing CTE (Common Table Expression) scans.

Getting ready

CTEs are the named inline queries, which we define using the WITH clause, and they can be used multiple times in the same query. We can also implement recursive/DML queries using CTEs. CTE improves the query readability when compared with inline sub queries. CTE also provide the RECURSIVE option, which takes the usage of CTE to another level. Using recursive CTE statements, we can build hierarchical SQL queries where the SQL execution refers to its own results for further processing.

How to do it…

  1. Let's get all the costly products from the sample dataset as follows:
    benchmarksql=# EXPLAIN WITH costly_products AS (
     SELECT i_name FROM bmsql_item ...

Get PostgreSQL High Performance Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.