Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

view help 2

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
OK, I have been working on this view for a while. The view pulls all of the correct information, but it is painfully slow (48 seconds to build a view just under 100k rows). I know at least part of the problem is that the underlying table structure was not well thought out ahead of time and unfortunately we cannot change it... Here is the full query
Code:
SELECT     PrimaryTbl.SpecimenID, COALESCE (SecondaryTbl.PptID, PrimaryTbl.PptID) AS pptid, 
	CASE 
		WHEN LEFT(COALESCE (SecondaryTbl.pptid, PrimaryTbl.pptid), 1) = '0' THEN substring((COALESCE (SecondaryTbl.pptid, PrimaryTbl.pptid)), 2, 2) 
		ELSE LEFT(COALESCE (SecondaryTbl.pptid, PrimaryTbl.pptid), 3) 
    END AS CollectionSiteID, 
    s.Abbreviation AS CollectionSiteName, 
    CASE 
		WHEN PrimaryTbl.parentspecimenid = '' THEN 'Parent' 
		ELSE 'Child' 
	END AS parentorchild, 
    PrimaryTbl.SpecimenSourceID,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues
    WHERE      (ElementID = 8) AND (Active = 1) AND (Value = PrimaryTbl.SpecimenSourceID)) AS SpecimenSourceName, 
    PrimaryTbl.SpecimenStoredID,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_18
    WHERE      (ElementID = 9) AND (Active = 1) AND (Value = PrimaryTbl.SpecimenStoredID)) AS SpecimenStoredName, 
    PrimaryTbl.Quantity, 
    CASE 
		WHEN PrimaryTbl.UnitID = 0 THEN 'unknown' 
		WHEN PrimaryTbl.UnitID = 1 THEN 'µl' 
		WHEN PrimaryTbl.UnitId = 2 THEN 'ml' 
	END AS Unit, 
    PrimaryTbl.CollectionDate, 
    PrimaryTbl.Comments, 
    PrimaryTbl.ParentSpecimenID, 
    CASE 
		WHEN PrimaryTbl.PptVisitType = 0 THEN SecondaryTbl.PptVisitType 
		ELSE PrimaryTbl.PptVisitType 
	END AS PptVisitType,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_17
    WHERE      (ElementID = 82) AND (Active = 1) AND 
    (Value = CASE 
				WHEN PrimaryTbl.PptVisitType = 0 THEN SecondaryTbl.PptVisitType 
				ELSE PrimaryTbl.PptVisitType 
			 END)) AS PptVisitTypeName, 
   PrimaryTbl.PptVisitDate, 
   PrimaryTbl.CurrentQuantity, 
   PrimaryTbl.CurrentQuantityUnitID,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_16
    WHERE      (ElementID = 47) AND (Active = 1) AND (Value = PrimaryTbl.CurrentQuantityUnitID)) AS CurrentQuantityUnitName, 
    PrimaryTbl.Concentration, 
    PrimaryTbl.ConcentrationUnitID,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_15
    WHERE      (ElementID = 42) AND (Active = 1) AND (Value = PrimaryTbl.ConcentrationUnitID)) AS ConcentrationUnitName, 
   PrimaryTbl.NumOfThawed, 
   L.SiteID AS StorageLabID,
   L.LabNameShort AS StorageLabName,
   PrimaryTbl.RefrigeratorID, 
   PrimaryTbl.BoxID, 
   PrimaryTbl.Row, 
   PrimaryTbl.Col, 
   --if specimenstoredid in 2,35,9, then fasting needs to be inherited from the parent
   Case
	WHEN PrimaryTbl.specimenstoredid in (2,9,35) THEN SecondaryTbl.Fasting
	ELSE PrimaryTbl.Fasting
	END AS Fasting,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_14
    WHERE      (ElementID = 434) AND (Active = 1) AND (Value = Case
		WHEN PrimaryTbl.specimenstoredid in (2,9,35) THEN SecondaryTbl.Fasting
		ELSE PrimaryTbl.Fasting
		END)) AS FastingName, 
    PrimaryTbl.Status,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_13
    WHERE      (ElementID = 29) AND (Active = 1) AND (Value = PrimaryTbl.Status)) AS StatusName, 
    PrimaryTbl.ReasonOfChange, 
    PrimaryTbl.ReasonOfDefective,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_12
    WHERE      (ElementID = 61) AND (Active = 1) AND (Value = PrimaryTbl.ReasonOfDefective)) AS ReasonOfDefectiveName, 
    PrimaryTbl.OtherReasonOfDefective, 
    PrimaryTbl.TubeSize, 
    PrimaryTbl.MainSelected,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195
    WHERE      (ProcessQuestionID = 422) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS SpecimensFrozenAtMinus70OrColder,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_11
    WHERE      (ElementID = 422) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_26
         WHERE      (ProcessQuestionID = 422) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS SpecimensFrozenAtMinus70OrColderName,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_25
    WHERE      (ProcessQuestionID = 431) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS PQComments,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_24
    WHERE      (ProcessQuestionID = 435) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS SpecimenPlacedImmedOnIceOrAt4Celsius,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_10
    WHERE      (ElementID = 435) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_23
         WHERE      (ProcessQuestionID = 435) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS SpecimenPlacedImmedOnIceOrAt4CelsiusName,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_22
    WHERE      (ProcessQuestionID = 438) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS FreezerNumberSpecimenStoredAt,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_21
    WHERE      (ProcessQuestionID = 439) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS SpecimenHemolyedLipemicIcteric,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_9
    WHERE      (ElementID = 439) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_20
         WHERE      (ProcessQuestionID = 439) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS SpecimenHemolyedLipemicIctericName,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_19
    WHERE      (ProcessQuestionID = 442) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS TimeFromCollectionToFreezingHours,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_18
    WHERE      (ProcessQuestionID = 443) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS EquipmentFailureDeviationFromProtocol,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_8
    WHERE      (ElementID = 443) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_17
         WHERE      (ProcessQuestionID = 443) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS EquipmentFailureDeviationFromProtocolName,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_16
    WHERE      (ProcessQuestionID = 450) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS VolumeRawUrineProcessed,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_15
    WHERE      (ProcessQuestionID = 451) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS SpecimenInverted,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_7
    WHERE      (ElementID = 451) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_14
         WHERE      (ProcessQuestionID = 451) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS SpecimenInvertedName,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_13
    WHERE      (ProcessQuestionID = 454) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS SpecimenProcessedWithin,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_6
    WHERE      (ElementID = 454) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_12
         WHERE      (ProcessQuestionID = 454) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS SpecimenProcessedWithinName,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_11
    WHERE      (ProcessQuestionID = 455) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS TissuePosition,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_5
    WHERE      (ElementID = 455) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_10
         WHERE      (ProcessQuestionID = 455) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS TissuePositionName,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_9
    WHERE      (ProcessQuestionID = 456) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS StorageTemperature,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_4
    WHERE      (ElementID = 456) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_8
         WHERE      (ProcessQuestionID = 456) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS StorageTemperatureName,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_7
    WHERE      (ProcessQuestionID = 457) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS ProcessingMethod,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_3
    WHERE      (ElementID = 457) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_6
         WHERE      (ProcessQuestionID = 457) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS ProcessingMethodName,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_5
    WHERE      (ProcessQuestionID = 458) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS PathologyDeptBlockNumber,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_4
    WHERE      (ProcessQuestionID = 459) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS PrimaryTissueType,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_2
    WHERE      (ElementID = 459) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_3
         WHERE      (ProcessQuestionID = 459) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS PrimaryTissueTypeName,
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_2
    WHERE      (ProcessQuestionID = 460) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS SpecimenProcessedProtocol,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_1
    WHERE      (ElementID = 460) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_1
         WHERE      (ProcessQuestionID = 460) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS SpecimenProcessedProtocolName
