ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 254.9k

How to replace (or Plid is null) with any thing more best practice?

Sep 5 2020 7:46 AM

I work on SQL server 2012 I face issue I need to remove statement ( plid is null or ) with anything more good performance.

I enter CodeType Mandatory but plid not mandatory meaning maybe I entered or not

so main problem on my script using (or Plid is null)

I need to give anything alternative to (or Plid is null)

because it make query more slow

so what I do please

this is dashboard display count for Parts based on PLID and CodeTypeId
 
this is the full script 
  1. CREATE TABLE #TempPlAndCodeType    
  2.  (    
  3.  CodeType NVARCHAR(100),    
  4.  PlName NVARCHAR(100),    
  5.  CodeTypeId int,    
  6.  PLID   int,    
  7.  [Status] Nvarchar(500)    
  8.  )    
  9.   
  10.  create clustered index idx on #TempPlAndCodeType (CodeTypeId)           
  11.      insert into #TempPlAndCodeType(CodeType,PLID) values ('HTS-US',NULL)    
  12.     update c set c.CodeTypeId=a.AcceptedValuesOptionID from #TempPlAndCodeType c    
  13.     inner join Nop_AcceptedValuesOption a with (nolock) on    
  14.  a.Name = c.CodeType and AcceptedValuesID=5652          
  15.    update c set c.PLID=a.AcceptedValuesOptionID from #TempPlAndCodeType c    
  16.     inner join Nop_AcceptedValuesOption a with (nolock) on    
  17.  a.Name = c.PlName and AcceptedValuesID=110    
  18.         
  19.  create table #AllPlData    
  20.  (    
  21.  PLID  nvarchar(50),    
  22.  PlIDName  nvarchar(500),        
  23.  PartFamilyId  int,    
  24.  PartId  int,    
  25.  CodeType nvarchar(50),    
  26.  CodeTypeId int    
  27.  )    
  28.         
  29.  insert into #AllPlData(PLID,PlIDName,PartFamilyId,PartId)    
  30.  SELECT fmat.Value as PLID,acc.Name,fmat.PartFamilyID,pt.PartId    
  31.         
  32.  FROM    
  33.  Parts.Nop_Part pt WITH(NOLOCK)      
  34.  INNER JOIN Parts.Nop_PartsFamilyAttribute fmat WITH(NOLOCK) ON  fmat.PartFamilyID=pt.PartsFamilyID AND fmat.[Key]=20281007    
  35.  INNER JOIN dbo.Nop_AcceptedValuesOption acc WITH(NOLOCK) ON acc.AcceptedValuesOptionID=fmat.Value    
  36.         
  37.  create table #CountAllPartsPLs    
  38.  (    
  39.   PLID nvarchar(50),    
  40.   CountAllPLParts  int,    
  41.   Pl  nvarchar(500),    
  42.   CodeType  nvarchar(50),    
  43.   CodeTypeId  int    
  44.  )    
  45.  insert into #CountAllPartsPLs (PLID,CountAllPLParts)    
  46.  SELECT d.PLID,COUNT(d.PartID) AS CountAllPLParts    
  47.  FROM    
  48.  #AllPlData d    
  49.  inner join #TempPlAndCodeType c on c.PLID is null OR d.PLID=c.PLID          
  50.  GROUP BY d.PLID    
  51.         
  52.  update p set p.Pl=d.PlIDName from #CountAllPartsPLs p inner join #AllPlData d on d.PLID = P.PLID    
  53.         
  54.  select distinct t.PartId,c.CodeTypeId,c.CodeType,c.PlID,C.PlName,PartLevel into #tradecodes from parts.Tradecodes t    
  55.  inner join #TempPlAndCodeType c on t.CodeTypeId=c.CodeTypeId          
  56.         
  57.  SELECT pt.PLID,tr.CodeTypeId,    
  58.  COUNT(pt.PartID) [#partsHasCodes]    
  59.  into #partsHasCodes    
  60.  FROM  #tradecodes tr WITH(NOLOCK)    
  61.  inner join #AllPlData pt  ON pt.PartID = tr.PartID    
  62.  WHERE (tr.PLID is null OR pt.PLID=tr.PLID)    
  63.  GROUP BY pt.PLID,tr.CodeTypeId        
  64.          
  65.  SELECT s.PLID,c.CodeTypeId,    
  66.  sum(isnull(CountAllPLParts,0) - isnull([#partsHasCodes],0)) [#MissedpartsHasCodes] into #MissedpartsHasCodes    
  67.  FROM #CountAllPartsPLs s left join  #partsHasCodes c on c.PLID=s.PLID    
  68.  GROUP BY s.PLID,c.CodeTypeId          
  69.         
  70.  ---------------------------    
  71.  SELECT pt.PLID,pt.CodeTypeId,    
  72.  COUNT(c.PartID ) AS [#partLevel]    
  73.  into #TpartLevel    
  74.  FROM #partsHasCodes pt    
  75.  inner join parts.TradeCodes c on c.CodeTypeId=pt.CodeTypeid and    
  76.  where c.PartLevel=1    
  77.  GROUP BY pt.PLID,pt.CodeTypeId          
  78.  ------------------------------    
  79.               
  80.  SELECT s.PLID,c.CodeTypeId,    
  81.  sum(isnull([#partsHasCodes],0) - isnull([#partLevel],0)) [#partGeneration] into #TpartGeneration    
  82.  FROM #partsHasCodes s left join  #TpartLevel c on c.PLID=s.PLID --and c.CodeTypeid=s.CodeTypeid    
  83.  GROUP BY s.PLID,c.CodeTypeId    
  84.         
  85.  SELECT  pt.PLID,t.CodeType    
  86.  into #AllPLs    
  87.  FROM    
  88.   #TempPlAndCodeType t    
  89.  inner join #AllPlData pt  on (t.PLID is null OR pt.PLID=t.PLID)    
  90.  left join Parts.TradeCodes tr WITH(NOLOCK) ON pt.PartID = tr.PartID    
  91.  left join #TempPlAndCodeType c on (c.CodeTypeId=tr.CodeTypeID)    
  92.  GROUP BY pt.PlIDName,t.CodeType,pt.PLID    
  93.         
  94.  select L.CodeType,ca.Pl,isnull(Ca.CountAllPLParts,0) as PlPartCount,isnull(c.[#partsHasCodes],0) as PartsHaveCodes,ISNULL(m.[#MissedpartsHasCodes],0) as MissedParts ,isnull(G.[#partGeneration],0) PartsHaveGeneration,isnull(P.[#partLevel],0)PartsOnPartLevel    
  95.  from #AllPLs L    
  96.  left join #CountAllPartsPLs Ca on L.PLID=Ca.PLID    
  97.  left join #partsHasCodes C on L.PLID=c.PLID    
  98.  left join #MissedpartsHasCodes m on L.PLID=m.PLID    
  99.  left join #TpartGeneration G on G.PLID=C.PLID    
  100.  left join #TpartLevel P on P.PLID=C.PLID      
  101.         
  102.  drop table #partsHasCodes    
  103.  drop table #TpartGeneration    
  104.  drop table #TpartLevel    
  105.  drop table #MissedpartsHasCodes    
  106.  drop table #TempPlAndCodeType    
  107.  drop table #AllPLs    
  108.  drop table #CountAllPartsPLs    
  109.  drop table #AllPlData    
  110.  drop table #tradecodes    
  111.  drop table #TempPlAndCodeType