【问题标题】:Does limiting the number of columns in Snowflake CTE improve the performance of a VIEW限制 Snowflake CTE 中的列数是否会提高 VIEW 的性能
【发布时间】:2022-06-14 21:42:48
【问题描述】:

我有一个 Snowflake VIEW 定义如下:

create order_amount_by_order_type_view as (
with temp_table as (
select * FROM orders inner join order_lines on orders.order_no=order_lines.order_no)
select order_type, sum(amount)
from temp_table
group by orders.order_type
)

请注意,我选择了 CTE 中的所有字段,即使主查询中不需要它们。

问题:Snowflake 是否足够聪明,能够识别出主要使用 order_typeamount 并相应地优化 CTE?

或者我应该手动限制 CTE 中的必填字段吗?

create order_amount_by_order_type_view as (
with temp_table as (
select orders.order_type, order_lines.amount FROM orders inner join order_lines on orders.order_no=order_lines.order_no)
select order_type, sum(amount)
from temp_table
group by orders.order_type
)

【问题讨论】:

    标签: snowflake-cloud-data-platform


    【解决方案1】:

    应该足够聪明,测试将是:

    select * from table_name 看看读取了多少字节,然后 select col1,col2 from table_name 看看读取了多少字节。

    然后在查询中查看从 CTE 表中读取的内容,看看它是否更小。

    【讨论】:

      【解决方案2】:

      您的问题的简短回答是:不。但雪花可能会在某个时候引入这一点。

      更有趣的问题是为什么查询几列会更快?

      Snowflake 是一种列式数据库,数据物理存储在其中,以便快速访问特定列。

      如果您查看 Oracle 或 SQL Server,您会发现数据是按行存储的。在 Oracle 中,这些是 4K 块。

      Row Storage

      但是,如果您查看 Snowflake,您会发现数据存储在大约 16MB 大小的“微分区”内的列中。

      Column Storage

      这意味着你应该避免使用:

      从表中选择 *

      由于 Snowflake 需要从表中返回所有列。但是,如果您只查询您需要的列,假设该表有 100 列并且您的查询返回 2 - 您只会从远程存储返回 2 列(这更快),并且还可以更好地利用仓库缓存。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多