首页 > 解决方案 > 当我更改顺序时,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)

标签: sqlperformancenetezza

解决方案


推荐阅读