SQL:添加“ not in”会使SQL命令变慢

时间:2019-07-02 12:15:08

标签: sql-server join

在我的SQL命令中添加“ not in”语句会使整个过程非常缓慢。任何有关如何解决此问题的建议将不胜感激!

select distinct 
    d.fiscal_year,d.calendar_year,d.month_num
from vw_ePCR_eMeds_tickets v
    inner join incident_date_dim d on v.ticketdos=d.inci_date
    left join vw_all_incidents i on i.CADIncidentNumber=v.incident_num
where TicketDOS between  '12/01/2017' and '05/31/2018'
    and [transportedstatus] = 'transported'
    and i.call_type_id  not in (
        select call_type_id
        from DW_EMS_Elite.dbo.MCFRS_EMS_BillingExclude 
    )

1 个答案:

答案 0 :(得分:0)

只需尝试

with cte as (
    select call_type_id from DW_EMS_Elite.dbo.MCFRS_EMS_BillingExclude
)
select distinct 
    d.fiscal_year,
    d.calendar_year,
    d.month_num
from
    vw_ePCR_eMeds_tickets       as v
    join incident_date_dim      as d on ( v.ticketdos=d.inci_date )
    left join vw_all_incidents  as i on ( i.CADIncidentNumber=v.incident_num )
    left join cte               as c on ( c.call_type_id = i.call_type_id )
where 
    ( ( '20171201'<= TicketDOS ) and ( TicketDOS < '20180601' ) )
    and ( [transportedstatus] = 'transported' )
    and ( c.call_type_id is null )