O'Reilly logo

PostgreSQL High Performance Cookbook by Dinesh Kumar, Chitij Chauhan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required