为什么当我用下面的聚合查询查询基本表时,雪花没有引用我的MV?
create or replace table customer_sample as (
SELECT * FROM
"SNOWFLAKE_SAMPLE_DATA"."TPCDS_SF100TCL"."CUSTOMER");
create or replace materialized view customer_sample_mv
as
select c_customer_sk,
sum(c_current_hdemo_sk) total_sum
from customer_sample
group by 1;
select c_customer_sk,
sum(c_current_hdemo_sk) total_sum
from customer_sample
group by 1;发布于 2022-04-22 21:11:08
有很多可能的原因。
发布于 2022-04-23 04:17:01
在这个例子中,雪花通过跳过物化视图来做正确的事情。
第一个惊喜:扫描物化视图比重新运行查询要慢:
select *
from customer_sample_mv
order by total_sum desc nulls last
limit 100;
-- 4.4svs
select *
from (
select c_customer_sk,
sum(c_current_hdemo_sk) total_sum
from customer_sample
group by 1
)
order by total_sum desc nulls last
limit 100;
-- 3.6s

因此,雪花通过不选择物化视图来节省时间。
这怎麽可能?
原来没有重复的客户身份证明。所以分组前他们什么也做不了。
select c_customer_sk, count(*) c
from customer_sample
group by 1
having c>1
order by 2 desc
limit 10;
-- null从医生那里:
即使物化视图可以替换特定查询中的基表,优化器也可能不使用物化视图。例如,如果基表由字段聚集,优化器可能选择扫描基表(而不是物化视图),因为优化器可以有效地删除分区,并使用基表提供等效的性能。
https://stackoverflow.com/questions/71973389
复制相似问题