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…
- 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 WHERE i_price BETWEEN 10 AND 100 ) SELECT * FROM costly_products; QUERY PLAN -------------------------------------------------------------- -----------...