|
-
Jul 18th, 2023, 11:57 AM
#41
Re: Return to very slow query
This is a good read:
https://devblogs.microsoft.com/azure...r-temp-tables/
And again - I'm not sure how CTE's materialize - if that's a memory object or a disk based stored object. And even with TEMP TABLES, which are TEMPDB "stored" - they can still reside in memory alone if small enough.
-
Jul 19th, 2023, 01:38 AM
#42
Re: Return to very slow query
 Originally Posted by szlamany
This is a good read:
https://devblogs.microsoft.com/azure...r-temp-tables/
And again - I'm not sure how CTE's materialize - if that's a memory object or a disk based stored object. And even with TEMP TABLES, which are TEMPDB "stored" - they can still reside in memory alone if small enough.
I don't think CTE a disk based objects, since CTE's are "temp. VIEWs", and a View is, as in that article described, just the definition of the Data queried, not the Data itself.
With CTE (compared to Views) it's even only for the lifetime of the query itself: it creates the definition, it loads the data when called, it discards everything once the end is reached
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jul 19th, 2023, 03:53 AM
#43
Re: Return to very slow query
I have an issue with the CTE Zvoni
I suppose it's an update so it will only update one column but this:
Code:
DECLARE @365DaysAgo DATETIME = DATEADD(DAY, -365, GETDATE());
select C.LoyaltyMemberCode, tblDWPayment.LoyaltyMemberCode,
C.LoyaltyClubID, tblDWLoyaltyMember.LoyaltyClubID
FROM tblDWConcession C
INNER JOIN tblDWPayment ON tblDWPayment.CinemaCode = C.CinemaCode AND tblDWPayment.TransNumber = C.TransNumber AND C.SalesTransCinemaDate > @365DaysAgo AND PaymentDate > @365DaysAgo --AND tblDWPayment.LoyaltyMemberCode IS NOT NULL
INNER JOIN tblDWLoyaltyMember ON tblDWLoyaltyMember.LoyaltyMemberCode = tblDWPayment.LoyaltyMemberCode
WHERE C.LoyaltyMemberCode IS NULL AND (C.IsProcessCompleted = 1 OR C.IsProcessCompleted IS NULL)
produces 4 exact same lines of:
LoyaltyMemberCode LoyaltyMemberCode LoyaltyClubID LoyaltyClubID
NULL xxx 0 10
While this:
Code:
WITH
DA AS (SELECT DateAdd(Year, -1, GetDate()) As OneYearAgo),
C AS (SELECT CinemaCode, TransNumber
FROM tblDWConcession
INNER JOIN DA ON 1=1
WHERE LoyaltyMemberCode IS NULL AND (tblDWConcession.IsProcessCompleted = 1 OR tblDWConcession.IsProcessCompleted IS NULL) AND SalesTransCinemaDate>(DA.OneYearAgo)),
P AS (SELECT T.LoyaltyMemberCode, T.CinemaCode, T.TransNumber, L.LoyaltyClubID
FROM tblDWPayment T
INNER JOIN tblDWLoyaltyMember L ON L.LoyaltyMemberCode=T.LoyaltyMemberCode --Index on both LoyaltyMemberCode
INNER JOIN DA ON 1=1
WHERE T.PaymentDate>(DA.OneYearAgo))
select U.LoyaltyMemberCode, P.LoyaltyMemberCode,
U.LoyaltyClubID, P.LoyaltyClubID
FROM tblDWConcession U
INNER JOIN C ON C.CinemaCode = U.CinemaCode and C.TransNumber= U.TransNumber --I'm implying you have an ID-Field Primary Key
/* Don't forget the combined Index on both columns */
INNER JOIN P ON P.CinemaCode = C.CinemaCode AND P.TransNumber = C.TransNumber;
produces 16 exact same lines of:
LoyaltyMemberCode LoyaltyMemberCode LoyaltyClubID LoyaltyClubID
NULL xxx 0 10
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 19th, 2023, 04:37 AM
#44
Re: Return to very slow query
Crap.
A CROSS JOIN, and i can't see it on first look
EDIT: Just saw it: Why is IsProcessCompleted = 1 here?
In the other query you wrote IsProcessCompleted=0 or IS NULL
Add CinemaCode and TransNumber to the Output of the outer SELECT
SELECT C.CinemaCode, C.TransNumber, U.LoayaltyMemberCode etc....
Last edited by Zvoni; Jul 19th, 2023 at 04:49 AM.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jul 19th, 2023, 04:48 AM
#45
Re: Return to very slow query
It's day to night values. It really should not make a difference at the specific select.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 19th, 2023, 04:49 AM
#46
Re: Return to very slow query
Actually the first query also cross joining as it gives 4 same columns so I was really thinking if that will affect the update or not.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 19th, 2023, 04:50 AM
#47
Re: Return to very slow query
 Originally Posted by sapator
