Solved

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

Posted on 2014-11-07
8
89 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
  • 3
  • 2
  • 2
8 Comments
 
LVL 5

Author Comment

by:25112
Comment Utility
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 45

Expert Comment

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

Author Comment

by:25112
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 167 total points
Comment Utility
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 48

Assisted Solution

by:PortletPaul
PortletPaul earned 333 total points
Comment Utility
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
Comment Utility
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 48

Accepted Solution

by:
PortletPaul earned 333 total points
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now