I'm needing some help optimizing a query. The following simplified query returns the results that I need, but the performance is terrible. Here is a simplified view of my data:
Table: BillRequests
BillRequestID
-------------
1
2
3
4
Table: ActivityHistory
BillRequestID ActivityCode Position
------------- ------------ --------
1 1 1
1 6 2
1 8 3
2 3 1
2 6 2
So, I have a Master/Detail relationship and I want a single row for each record in the BillRequests table, even if there is no record with the specified ActivityCode in the Activity History table. I think if I can update my WHERE clause to prune more records out earlier, my performance will improve and I can get rid of the DISTINCT keyword. I have about 30,000 records in the BillRequests table and about 350,000 in the ActivityHistory table and the ultimate use of the query will be to define a View. Once I have created the View, the queries on it take a VERY long time. I have not really used the NOT EXISTS functionality before, so I don't know if I am using it properly or if there is some better way.
Here is my simplified query:
select distinct BR.BillRequestID,
case when Exists(select * from ActivityHistory AH
where (AH.BillRequestID = BR.BillRequestID) and (ActivityCode = 6))
then AH2.SentToID
else NULL
end as EditorID
from BillRequests BR
left outer join ActivityHistory AH2 on AH2.BillRequestID = BR.BillRequestID
where
(
(Not Exists(select * from ActivityHistory AH
where (AH.BillRequestID = BR.BillRequestID) and (AH.ActivityCode = 6)))
or
(AH2.Position = ( select max(AH.Position) from ActivityHistory AH
where (AH.BillRequestID = BR.BillRequestID) and (AH.ActivityCode = 6)))
)
Any ideas on how I can get rid of the DISTINCT and still only have 1 row per record in my BillRequest table?
Thanks - RC
Table: BillRequests
BillRequestID
-------------
1
2
3
4
Table: ActivityHistory
BillRequestID ActivityCode Position
------------- ------------ --------
1 1 1
1 6 2
1 8 3
2 3 1
2 6 2
So, I have a Master/Detail relationship and I want a single row for each record in the BillRequests table, even if there is no record with the specified ActivityCode in the Activity History table. I think if I can update my WHERE clause to prune more records out earlier, my performance will improve and I can get rid of the DISTINCT keyword. I have about 30,000 records in the BillRequests table and about 350,000 in the ActivityHistory table and the ultimate use of the query will be to define a View. Once I have created the View, the queries on it take a VERY long time. I have not really used the NOT EXISTS functionality before, so I don't know if I am using it properly or if there is some better way.
Here is my simplified query:
select distinct BR.BillRequestID,
case when Exists(select * from ActivityHistory AH
where (AH.BillRequestID = BR.BillRequestID) and (ActivityCode = 6))
then AH2.SentToID
else NULL
end as EditorID
from BillRequests BR
left outer join ActivityHistory AH2 on AH2.BillRequestID = BR.BillRequestID
where
(
(Not Exists(select * from ActivityHistory AH
where (AH.BillRequestID = BR.BillRequestID) and (AH.ActivityCode = 6)))
or
(AH2.Position = ( select max(AH.Position) from ActivityHistory AH
where (AH.BillRequestID = BR.BillRequestID) and (AH.ActivityCode = 6)))
)
Any ideas on how I can get rid of the DISTINCT and still only have 1 row per record in my BillRequest table?
Thanks - RC