May 21, 2007 at 8:17 pm
Hi All
Thank in advance for those who reply. I have written a query
select * from TableA
where ID not in
(
select a.ID
from TableA a inner join TableB b on a.ID= b.ID
where (a.TransactionDate between dateadd(dd , -90 , getdate()) and getdate())
)
However, it takes almost 2-3 minutes for the execution of the query How can I rewrite the query using inner join? I had number of tried but fails. Any suggestion will be helpful. Thanks.
May 21, 2007 at 8:58 pm
select * from TableA
LEFT join TableB b on a.ID= b.ID AND (a.TransactionDate between dateadd(dd , -90 , getdate()) and getdate())
WHERE B.ID IS NULL
I'm sure you have clustered index on column a.TransactionDate
_____________
Code for TallyGenerator
May 21, 2007 at 9:46 pm
Thanks for prompt reply.
May 21, 2007 at 11:11 pm
The outer join certainly does the trick but the subquery wasn't the problem... the fact that you made it a correlated subquery may have been.
select * from TableA
where ID not in
(
select a.ID
from TableA a inner join TableB b on a.ID= b.ID
where (a.TransactionDate between dateadd(dd , -90 , getdate()) and getdate())
)
"WHERE NOT IN" actually runs a tiny bit faster than Left Outer's with a NULL detector. 'Course, that won't work if you have more than one column that you need to check.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 21, 2007 at 11:40 pm
Jeff,
select * from TableA
where TransactionDate NOT between dateadd(dd , -90 , getdate()) and getdate()
will be even more faster, but I believe TableB was mentioned in original query on purpose.
Your query will return all entries from TableA not within the time period.
Original (and my) query will return all entries from TableA not within time period OR not referenced in TableB.
That's different.
_____________
Code for TallyGenerator
May 22, 2007 at 12:00 am
Heh... yeah, I crossed out the wrong part, huh? Shouldn't post when I'm pooped. Thanks, Serqiy.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 22, 2007 at 3:11 am
Hi,
I had the same problem when I switched from SQL2K to SQL2K5. I replaced the IN/Subquery part with an EXISTS statement, and it worked perfectly.
select * from TableA
where NOT EXISTS
(
select * from TableA a inner join TableB b on a.ID= b.ID
where (a.TransactionDate between dateadd(dd , -90 , getdate()) and getdate())
AND TableA.ID=a.ID
)
Robert
May 22, 2007 at 5:11 am
My experience in SQL2000 is that joins in subqueries always produce nested loops. I therefore suspect that Sergiy's LEFT JOIN solution, or something like the following, will be more efficient.
SELECT *
FROM TableA
WHERE NOT EXISTS (
SELECT *
FROM TableA a
WHERE TableA.[ID] = a.[ID]
AND a.TransactionDate BETWEEN DATEADD(dd , -90 , GETDATE()) AND GETDATE()
AND EXISTS (
SELECT *
FROM TableB b
WHERE a.[ID] = b.[ID]
)
)
May 22, 2007 at 5:49 am
But, that's more correlated subqueries... why not use a simple join for the WHERE EXISTS?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 22, 2007 at 6:43 am
I was thinking of a left semi join operator which should require less reads than a join; especially if there are a lot of FKs in TableB for each PK in TableA.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply