DT4K
Diamond Member
OK, I've got two versions of a query. Seems to me like both should take exactly the same amount of time to run. In fact, one runs in about 4 seconds and the other takes 6 minutes to complete. Both of them return the same records. About 40 records get returned. The only difference is that one uses >= some date and the other uses between some date and now.
Here is the one that takes 4 seconds:
select distinct t.TestName, t.NumericOrText, t.testgroup
from tblTests t join tblQualityData qd
on t.TestName = qd.TestName
where TestGroup <> 'BatchTags'
and ItemDataAutoID in
(select ItemDataAutoID
from tblItemData
where (EquipmentID like '622010%' or EquipmentID like '%MakeToBatch%')
and StartDateTime between '2005-02-21 10:13:00' and getdate())
order by t.testgroup, t.TestName
Here is the one that takes 6 minutes:
select distinct t.TestName, t.NumericOrText, t.testgroup
from tblTests t join tblQualityData qd
on t.TestName = qd.TestName
where t.TestGroup <> 'BatchTags'
and qd.ItemDataAutoID in
(select ItemDataAutoID
from tblItemData
where (EquipmentID like '622010%' or EquipmentID like '%MakeToBatch%')
and StartDateTime >= '2005-02-21 10:13:00')
order by t.testgroup, t.TestName
Notice that the only difference between the two is in the last line of the subquery. Now the part that really throws me off is that if I run each of those subqueries independently, they both execute instantly and return about 14 ItemDataAutoID's. So from what I can see, if both subqueries return the same thing in the same amount of time and everything except the subquery is identical, then both queries should take the same amount of time. Obviously I'm missing something. The only thing I could think of is that maybe for some reason, the second version is resulting in an unintentional correlated subquery, but I can't figure out why that would be.
Any help would be greatly appreciated.
Here is the one that takes 4 seconds:
select distinct t.TestName, t.NumericOrText, t.testgroup
from tblTests t join tblQualityData qd
on t.TestName = qd.TestName
where TestGroup <> 'BatchTags'
and ItemDataAutoID in
(select ItemDataAutoID
from tblItemData
where (EquipmentID like '622010%' or EquipmentID like '%MakeToBatch%')
and StartDateTime between '2005-02-21 10:13:00' and getdate())
order by t.testgroup, t.TestName
Here is the one that takes 6 minutes:
select distinct t.TestName, t.NumericOrText, t.testgroup
from tblTests t join tblQualityData qd
on t.TestName = qd.TestName
where t.TestGroup <> 'BatchTags'
and qd.ItemDataAutoID in
(select ItemDataAutoID
from tblItemData
where (EquipmentID like '622010%' or EquipmentID like '%MakeToBatch%')
and StartDateTime >= '2005-02-21 10:13:00')
order by t.testgroup, t.TestName
Notice that the only difference between the two is in the last line of the subquery. Now the part that really throws me off is that if I run each of those subqueries independently, they both execute instantly and return about 14 ItemDataAutoID's. So from what I can see, if both subqueries return the same thing in the same amount of time and everything except the subquery is identical, then both queries should take the same amount of time. Obviously I'm missing something. The only thing I could think of is that maybe for some reason, the second version is resulting in an unintentional correlated subquery, but I can't figure out why that would be.
Any help would be greatly appreciated.