连接子句中带有计数不同子查询的SQL查询速度慢

时间:2019-01-16 00:41:53

标签: sql sql-server sql-server-2014

具有以下查询,在联接情况下,如果没有此子查询,该查询的效果很好。关于更有效的方法有什么想法?目前尚不清楚是否发生这种情况,因为子查询正在针对项中的所有记录运行,并且不遵守where = cms.orderno ...谢谢您的帮助!

子查询:

select count(distinct(ship_to)) 
from items 
where cms.orderno = items.orderno and ship_to <> 0

完整查询:

select
    cms.custnum,
    cms.odr_date,
    cms.orderno,
    internetid,
    ltrim(rtrim(cust.firstname))+' '+ltrim(rtrim(cust.lastname)) as    recipient,
    ord_total,
    ordertype,
    order_st2,
    websitestatdesc,
    holdcode
from 
    cms
    join statdesc on cms.order_st2 = statdesc.statcode
    join items on cms.orderno = items.orderno
    join cust on 
        case 
            when (
                select count(distinct(ship_to)) 
                from items
                where cms.orderno = items.orderno and ship_to <> 0
            ) = 1 then items.ship_to 
            when cms.shipnum <> 0 then cms.shipnum
            else cms.custnum 
        end = cust.custnum
where 
    cms.custnum = 3300 
    and statdesc.stattype = 'O'
group by 
    cms.orderno,
    cms.custnum,
    cms.odr_date,
    internetid,
    firstname,
    lastname,
    ord_total,
    ordertype,
    order_st2,
    websitestatdesc,
    holdcode
order by cms.orderno desc

1 个答案:

答案 0 :(得分:1)

首先,您在表orderno的列items上有索引吗?我想您不会返回很多行...因此缺少索引可能是一个杀手.。

然后,我将倾向于修改查询以避免出现如下group by(假设我当然理解您的意图)的情况:

select
  cms.custnum
  , cms.odr_date
  , cms.orderno
  , internetid
  , ltrim(rtrim(cust.firstname))+' '+ltrim(rtrim(cust.lastname)) as recipient
  , ord_total
  , ordertype
  , order_st2
  , websitestatdesc
  , holdcode
from (
  select *, (
      select count(distinct(ship_to)) 
      from items
      where cms.orderno = items.orderno and ship_to <> 0
    ) distinct_ship_to_count
    , (
      select top 1 ship_to 
      from items
      where cms.orderno = items.orderno and ship_to <> 0
    ) distinct_ship_to
    from cms
) cms
join statdesc on cms.order_st2 = statdesc.statcode
--join items on cms.orderno = items.orderno
join cust on 
  case 
    when cms.distinct_ship_to_count = 1 then cms.distinct_ship_to
    when cms.shipnum <> 0 then cms.shipnum
    else cms.custnum 
  end = cust.custnum
where cms.custnum = 3300 
and statdesc.stattype = 'O'
--group by cms.orderno, cms.custnum, cms.odr_date, internetid, firstname, lastname, ord_total, ordertype, order_st2, websitestatdesc, holdcode
order by cms.orderno desc