FROM	dbo.tblSpecimenBank195 AS PrimaryTbl 
		LEFT OUTER JOIN dbo.tblSpecimenBank195 AS SecondaryTbl ON PrimaryTbl.ParentSpecimenID = SecondaryTbl.SpecimenID 
		INNER JOIN NewCompass.dbo.tblSite AS s ON s.SiteID = 
			(CASE 
				WHEN LEFT(COALESCE (SecondaryTbl.pptid, PrimaryTbl.pptid), 1) 
                      = '0' THEN substring((COALESCE (SecondaryTbl.pptid, PrimaryTbl.pptid)), 2, 2) 
                ELSE LEFT(COALESCE (SecondaryTbl.pptid, PrimaryTbl.pptid), 3) 
             END) LEFT JOIN tblLabs L on PrimaryTbl.LabID=L.LabID
The parts that I am particularly interested in are the two areas where I have to make subquery calls over and over to separate tables. For instance
Code:
  (SELECT     Answer
    FROM          dbo.tblSpecimenPQ195
    WHERE      (ProcessQuestionID = 422) AND (SpecimenID = PrimaryTbl.SpecimenID)) AS SpecimensFrozenAtMinus70OrColder,
  (SELECT     ValueName
    FROM          dbo.tblMetaElementValues AS tblMetaElementValues_11
    WHERE      (ElementID = 422) AND (Value =
       (SELECT     Answer
         FROM          dbo.tblSpecimenPQ195 AS tblSpecimenPQ195_26
         WHERE      (ProcessQuestionID = 422) AND (SpecimenID = PrimaryTbl.SpecimenID)))) AS SpecimensFrozenAtMinus70OrColderName,
Uses the same code twice, once as the main subquery and then again as a nested subquery, and I have to do this like 16 times. I have tried this with a scalar valued function and it slowed it down massively. I have considered breaking this down into multiple steps, creating a few views and then creating an aggregate view, but I haven't been able to see where that would cut any time. Looking at the Execution Plan, there are no big time draws, but there are a bunch at about 3% on tblSpecimenPQ195. That table has a clustered index on ProcessQuestionID and SpecimenID. Any thoughts on what I might try to speed this up a bit?

Thanks,
wb
 
did you add the index that George asked for? if so it may cause a significant change.

Also the split of my original sub-query into 2 separate sub-queries will most likely make a difference - I was going to post it today but George beat me to it :)

And... I missed the fact that the inner join had (probably) no filter applied by joining it to tblSpecimenBank195 - that also makes a difference for sure




Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
I'm surprised that the execution dropped by changing the join to LEFT instead of INNER. Usually inner joins are faster than lefts.

HA. In my mind I was thinking that the query would take about 10 seconds, and then you said 9 seconds.

Anyway... are you satisfied with the performance?
Do you understand the changes that were made to the query and (more importantly) why it would have such an impact on performance?

Since the last index I suggested didn't make any difference on the performance of the smaller query, I suggest you drop the index and then run the full query again. If the execution time gets longer after dropping the index, then put it back.

There is no sense in having extra indexes in the system that never get used.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Yes, I think I understand why the structure changed and how it affected the performance and yes, I am very happy with the performance. Thank you both for your help, I greatly appreciate it. I have never really used sub-selects in join statements, but now I see they can be quite beneficial.

Thanks again,
wb
 
Glad you are happy with both.

Yes sub selects can be extremely helpful and on top of that they can also make code a lot more readable

Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top