In postgres, what is the difference in efficiency between with and manual create table to create temporary tables and drop table after use?

in a stored procedure:

    Will
  1. with create temporary tables? Where is it stored?
  2. if with also creates temporary tables, how is the efficiency different from manual create table creating temporary tables and drop table after use?
Jul.01,2022

refer to https://www.postgresql.org/do...

WITH provides a way to write auxiliary statements for use in a larger query. These statements, which are often referred to as Common Table Expressions or CTEs, can be thought of as defining temporary tables that exist just for one query.
WITH provides a way to write auxiliary statements for use in larger queries. These statements, often referred to as common table expressions or CTE, can be seen as defining temporary tables that exist only for one query.

personally understand that CTE is also a temporary table (implemented by postgre itself), which is more efficient than creating a temporary table yourself, but only valid in one query. In the storage process of multiple queries, using one temporary table must be more efficient than using multiple with.


when the drop table temporary table adds an exclusive lock to the metadata table, it will have some impact on the performance of the database.
it is recommended that if the logic is fixed, only create the intermediate table for the first time, each time the truncate data can be

Menu