筛选具有一对多父级和子级映射的表的最有效方法

时间:2018-12-06 20:52:49

标签: sql oracle self-join query-tuning

我有一个客户表,该表具有一个外键,其中每个客户在每个部门中都有一个特定的ID,但有一个主ID。我试图找到最有效的方法来将查询限制为仅主条目。

这是我完成的两个(简化)查询,但是我觉得有一种更有效的方法来完成此操作,尤其是在连接到其他大表时:


-- version 1
select 
    client.id
from
    client
        join client client2 on client.id = client2.masterid 
                               and client2.id = client2.masterid

--version 2

select 
    client.id,
from
    client
where
    client.id = client.masterid

-- Expanded view
select
t1.id masterid,
t1.dob dob,
trunc((months_between(trunc(sysdate),t1.dob)/12),0) age,
case
    when substr(t1.zip,1,5) in ('48502','48503','48504','48505','48506','48507','48529','48532') then null 
else
(select
    max(audit1.operationid)
from
    t2 audit1
where
    t1.id = audit1.sourceid                                                                                             
    and audit1.fieldname = 'ZIP'
    and substr(audit1.oldvalue,1,5) in ('48502','48503','48504','48505','48506','48507','48529','48532')                        
    and audit1.created >= to_date('04/25/2014', 'MM/DD/YYYY')                                                                   
    and 1 < (                                                                                                                   
            select
                count(audr.id)
            from
                t2 audr
            WHERE
                audr.operationid = audit1.operationid
                and audr.fieldname in ('ADDRESS1','CITY')                                                                       
            )                                                                                                                   

) end auditref,
t1.address1 addr1,
t1.address2 addr2,
t1.city city,
substr(t1.zip,1,5) zip
from
t1
where
t1.id = t1.masterid
and 1 = case
            when substr(t1.zip,1,5) in ('48502','48503','48504','48505','48506','48507','48529','48532') then 1
            when substr(t1.zip,1,5) not in ('48502','48503','48504','48505','48506','48507','48529','48532') and exists                                                                                                         
                                    (select
                                        1
                                    from
                                        t2 audit2
                                    where
                                        audit2.sourceid = t1.id                                                                         
                                        and audit2.fieldname = 'ZIP'
                                        and substr(audit2.oldvalue,1,5) in ('48502','48503','48504','48505','48506','48507','48529','48532')    
                                        and audit2.created >= to_date('04/25/2014', 'MM/DD/YYYY')                                               
                                    ) then 1
            else 0
        end 

任何想法都将受到赞赏,因为我尝试过这些联接的任何其他方式都会导致重复行,因为每个masterid可能有许多id。

编辑:

这是查询的扩展版本,但是在使用client.id = client.masterid的情况下使用了更多的联接和过滤器,导致查询运行慢得多

问题是限制t1和t2表扫描的最有效方法,因为这些表很大...

1 个答案:

答案 0 :(得分:0)

使用以下联接完成了限制表扫描的目标:


from
    client 
        left join client client1 on client1.masterid = client.id and client1.id is null