clickhouse双分布式子查询被拒绝: select * from chuz_ods.ods_mes_sfc_all where toDate(partition_date) = yesterda

select * from chuz_ods.ods_mes_sfc_all 
where toDate(partition_date) = yesterday() 
or partition_date in 
(select partition_date from chuz_ods.ods_mes_sfc_all 
where toDate(modified_date_time) = yesterday());

但是clickhouse是不是不支持这样的子查询呀? 会报以下错误:
SQL 错误 [288] [07000]: Code: 288. DB::Exception: Double-distributed IN/JOIN subqueries is denied (distributed_product_mode = ‘deny’). You may rewrite query to use local tables in subqueries, or use GLOBAL keyword, or set distributed_product_mode to suitable value.: While processing chuz_ods.ods_mes_sfc_all: While processing partition_date IN (SELECT partition_date FROM chuz_ods.ods_mes_sfc_all WHERE toDate(modified_date_time) = yesterday()).

select * from chuz_ods.ods_mes_sfc_all 
where toDate(partition_date) = yesterday() 
or toDate(partition_date) GLOBAL in 
(select toDate(partition_date) 
from chuz_ods.ods_mes_sfc_all 
where toDate(modified_date_time) = yesterday());

这个解决方案使用GLOBAL关键字告诉ClickHouse在子查询中执行全局查询,从而绕过了双分布式子查询的限制。

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了优化这段代码,可以考虑以下几点: 1. 使用JOIN代替子查询:将子查询中的表连接操作改为使用JOIN语句,可以提高查询效率。 2. 使用EXISTS代替IN:将子查询中的IN子句改为EXISTS子句,可以提高查询效率。 3. 使用变量存储重复的表达式:在查询中使用重复的表达式会增加计算成本,可以使用变量存储这些重复的表达式,以减少计算次数。 4. 使用索引:根据查询条件和连接条件,在关键列上创建索引,可以提高查询性能。 下面是优化后的代码: ```sql INSERT INTO fr.fr_equipment_fill_out_all SELECT date2, equipment, team, target_oee, theory_qty, theory_ppm, stop_time, plan_downtime, text1, text2, text3, text4, text5 FROM ( SELECT ROW_NUMBER() OVER (PARTITION BY t1.equipment ORDER BY t1.date1) AS num, neighbor(t1.date1, 1) AS date2, neighbor(t1.equipment, 1) AS equipment, t1.date1, t1.equipment, t2.* FROM ( SELECT DISTINCT a.date1, b.equipment FROM chuz_dim.dim_mes_date_all a LEFT JOIN fr.fr_equipment_fill_out_all b ON 1 = 1 WHERE date1 BETWEEN (SELECT MIN(date_time) FROM fr.fr_equipment_fill_out_all) AND NOW() ) t1 LEFT JOIN ( SELECT a.date1, b.* FROM chuz_dim.dim_mes_date_all a LEFT JOIN fr.fr_equipment_fill_out_all b ON a.date1 = toDate(b.date_time) WHERE date1 BETWEEN (SELECT MIN(date_time) FROM fr.fr_equipment_fill_out_all) AND NOW() ) t2 ON t1.date1 = t2.date1 AND t1.equipment = t2.equipment WHERE date2 > date1 AND date_time = (SELECT MAX(date_time) FROM fr.fr_equipment_fill_out_all) ) t3; ``` 请注意,这只是一种可能的优化方法,具体的优化策略还需要根据数据量和表结构进行调整。在实际应用中,建议根据数据库的性能特点进行测试和调整,以获得最佳的查询性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值