当前位置:网站首页>[SQL optimization] the difference between with as and temporary tables

[SQL optimization] the difference between with as and temporary tables

2022-07-01 19:36:00 Tanyue Jianzhi Dachang

with as and The difference between temporary tables

1.with as

with as Just treat the subquery statement as a table , But the real data is not inserted into the database , Its advantage is to increase the readability and maintenance of the code .

with tmp_01 as (
    select *
    from table)
select *
from tmp_01;

2. A temporary table

The data of the temporary table needs to be inserted into the database , So it is generally used for making statements , After inserting data, you need to delete the temporary table .

drop table if exists tmp_01;
create table if not exists tmp_01 as
select *
from table;
select *
from tmp_01;

3. Operational efficiency

use with as , In fact, there is no difference in efficiency with direct sub query ;

The temporary table is similar to the permanent table , The data is really running into the database , Equivalent to the second direct correlation is a small table , Query efficiency is greatly improved .

4. Application scenarios

Temporary table applies to : There are many complex associated sub table queries .

with as Apply to : To increase code readability , And there are not many complex associated sub queries .

原网站

版权声明
本文为[Tanyue Jianzhi Dachang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011756176921.html