sql - 将子查询转换为联接
问题描述
我正在尝试提高我的查询性能,因为我正在处理大数据。(许多 TB 的数据)我正在研究 Impala。
我使用的查询如下所述。这是子查询的形式。不知道如何优化它。可能是加入。如果是,我无法将此子查询转换为 JOINS。
请建议如何将此子查询转换为 JOINS。我需要提高我的查询性能
select mytable.id,group_concat(mytable.acqid) as 'acqid',group_concat(mytable.address) as 'address'
from (select myinnertable.id, case when myinnertable.mykey = 'AcqId' then myinnertable.myvalue end as 'acqid',
case when myinnertable.mykey = 'Address' then myinnertable.myvalue end as 'address'
from (select id,mykey,myvalue
from table1
where table1.date_col BETWEEN '2021-05-02' and '2021-05-19' and mykey!='velList'
) myinnertable
) mytable
group by mytable.id;
解决方案
您正在做的事情可以在没有嵌套上下文的情况下简化。正如 Gordon 指出的那样,您仅使用 1 个表,但通过结果进行了 3 次传递。我会确保它有一个索引(date_col, id, mykey)
来帮助优化拉取数据。
select
t1.id,
group_concat( case when t1.mykey = 'AcqId'
then t1.myvalue end ) acqid,
group_concat( case when t1.mykey = 'Address'
then t1.myvalue end ) address
from
table1 t1
where
t1.date_col BETWEEN '2021-05-02' and '2021-05-19'
and t1.mykey != 'velList'
group by
t1.id;
推荐阅读
- mysql - 如何在 ReactJs 中保存 mysql 请求的结果?
- python-3.x - 为什么python在减去浮点值时返回错误的数字
- django - 如何在 Django 中实现广义唯一性 DB 约束 (A,B) 和 (B,A)?
- java - 如何在 Spring Boot 中的另一个 API 调用中使用一个 API 调用的结果?
- r - 如何在R中的分组条形图中以R中的升序排列子组
- java - 如何模拟从特定方法返回的地图?
- exception - FastAPI 错误处理:需要处理主键和唯一键冲突,但代码从不运行异常处理部分
- javascript - 错误:无法在“窗口”上执行“获取”:使用 GET/HEAD 方法的请求不能有正文
- python - 如何制作 xarray 数据集
- java - 如何使用 spring-jdbc 模板和 spring-transaction 在两个模块之间共享 DAO 类?