sql - 当我更改顺序时,SQL 左连接多个表连接性能问题
问题描述
我正在使用 netezza,我有一个用例,我在 3 个表之间进行左连接。以下是我的 SQL 查询
-- FAST query (finishes in <3 secs): Join order a<-b<-c
select a.asof_date, a.sfl_fs_perm_id,
b.jobsactive, b.jobscreated, b.jobsremoved,
c.aqe_sector_code
from DBS_E474947..TEMP_UNIV a
left join DBS_E474947..TEMP_JOBS b
on a.asof_date=b.asof_date and a.factset_entity_id=b.factset_entity_id
left join DBS_E474947..TEMP_REFERENCE c
on a.asof_date=c.asof_date and a.sfl_fs_perm_id=c.sfl_fs_perm_id
limit 100;
-- SLOW query (takes 400+ secs): Join order a<-c<-b
select a.asof_date, a.sfl_fs_perm_id,
b.jobsactive, b.jobscreated, b.jobsremoved,
c.aqe_sector_code
from DBS_E474947..TEMP_UNIV a
left join DBS_E474947..TEMP_REFERENCE c
on a.asof_date=c.asof_date and a.sfl_fs_perm_id=c.sfl_fs_perm_id
left join DBS_E474947..TEMP_JOBS b
on a.asof_date=b.asof_date and a.factset_entity_id=b.factset_entity_id
limit 100;
还有尺寸
SELECT count(1) FROM DBS_E474947..TEMP_UNIV --36152447
SELECT count(1) FROM DBS_E474947..TEMP_REFERENCE --422341884
SELECT count(1) FROM DBS_E474947..TEMP_JOBS -- 73715474
有人可以帮我理解为什么第二个查询需要时间吗?我知道这是因为 TEMP_REFERENCE 表与其他两个表相比有很多记录。但是,这种剧烈变化背后的强调逻辑是什么?
查询计划是否有帮助 --FASTER TABLE Limit (cost=1758.5..1758.5 rows=100 width=53 conf=0) l: Hash Join(left outer) (cost=1758.5..1205219.9 rows=283667616751 width=53 conf= 33) l: Hash Join(右外) (cost=211.8..1356.1 rows=36152447 width=49 conf=51) l: Sequential Scan table "B" (cost=0.0..794.9 rows=73715474 width=46 conf= 80) r: 哈希 (cost=211.8..211.8 rows=36152447 width=27 conf=0) l: 顺序扫描表“A” (cost=0.0..211.8 rows=36152447 width=27 conf=100) r: 哈希(成本=1546.7..1546.7 行=422341884 宽度=21 conf=0) l:顺序扫描表“C”(成本=0.0..1546.7 行=422341884 宽度=21 conf=80)(事实)
--SLOWER TABLE Limit (cost=58492.0..58492.0 rows=100 width=53 conf=0) l: Hash Join(left outer) (cost=58492.0..1628262.7 rows=62717563532 width=53 conf=33) l: Hash连接(左外)(成本=1546.7..1203983.1 行=36152447 宽度=31 conf=51) l:顺序扫描表“A”(成本=0.0..211.8 行=36152447 宽度=27 conf=100) r:哈希(成本=1546.7..1546.7 行=422341884 宽度=21 conf=0) l:顺序扫描表“C”(成本=0.0..1546.7 行=422341884 宽度=21 conf=80)(FACT)r:哈希(成本=794.9..794.9 rows=73715474 width=46 conf=0) l:顺序扫描表“B”(成本=0.0..794.9 rows=73715474 width=46 conf=80)
解决方案
推荐阅读
- c++ - 如何为更大的项目正确准备makefile?
- javascript - Reactjs:如何在父组件和子组件中访问子组件的引用?
- vba - VBA如何根据当前日期减去2天隐藏多个工作表
- java - 为什么 jUnit 测试必须公开?
- python - 按参数提示进行类型转换
- r - 如何使用新添加的列以闪亮的方式呈现输出
- excel - SaveAs2 输出文件的编码错误
- google-sheets - 在 Google 表格中,如何使一列的值成为另一列但已排序?
- google-cloud-platform - GCP 存储桶中的 Zip 文件夹
- wordpress - 将分页重定向到网站 Wordpress 中的基本 URL