Solved

can lot of CASTs/Union or ISNULL possibly cause blocks?

Posted on 2014-11-07
8
106 Views
Last Modified: 2014-11-25
This query runs fine every day scheduled.. but yesterday, it was blocked by itself in sysprocesses.. when I did select * from master..sysprocesses where blocked<>0 I saw 12 lines all for this one process running this, and all with same spid and same blocked column data.

is it because of excessive UNION/CAST etc. CAn you suggest?
0
Comment
Question by:25112
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
8 Comments
 
LVL 5

Author Comment

by:25112
ID: 40428647
The actual processing is from stored proc.. but I put it as code format by substituting the parameter values here to illustrate, any thoughts on making this code efficient to avoid blocks/locks?

 ~~~~~~~~~~~~~~~~~~~~~~
SET NOCOUNT ON        
        
DECLARE @JobYear varchar(4)          
SET @JobYear = '2004'        
DECLARE @PairedID char(3)          
SET @PairedID = '113'         
        
        
DECLARE @ExtractDt datetime        
SET @ExtractDt = (Select Distinct Max(dbo.PASNote.ExtractDt)         
   From dbo.PASNote         
   Where dbo.PASNote.JobYear = @JobYear)        
        
DECLARE @PPExtractDate datetime        
SET @PPExtractDate = (Select Distinct  PPExtractDate        
   From dbo.PASNote         
   Where dbo.PASNote.JobYear = @JobYear        
               And ExtractDt = @ExtractDt)        
          
 Select         
 CAST(@JobYear as char(4))         
        + '-'        
        + CAST(@JobYear+1 as char(4)) As [JobYear],        
 CAST([PairID] as int) As [FinalizedPair],        
          CAST([BCPERName] as varchar(60)) As [FinalizedBCPER],        
        CAST([UCANName] as varchar(30)) As [FinalizedUCAN],        
        CAST((SUBSTRING(Cast([PairID] as char),1,1) + '-'        
         + SUBSTRING(Cast([PairID] as char),2,2) + '-'        
          + SUBSTRING(Cast([PairID] as char),4,2) + '-'        
           + SUBSTRING(Cast([PairID] as char),6,3) + '-'        
          + SUBSTRING(Cast([PairID] as char),9,1)) as varchar(30)) As [mtPair],        
        CAST(TempTable.[PickID] as varchar(4)) PickID,        
        CASE WHEN TempTable.[PickID]  IN('J2','J3','J4','J5','J6') THEN 'J2'        
                    WHEN TempTable.[PickID]  IN('M2','M3') THEN 'M2'           
                    ELSE  TempTable.[PickID]   END AS  [PickGroupCd],        
 CAST(' ' as varchar(60)) As [RegulatingNUA],        
        CAST(' ' as varchar(30)) As [SchedProgramCd],        
 CAST(' ' as varchar(30)) As [SchedIdCd],        
