You seem to think that your main request involves the following steps:
(1) Run the subquery (2) Check each row in table1 against the result set from the subquery.
Therefore, you think that executing a subquery separately should take less time than executing the entire query.
But SQL is not a procedural language, and the structure of a query does not necessarily imply the steps that will be taken to execute the query.
According to Guff, the optimizer will come up with (which, in his opinion,) an optimal plan for fulfilling each request. These execution plans are not always obvious when viewing the request, and in some cases can indeed be completely contradictory.
I think that in this case it is more likely that the optimizer came up with a faster method of checking whether a value exists in table2 than just querying the whole table2 at once. This may be the transformation that Guff demonstrated (although this still does not mean that a specific implementation plan is being used).
I would suggest that table1 has a significantly smaller number of rows than table2, and the index exists in table2.columnB. So all you have to do is get the rows from table1, and then examine the index for each of these values ββto check for existence. But this is only one possibility.
In addition, as Michael Buen noted, differences in the size of the returned result set can also affect your perceived performance. My intuition is that this is secondary to the differences in the implementation plan, but can be significant.
source share