As I mentioned in a previous post I am no database programmer, I just got thrown in at the deep end. As I was working on this project a coworker, who is an AS/400 guru, suggested a query structure that eliminates the need for joins and is supposed to result in quicker, less resource-intensive queries. Below are before and after examples. Both complete in 26 seconds, returning just under 9,000 rows from two 5 million + row tables.
BTW, the MAX(h.DateChanged) is intended to get me just the most recent record for each AccountID. Is how I did that correct for what I want?
With Join
Without Join
BTW, the MAX(h.DateChanged) is intended to get me just the most recent record for each AccountID. Is how I did that correct for what I want?
With Join
Code:
SELECT h.AccountID,
MAX(h.DateChanged) AS DateChanged,
m.[status] AS CURRENTSTATUS,
h.OldStatus,
h.NewStatus,
GETDATE() AS RECORDDATE,
FROM dbo.StatusHistory h
INNER JOIN dbo.[master] m
ON h.AccountID = m.[number]
WHERE (h.OldStatus <> h.NewStatus)
AND (h.OldStatus <> m.[status])
AND (h.OldStatus IN ('CBD', 'CYC', 'DCC', 'FRD')
OR h.NewStatus = 'NSF')
GROUP BY h.AccountID,
h.OldStatus,
h.NewStatus,
h.DateChanged,
m.[status]
ORDER BY h.DateChanged DESC
Code:
SELECT h.AccountID,
MAX(h.DateChanged) AS DateChanged,
m.[status] AS CURRENTSTATUS,
h.OldStatus,
h.NewStatus,
GETDATE() AS RECORDDATE,
FROM dbo.StatusHistory h, dbo.[master] m
WHERE h.AccountID = m.[number]
AND (h.OldStatus <> h.NewStatus)
AND (h.OldStatus <> m.[status])
AND (h.OldStatus IN ('CBD', 'CYC', 'DCC', 'FRD')
OR h.NewStatus = 'NSF')
GROUP BY h.AccountID,
h.OldStatus,
h.NewStatus,
h.DateChanged,
m.[status]
ORDER BY h.DateChanged DESC