简体   繁体   中英

view over cte or temp table?

I have a table in staging layer(not indexed) which has close to 100 million rows. In the data warehouse layer, I need to select a certain number of rows from this table and join with another table, roughly having around 50 million rows, for which I use a cte now. From this cte, again some aggregations are carried out before joining with some other tables. So here, what will happen if use a view instead of the cte. I cannot test run it since it takes a lot of time.

So in a general aspect, which holds a slight advantage in terms of performance?

cte or temp table or view ?

Any help is appreciated.

I think you should go with local (single #) temporary table with indexes. Because first you will fetch the data from main table. Then you will apply some aggregations, looping and custom logic. There will be few benefits :-

  1. First whenever connection will close then local temporary table will dropped.
  2. As you are saying you will get millions of records, then using index it will search faster.
  3. Using temporary table and applying some aggregations will not put load on your main tables.

From what you describe the CTE is "created" once (when defined) and used once (when aggregated).

In general, this means that you should keep the code as a single query, letting the optimizer find the best execution path.

In general, materializing CTEs is going to be a bigger win when the CTE is referenced multiple times. Often, you can get around multiple references using window functions, but that is a different matter.

That is general advice, but not always true. Materializing a CTE as a temporary table can give two benefits:

  • The query optimizer has a more accurate estimate of the number of rows for optimization.
  • You can add indexes to boost performance.

The first is possibly not an issue, because you still have a large percentage of the original rows. The second could possibly help, but it is not a no-brainer.

You might want to create an indexed materialized view instead of a temporary table. This would stay up-to-date and possibly be a big boost to performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM