goofaholix
MIS
I have a task table that I need to derive for each task which of the previous tasks was the parent.
The parents are found by ranking the tasks by various criteria then joining the task table to the previous task and comparing them. Depending on a series of business rules (compared with the previous task) then if the new task meets those rules it's a parent (called a Placement).
I end up with two tables, one is all the tasks ranked (1 milion rows), the other is the Placements (300,000 rows) with only those tasks that are parents. I thought this would be the hard part done.
I then want to select for each task id the parent id by looking for the max task id that is <= the current task id.
I've tried various options, added indexes, cte's, tables, views. It takes an icredibly long time, 19 hours+.
Here are a couple of queries I've tried, I'm sure there is a better way of doing this but I'm drawing a blank, any ideas?
Insert INTO dbo.PlacementTaskLink
SELECT
MAX(Placement_Id) AS Placement_Id,
Fact_Tasks_Id
FROM
(SELECT
p.Placement_Id,
t.Fact_Tasks_Id
FROM
dbo.TaskResultRanked as t CROSS JOIN
dbo.Placement p
WHERE p.RankRows <= t.RankRows) x
GROUP BY
Fact_Tasks_Id;
Insert INTO dbo.PlacementTaskLink
Select
(Select max(Placement_Id) from Placement p where p.RankRows <= tr.RankRows) as Placement_Id,
Fact_Tasks_Id
from
TaskResultRanked tr
Thanks
Bruce
The parents are found by ranking the tasks by various criteria then joining the task table to the previous task and comparing them. Depending on a series of business rules (compared with the previous task) then if the new task meets those rules it's a parent (called a Placement).
I end up with two tables, one is all the tasks ranked (1 milion rows), the other is the Placements (300,000 rows) with only those tasks that are parents. I thought this would be the hard part done.
I then want to select for each task id the parent id by looking for the max task id that is <= the current task id.
I've tried various options, added indexes, cte's, tables, views. It takes an icredibly long time, 19 hours+.
Here are a couple of queries I've tried, I'm sure there is a better way of doing this but I'm drawing a blank, any ideas?
Insert INTO dbo.PlacementTaskLink
SELECT
MAX(Placement_Id) AS Placement_Id,
Fact_Tasks_Id
FROM
(SELECT
p.Placement_Id,
t.Fact_Tasks_Id
FROM
dbo.TaskResultRanked as t CROSS JOIN
dbo.Placement p
WHERE p.RankRows <= t.RankRows) x
GROUP BY
Fact_Tasks_Id;
Insert INTO dbo.PlacementTaskLink
Select
(Select max(Placement_Id) from Placement p where p.RankRows <= tr.RankRows) as Placement_Id,
Fact_Tasks_Id
from
TaskResultRanked tr
Thanks
Bruce