'CREATE TABLE AS takes too long in clickhouse

I am trying to create a table in Clickhouse(v22.4.3) based on the result that returns a query. The query returns only 1 record and takes around 12 seconds to be executed. When I put this specific query in a CREATE TABLE AS statement like the following one, around 200 seconds are needed for the table to be created.

create table mydb.test_table engine = MergeTree() order by a
  as (
    with
    cte1 as (..),
    cte2 as (..),
    ...
    final_cte as ()
    
    select a
    from final_cte
    )

Does anyone know why this CREATE TABLE AS statement takes that much time despite that the SELECT statement is quick?

P.S. I have around 10 CTEs before the final SELECT statement. I don't know if this plays any role and makes the creation of the table slower



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source