I am trying to get the latest data for a record from multiple tables. The issue I am running into is that the data on each table could have been modified at different times and each table may contain multiple rows per record. I am currently using row number ordered by modified to get the latest record per table then joining them together however this is very slow when each table contains 1 million+ rows.
Example:
WITH cteTable1 AS (
SELECT
Column1,
Column2,
ROW_NUMBER() OVER (PARTITION BY Column1 ORDER BY ModifiedDateTime DESC) AS RowNum
FROM
dbo.Table1
)
,cteTable2 AS (
SELECT
Column1,
Column2,
Column3,
ROW_NUMBER() OVER (PARTITION BY Column1 ORDER BY ModifiedDateTime DESC) AS RowNum
FROM
dbo.Table2
)
,cteTable3 AS (
SELECT
Column1,
Column3,
ROW_NUMBER() OVER (PARTITION BY Column1 ORDER BY ModifiedDateTime DESC) AS RowNum
FROM
dbo.Table3
)
SELECT
*
FROM
cteTable1 t1
LEFT OUTER JOIN cteTable2 t2
ON t1.Column1 = t2.Column1
AND t2.RowNum =1
LEFT OUTER JOIN cteTable2 t3
ON t2.Column2 = t3.Column1
AND t3.RowNum = 1
WHERE
t1.RowNum = 1
This is very inefficient when the number of joins could be 10+ tables and with large tables sizes.