SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Previously well-behaved query is now causing tempdb to choke up


Previously well-behaved query is now causing tempdb to choke up

Author
Message
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16010 Visits: 19524
How many rows are returned by the SELECT version of the first update in your script?

SELECT PatientKey = P.PatientKey 
, AgeKey = PM.AgeKey
, GenderKey = P.GenderKey
, GenderCode = P.GenderCode
, GPPracticeKey = P.GPPracticeKey
, GPCodeKey = P.GPCodeKey
-- , PCTKey = P.PCTKey
--, PCTCode = P.PCTCode
--Added on 11/04/2007
, GPPracticeCode = P.GPPracticeCode
--
, LocationKey = P.LocationKey
, DeprivationIMDKey = PM.DeprivationIMDKey
, DeprivationNationalKey = PM.DeprivationNationalKey
FROM dbo.LoadConsultationsOP SCOP
INNER JOIN dbo.Patient P ON SCOP.PatientId = P.PatientId
INNER JOIN dbo.PatientMonths PM ON P.PatientKey = PM.PatientKey
AND SCOP.DatePeriodKey = PM.DatePeriodKey



You might also want to change the update slightly;
UPDATE SCOP 
SET PatientKey = P.PatientKey
, AgeKey = PM.AgeKey
, GenderKey = P.GenderKey
, GenderCode = P.GenderCode
, GPPracticeKey = P.GPPracticeKey
, GPCodeKey = P.GPCodeKey
-- , PCTKey = P.PCTKey
--, PCTCode = P.PCTCode
--Added on 11/04/2007
, GPPracticeCode = P.GPPracticeCode
--
, LocationKey = P.LocationKey
, DeprivationIMDKey = PM.DeprivationIMDKey
, DeprivationNationalKey = PM.DeprivationNationalKey
FROM dbo.LoadConsultationsOP SCOP
INNER JOIN dbo.Patient P ON SCOP.PatientId = P.PatientId
INNER JOIN dbo.PatientMonths PM ON P.PatientKey = PM.PatientKey
AND SCOP.DatePeriodKey = PM.DatePeriodKey



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Mark Dalley
Mark Dalley
SSC Veteran
SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)

Group: General Forum Members
Points: 263 Visits: 1617


Was the Tempdb log previously capped?


No, it wasn't, but it is now (10 GB).

@ChrisM@Work

Number of recs returned by your SELECT version of the query is unknown but large...it has now been running for 28 minutes, no 34 minutes; and is still going!!

What does SELECT PatientKey = P.PatientKey, AgeKey = PM.AgeKey ... mean - in a SELECT query that is? are the assignments just ignored?

Hmm, just looked at the PatienMonths table - it has 47303750 records and occupies 2,469.805 MB data space and 816.430 MB index space!

MarkD
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16010 Visits: 19524
There’s your problem. You’re only updating a maximum of a few tens of thousands of rows in the target table, but the source – now rejigged as a SELECT for this purpose, is returning millions. The SELECT shouldn’t return more than one row per target row. In some RDBMS it would throw an error, and SQL Server is heading in the right direction with MERGE, which won’t allow more than one source row per target row.
Flag it up with the vendor as a lemon, it’s cost you time and money and justifies an immediate rewrite.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Jason-299789
Jason-299789
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2059 Visits: 3232
What does SELECT PatientKey = P.PatientKey, AgeKey = PM.AgeKey ... mean - in a SELECT query that is? are the assignments just ignored?


Its practiacally the same as doing : Select P.PaitentKey AS PatientKey,.......

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Jason-299789
Jason-299789
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2059 Visits: 3232
ChrisM@Work (10/31/2012)
There’s your problem. You’re only updating a maximum of a few tens of thousands of rows in the target table, but the source – now rejigged as a SELECT for this purpose, is returning millions. The SELECT shouldn’t return more than one row per target row. In some RDBMS it would throw an error, and SQL Server is heading in the right direction with MERGE, which won’t allow more than one source row per target row.
Flag it up with the vendor as a lemon, it’s cost you time and money and justifies an immediate rewrite.


+1, The supplier should have someone on hand to deal with this, and I wouldnt accept that the 'relevant' person is on holiday as an excuse either.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Mark Dalley
Mark Dalley
SSC Veteran
SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)SSC Veteran (263 reputation)

Group: General Forum Members
Points: 263 Visits: 1617
I have now killed the SELECT query, which was still churning away after more than an hour.

Many thanks everyone for helping to nail this one. I can now make my case to the vendor in really distinct and specific terms.

You guys rock.

MarkD
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search