使用2个键加快加入速度

时间:2018-06-28 08:29:39

标签: sql postgresql join netezza

我可以加快速度吗?

CREATE TABLE TEST AS 
SELECT t1.Tech, t2.Coloar,t2.Car from Table1 t1 
INNER JOIN Table2 t2 on (t1.ID = t2.ID AND t1.IT = t2.IT AND t1.LIFI = t2.LIFI) OR (t1.RA = t2.RA) 
where...

如果我只是用创建表

(t1.ID = t2.ID AND t1.IT = t2.IT AND t1.LIFI = t2.LIFI)

或使用此键

(t1.RA = t2.RA) 

这需要几秒钟,但是总共要花几分钟,而且我要创建越来越多的表,有时我需要像左键一样加入这个键对,

CREATE TABLE...
INNER JOIN...
LEFT JOIN on (t1.ID = t2.ID AND t1.IT = t2.IT AND t1.LIFI = t2.LIFI) OR (t1.RA = t2.RA)

2 个答案:

答案 0 :(得分:1)

OR子句中删除ON

CREATE TABLE TEST AS 
    SELECT t1.Tech,
           COALESCE(t2.Color, tt2.Color),
           COLAESCE(t2.Car, tt2.Car)
    FROM Table1 t1 LEFT JOIN
         Table2 t2 
         ON t1.ID = t2.ID AND t1.IT = t2.IT AND t1.LIFI = t2.LIFI LEFT JOIN
         Table2 tt2
         ON t1.RA = tt2.RA AND t2.ID IS NULL
     WHERE (t2.ID IS NOT NULL OR tt2.ID IS NOT NULL) AND
           ...

这应该能够为两个联接利用适当的索引。

答案 1 :(得分:0)

尝试一下

CREATE TABLE TEST AS 
select t1.Tech, t2.Coloar,t2.Car
From Table1  t1
left join Table2 t2 on (case when t1.RA = t2.RA then 1 when t1.ID = t2.ID AND t1.IT = t2.IT AND t1.LIFI = t2.LIFI then 1 else 0 end = 1)