sql - 如何使用 with 语句加速慢速左连接 SQL 查询?
问题描述
我有一个位于多列的左连接查询(在雪花中)。X
有 1100 万行和Y
1 亿行。它非常慢(大约 25 分钟),每天需要运行很多次。有没有可能使用 with 语句(或任何其他方法)来加速它?这是查询:
select id, start_ts, start_ts_lgn from
(select id, start_ts
from X
where start_ts >= '2019-11-01')a
left join
(select id as id_frm_lgn, cast(cast(strt_dt as varchar(10))||' '||cast(strt_tm as varchar(12)) as timestamp) as start_ts_lgn
from Y
where strt_dt >= '2019-10-01')b
on a.id = b.id_frm_lgn
and a.start_ts > b.start_ts_lgn;
解决方案
要加快此查询,您可以添加索引:
create index ix1 on X (start_ts, id);
create index ix2 on Y (strt_dt, id);
推荐阅读
- c++ - 将文件上传到 S3 aws sdk cpp 时出错
- python - 如何为 ECS 任务定义创建日志记录?
- spring - 为什么轴突框架不能插入到自己的表中?
- javascript - 无法读取未定义的属性“id”
- node.js - 用于响应和表达受保护路由的 JSON webtoken 登录授权
- c# - 如何在 C#/.net 中将 XML 数据链接在一起
- python-3.x - 如何识别和计算矩形中的圆?
- github - 在同一个网站(Jekyll GitHub)上创建两个博客而不改变布局/格式
- excel - 如何使用 Excel VBA 单击 Web 复选框?
- facebook - 如何在没有访问令牌的组中发布帖子