当前位置: 代码迷 >> Sql Server >> 左连接返回数据有关问题,
  详细解决方案

左连接返回数据有关问题,

热度:80   发布时间:2016-04-24 09:36:42.0
左连接返回数据问题,急

SELECT t2.GetChanceID,
   t2.OrderID ,
   ISNULL(t2.CombineID,0) CombineID,
   t4.UserGiftID,
   t4.PuttingID,
   t4.Tel ,
   t4.Contact ,
   t4.[Address] ,
   t4.SendState ,
   t4.ExamineState ,
   t4.ExamineState AS IsLock ,
   t4.PackagePrice,
   t4.UpdateDateTime,
   t4.ReceiveDateTime ,
   t5.PuttingName ,
   t5.Remark ,
   ISNULL(t6.AllocID, 0) AS AllocID ,
   t6.UserName,
   t7.[Status], 
   t8.CityID AS CityId,
   t9.CustomerPhone,
   t9.LastUpdateTime,
   --t10.StatusName,
   t11.LocationName 
FROM #PageData t1
INNER JOIN dbo.GetChance t2 ON t2.GetChanceID=t1.GetChanceID
INNER JOIN dbo.UserGiftInfo t4 ON t2.UserGiftID=t4.UserGiftID
INNER JOIN dbo.PuttingGiftInfo t5 WITH ( NOLOCK ) ON t5.PuttingID = t4.PuttingID
INNER JOIN EPBusinessOpportunity.dbo.MaiCheOrder t7 WITH(NOLOCK) ON t2.OrderID = t7.MCOrderId 
INNER JOIN EPBusinessOpportunity.dbo.MaiCheOrderExtend t8 WITH(NOLOCK) ON t7.MCOrderId=t8.MCOrderID
left JOIN MaiCheDealer.dbo.UserAccountBillingDetail t9 WITH ( NOLOCK )  ON t2.OrderID = t9.MCOrderId AND t9.BillingDetailStatusId NOT IN (13,14,20)
LEFT JOIN MaiCheDealer.dbo.BillingDetailStatus t10 WITH ( NOLOCK ) ON t9.BillingDetailStatusId = t10.BillingDetailStatusId
LEFT JOIN dbo.AllocSendGiftManager t6 WITH ( NOLOCK ) ON t6.GetChanceID = t1.GetChanceID
LEFT JOIN MaiCheBase.dbo.Location_Price t11 ON t8.CityID=t11.locationId


#PageData临时表有30条数据,left join MaiCheDealer.dbo.UserAccountBillingDetail t9  此表有8万多数据,查看执行计划,这个左连接Row 240多万条, 执行次数30次,为啥会返回这么多数据,请大牛帮忙,非常感谢

------解决思路----------------------
那是因为join的算法导致的,#PageData表的每一行都要和MaiCheDealer.dbo.UserAccountBillingDetail 的8万行数据关联一次,所以30×8万=240万。
但是从left JOIN MaiCheDealer.dbo.UserAccountBillingDetail t9 WITH ( NOLOCK )  ON t2.OrderID = t9.MCOrderId AND t9.BillingDetailStatusId NOT IN (13,14,20)来看,应该是t2和t9关联,而且后面那个NOT IN的条件,你确定不能放到where条件里面?
  相关解决方案