Actually the first query also cross joining as it gives 4 same columns so I was really thinking if that will affect the update or not.
Add CinemaCode and TransNumber to the Output of the outer SELECT
SELECT C.CinemaCode, C.TransNumber, U.LoayaltyMemberCode etc....
Execute two separate Queries:
Use the 3 CTE from above,
execute
SELECT * FROM C
and
SELECT * FROM P
and look at the output
Maybe the duplication happens in the CTE
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jul 19th, 2023, 05:04 AM
#48
Re: Return to very slow query
It's the same cinemacode and transnumber on all lines.
I did not understand the SELECT * FROM C . That will give me all the table of the year
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 19th, 2023, 05:07 AM
#49
Re: Return to very slow query
Just a note.
tblDWConcession actually have 4 lines of the transnumber, so something is happening and we get 4*4 lines.
So the first query will "correctly" give 4 lines but the second one will duplicate * 4
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 19th, 2023, 05:18 AM
#50
Re: Return to very slow query
Possibly the first tblDWConcession on the CTE, joined with the second tblDWConcession U will do 4 on the first CTE * 4 on the second tblDWConcession ?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 19th, 2023, 05:26 AM
#51
Re: Return to very slow query
 Originally Posted by sapator
It's the same cinemacode and transnumber on all lines.
I did not understand the SELECT * FROM C . That will give me all the table of the year
I wanted to see if there are duplicates of CinemaCode and TransNumber
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jul 19th, 2023, 05:27 AM
#52
Re: Return to very slow query
Change to and try again
Code:
C AS (SELECT DISTINCT CinemaCode, TransNumber --here with DISTINCT
FROM tblDWConcession
INNER JOIN DA ON 1=1
EDIT: that's actually what sz was alluding to: With CTE, VIEW or temp table you can test each Subquery for itself, until it returns correct values,
or reveal a mistake you might have.
I'm thinking about those other two Fields in that combined Index for CinemaCode and TransNumber.
Maybe they are needed to really get a unique key
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jul 19th, 2023, 05:36 AM
#53
Re: Return to very slow query
Ye with distinct it will work but isn't that a "group by"? Aren't we decrease performance by doing so?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 19th, 2023, 07:51 AM
#54
Re: Return to very slow query
I was trying with partition by and cross apply etc and the performance is terrible.
Either way in theory CTE looks more light on the execution plan, the update is executed only once on the same selects and I do not see any memory issues, so I think I will have a go for the night batch and see what happens.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 20th, 2023, 02:27 AM
#55
Re: Return to very slow query
Yeah,
Unfortunately the job has not been improved.
I don't think it has to do with the execution plan , something is wrong when running the job.
My last thought is put the query on it's own step but that is just a wild guess.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 20th, 2023, 03:15 AM
#56
Re: Return to very slow query
 Originally Posted by sapator
Yeah,
Unfortunately the job has not been improved.
I don't think it has to do with the execution plan , something is wrong when running the job.
My last thought is put the query on it's own step but that is just a wild guess.
Well, that's called "debugging" 
Yeah, i'd run the UPDATE (Query, whatever) on its own, too, just to see, if it's this Query, or something else
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jul 20th, 2023, 04:22 AM
#57
Re: Return to very slow query
I'm wondering if there is a reason to do a WITH FULLSCAN again. It might get a new execution plain but last time it did not improve the job run. It also takes 3 hours blocking everything so...
Btw this is a tough one , I haven't encountered something similar. Query running fine a tab with any execution plan and failing on the Job run....Well, get irritated with something new every day.
Last edited by sapator; Jul 20th, 2023 at 04:41 AM.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 21st, 2023, 02:07 AM
#58
Re: Return to very slow query
It worked!!
158 seconds instead of 3500 seconds.
I don't know why the job was bloated in a parallel execution but this was clearly lot's of inserts and updates in the same query on a job schedule.
I don't have a clue, I haven't read a document about it, probably all those years trimming with database batch moves triggered the thought.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|