SELECT A.Field1 FROM Table1 A INNER JOIN Table1 B
ON A.Field2 = B.Field2
AND A.Field3 = B.Field3
AND A.Field4 = B.Field4
AND B.Field1 = "string1"
AND B.Field2 = "string2"
AND A.Field5 = (SELECT MAX(C.Field5) FROM Table1 C INNER JOIN Table2 D
ON
(C.Field5 < B.Field5
AND B.Field6 = D.Field6
AND C.Field2 = B.Field2
AND C.Field3 = B.Field3
AND C.Field4 = B.Field4
AND D.Field7 = 'Y')
OR
(C.Field5 < B.Field5
AND B.Field6 = D.Field6
AND C.Field2 = B.Field2
AND C.Field3 = B.Field3
AND C.Field4 = B.Field4
AND D.Field7 = 'N')
)

This query takes 0.2 sec in an Oracle database but 4 mins in SQL Server database.
Both the databases have the same indexes, the same records structure and the same column structure.
Can anyone tell me why?
C.Field5 < B.Field5 AND B.Field6 = D.Field6 AND C.Field2 = B.Field2 AND C.Field3 = B.Field3 AND C.Field4 = B.Field4 AND D.field7 IN ('Y', 'N')– Vincent Malgrat Oct 29 '12 at 14:35