I ran into a rather strange problem. I created the following query in SQL Server
SELECT * FROM leads.BatchDetails T1 INNER JOIN leads.BatchHeader h ON T1.LeadBatchHeaderId = h.ID WHERE T1.LeadBatchHeaderId = 34 AND (T1.TypeRC = 'R' OR h.DefaultTypeRC = 'R') AND EXISTS (SELECT ID FROM leads.BatchDetails T2 where T1.FirstName = T2.FirstName AND T1.LastName = T2.LastName AND T1.Address1 = T2.Address1 AND T1.City = T2.City AND T1.[State] = T2.[State] AND T1.Zip5 = T2.Zip5 AND T1.LeadBatchHeaderId = T2.LeadBatchHeaderId and t2.ID < t1.ID AND (T2.TypeRC = 'R' OR h.DefaultTypeRC = 'R' ) )
It works pretty fast in 2 seconds. When formatting the code, I accidentally added an extra SPACE between AND + EXISTS , so the query looks like this.
SELECT * FROM leads.BatchDetails T1 INNER JOIN leads.BatchHeader h ON T1.LeadBatchHeaderId = h.ID WHERE T1.LeadBatchHeaderId = 34 AND (T1.TypeRC = 'R' OR h.DefaultTypeRC = 'R') AND EXISTS (SELECT ID FROM leads.BatchDetails T2 where T1.FirstName = T2.FirstName AND T1.LastName = T2.LastName AND T1.Address1 = T2.Address1 AND T1.City = T2.City AND T1.[State] = T2.[State] AND T1.Zip5 = T2.Zip5 AND T1.LeadBatchHeaderId = T2.LeadBatchHeaderId and t2.ID < t1.ID AND (T2.TypeRC = 'R' OR h.DefaultTypeRC = 'R' ) )
As a result, the request takes 13 seconds. I am running SQL Server in a sandbox sandbox, and I even tested it on a different sandbox. I also checked the completed request in the profiler, the reading has not changed much, but the processor time is up.
If it is not too strange, it becomes more strange. When I change SELECT * FROM to SELECT Field1, ... FROM at the top of the query, it takes more than 3 minutes to complete.
I have been working with SQL Server for 10 years and have never seen anything like it.
Edit: Following the recommendations below, you will see that queries are "space-sensitive." However, I still don't know why SELECT * FROM is much faster than SELECT Field1, ... FROM
Andre source share