CAST(' ' AS VARCHAR(5)) AS [LeadStatusCd],        
CAST(' ' AS VARCHAR(5)) AS [PLKStatusCd],        
 CAST(0 as smallint) As [PeakedCap],        
 CAST(0 as int) As [Accumul],        
        CAST(0 as decimal(18,3)) As [PeakedMDT],        
 CAST(0 as decimal(18,5)) As [PaceAdj],        
        CAST(0  as dec(13,0)) as [PrePHCstDst],        
 CAST(0  as dec(13,3)) as [PrePHMDT],        
 CAST(0  as dec(13,0)) as [PrePTCstDst],        
 CAST(0  as dec(13,3)) as [PrePTMDT],         
        CAST(0  as dec(13,0)) as [PeakedPHForCstDst],        
        CAST(0  as dec(13,3)) as [PeakedPHForMDT],        
        CAST(0  as dec(13,0)) as [PeakedPTForCstDst],        
        CAST(0  as dec(13,3)) as [PeakedPTForMDT],        
 CAST(0  as dec(13,0)) as [PHJustForCstDst],        
 CAST(0  as dec(13,3)) as [PHJustForMDT],        
 CAST(0  as dec(13,0)) as [PTJustForCstDst],        
 CAST(0  as dec(13,3)) as [PTJustForMDT],        
 CAST(0  as dec(13,0)) as [PHPunchCstDst],        
 CAST(0  as dec(13,3)) as [PHPunchMDT],        
 CAST(0  as dec(13,0)) as [PTPunchCstDst],        
 CAST(0  as dec(13,3)) as [PTPunchMDT],        
 CAST(0  as dec(13,0)) as [BaseCstDst],        
 CAST(0  as dec(13,3)) as [BaseMDT],        
 CAST(0  as dec(13,0)) as [AdvCstDst],        
 CAST(0  as dec(13,3)) as [AdvMDT],        
        CAST([UCANName] as varchar(30)) AS [UCANName],        
        Cast(@ExtractDt As datetime) As [ExtractDt],        
        Cast(@PPExtractDate as datetime) AS [PPExtractDate]        
 FROM  dbo.CI_NUA         
        INNER JOIN         
        (SELECT CAST('DX' AS VARCHAR(10)) AS [NUACTGY],        
       CAST(TBLSC.[PickID] AS VARCHAR(4)) PickID        
       FROM        
       (SELECT CAST('A1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('A2' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('A3' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('B1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('C1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('D1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('D2' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('E1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('E2' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('F1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('G1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('H1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('I1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('J1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('K1' AS VARCHAR(4)) PickID        
 UNION ALL        
       SELECT CAST('L1' AS VARCHAR(4)) PickID        
       UNION ALL        
  SELECT CAST('L2' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('M1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('N1' AS VARCHAR(4)) PickID        
       UNION ALL        
       SELECT CAST('O1' AS VARCHAR(4)) PickID        
     ) AS TBLSC        
       ) AS TempTable        
       ON dbo.CI_NUA.[BCPERAbbr] = TempTable.[NUACTGY]        
       AND dbo.CI_NUA.[JobYearFK] = @JobYear        
        WHERE [BCPERAbbr] = 'DX' AND PairID = @PairedID        
        AND TempTable.[PickID] NOT IN        
        (SELECT DISTINCT [ReportPickID]        
                FROM dbo.PASNote         
                WHERE GroupPair = @PairedID AND ExtractDt = @ExtractDt      
                AND [JobYear] = @JobYear)        
--         
--        
Union All          
--        
-- Select Note report lines from table dbo.PASNote         
--        
 Select         
 CAST(JobYear as char(4)) + '-' +        
        CAST((CAST(JobYear as dec(4,0)) + 1) as char(4)) As [JobYear],        
 CAST(GroupPair as int) As [FinalizedPair],        
        CAST(NUA.[BCPERName] as varchar(60)) As [FinalizedBCPER],        
        CAST(NUA.[UCANName] as varchar(30)) As [FinalizedUCAN],        
        CAST((SUBSTRING(Cast([GroupPair] as char),1,1) + '-'        
         + SUBSTRING(Cast([GroupPair] as char),2,2) + '-'        
          + SUBSTRING(Cast([GroupPair] as char),4,2) + '-'        
           + SUBSTRING(Cast([GroupPair] as char),6,3) + '-'        
          + SUBSTRING(Cast([GroupPair] as char),9,1)) as varchar(30)) As [mtPair],        
        CAST(ReportPickID as varchar(4)) PickID,        
        CASE WHEN ReportPickID  IN('J2','J3','J4','J5','J6') THEN 'J2'        
                    WHEN ReportPickID  IN('M2','M3') THEN 'M2'        
                    ELSE  ReportPickID   END AS  [PickGroupCd],        
 CAST(RegulatingBCPERName as varchar(60)) As [RegulatingNUA],        
        CAST(IsNull(SchedProgramCd,' ') as varchar(30)) As [SchedProgramCd],        
 CAST(IsNull(SchedIdCd,' ') as varchar(30)) As [SchedIdCd],        
    CAST([LeadStatusCd] AS VARCHAR(5)) AS [LeadStatusCd],        
CAST([PLKStatusCd] AS VARCHAR(5)) AS [PLKStatusCd],        
 CAST(IsNull(PeakedCap,0) as smallint) As [PeakedCap],        
 CAST(IsNull(Accumul,0) as int) As [Accumul],        
        CAST(IsNull(PeakedMDT1,0) as decimal(18,3)) As [PeakedMDT],        
 CAST(IsNull(DistrictPaceAdj,0) as decimal(18,5)) As [PaceAdj],        
        CAST(IsNull(PrePHCstDst,0)  as dec(13,0)) as [PrePHCstDst],        
 CAST(IsNull(PrePHMDT,0)  as dec(13,3)) as [PrePHMDT],        
 CAST(IsNull(PrePTCstDst,0)  as dec(13,0)) as [PrePTCstDst],        
 CAST(IsNull(PrePTMDT,0)  as dec(13,3)) as [PrePTMDT],         
        CAST((CAST(IsNull(PHJustForCstDst,0)  as dec(13,0)) + CAST(IsNull(PHPunchCstDst,0)  as dec(13,0)))  as dec(13,0)) as [PeakedPHForCstDst],        
        CAST((CAST(IsNull(PHJustForMDT,0)  as dec(13,3)) + CAST(IsNull(PHPunchMDT,0)  as dec(13,3)))  as dec(13,3)) as [PeakedPHForMDT],        
        CAST((CAST(IsNull(PTJustForCstDst,0)  as dec(13,0)) + CAST(IsNull(PTPunchCstDst,0)  as dec(13,0)))  as dec(13,0)) as [PeakedPTForCstDst],        
        CAST((CAST(IsNull(PTJustForMDT,0)  as dec(13,3)) + CAST(IsNull(PTPunchMDT,0)  as dec(13,3)))  as dec(13,3)) as [PeakedPTForMDT],        
 CAST(IsNull(PHJustForCstDst,0)  as dec(13,0)) as [PHJustForCstDst],        
 CAST(IsNull(PHJustForMDT,0)  as dec(13,3)) as [PHJustForMDT],        
 CAST(IsNull(PTJustForCstDst,0)  as dec(13,0)) as [PTJustForCstDst],        
 CAST(IsNull(PTJustForMDT,0)  as dec(13,3)) as [PTJustForMDT],        
 CAST(IsNull(PHPunchCstDst,0)  as dec(13,0)) as [PHPunchCstDst],        
 CAST(IsNull(PHPunchMDT,0)  as dec(13,3)) as [PHPunchMDT],        
 CAST(IsNull(PTPunchCstDst,0)  as dec(13,0)) as [PTPunchCstDst],        
 CAST(IsNull(PTPunchMDT,0)  as dec(13,3)) as [PTPunchMDT],        
 CAST(IsNull(BaseCstDst,0)  as dec(13,0)) as [BaseCstDst],        
 CAST(IsNull(BaseMDT,0)  as dec(13,3)) as [BaseMDT],        
 CAST(IsNull(AdvCstDst,0)  as dec(13,0)) as [AdvCstDst],        
 CAST(IsNull(AdvMDT,0)  as dec(13,3)) as [AdvMDT],        
      CAST(NUA.UCANName as varchar(30)) AS [UCANName],        
        Cast(ExtractDt As datetime) As [ExtractDt],        
        Cast(PPExtractDate As datetime) As [PPExtractDate]        
 FROM  dbo.PASNote         
        Inner Join dbo.CI_NUA NUA        
        On dbo.PASNote.GroupPair = NUA.PairID        
        AND NUA.[JobYearFK] = @JobYear        
        WHERE        
        JobYear = @JobYear and      
        GroupPair = @PairedID AND        
        ExtractDt = @ExtractDt        
   
 Order by FinalizedPair,         
                 PickID,RegulatingNUA,        
                 PeakedCap,SchedIdCd         
        
SET NOCOUNT OFF

Open in new window

0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40428666
What causes locks are long running queries. How long usually this query takes to run?
0
 
LVL 5

Author Comment

by:25112
ID: 40428806
right now it just ran in 3 seconds..  it has run scheduled for many months.. it was just yesterday that this has misbehaved (that we saw)..

what was puzzling was generally in sysprocesses, the blocked id is different than spid.. but here spid was same as blocked id and also several lines...
0
Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

 
LVL 51

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 167 total points
ID: 40428820
Maybe was a single time issue. I recommend you to let it be and keep an eye on the process to see if will happen again.
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 333 total points
ID: 40429769
Just a small observation,

      SELECT DISTINCT
            MAX(dbo.PASNote.ExtractDt)
      FROM dbo.PASNote
      WHERE dbo.PASNote.JobYear = @JobYear


There is only a single value returned, so "distinct" is completely redundant in such circumstances.
"select distinct": use it sparingly because it is the enemy of performance.
0
 
LVL 5

Author Comment

by:25112
ID: 40438226
thank you. I will keep an eye on it.

Paul, appreciate your note.

do you have an mock tsql example to show how bad performance can deteriorate by adding a distinct?
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 333 total points
ID: 40439216
do you have an mock tsql example to show how bad performance can deteriorate by adding a distinct?
No. (And note, the impact would probably not be measurable in the query above. )

I raise it so you will be more aware of the implications it brings; it can be horrible. Literally horrible.


I do have an article on Distinct: Select Distinct is returning duplicates (Look for "Hail Mary..." in particular)
and in the references: Why I Hate DISTINCT
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question