Is there any difference in terms of performance between these two versions of the same query?
--Version 1
SELECT p.Name, s.OrderQty
FROM Product p
INNER JOIN SalesOrderDetail s on p.ProductID = s.ProductID
--Version 2
SELECT p.Name, s.OrderQty
FROM Product p
INNER JOIN (SELECT ProductID, OrderQty FROM SalesOrderDetail) s on p.ProductID = s.ProductID
I've heard it said (by a DBA) that Version 2 is faster because it fetches, within the inner SELECT statement, only the columns that are required for the query. But that doesn't seem to make sense, since query performance (as I know) is based on number of rows affected and final list of columns returned.
The query plans for both are identical, so I'm guessing there isn't any difference between the two.
Am I correct?
INNER JOIN table
will fetch all columns, whileINNER JOIN (SELECT column1, column2, ...)
will only fetch specified columns and has less data width than first one. But this only applies to PostgreSQL 9.5.6 and not yours. Fire that DBA :P – Paolapaolina