Please have these two types of query in your mind:
--query1
Select someFields
From someTables
Where someWhereClues
Union all
Select someFields
FROM some Tables
Where someWhereClues
--query2
Select * FROM (
Select someFields
From someTables
Union all
Select someFields
FROM someTables
) DT
Where someMixedWhereClues
Note :
In both queries final result fields are same
I thought the 1st. query is faster or its performance is better!
But after some researches I confused by this note:
SQL Server (as a sample of RDBMS) first reads whole data then seek records. => so in both queries all records will read and seek.
Please Help me on my misunderstandings, and on if there is any other differences between query1 and query2 ?
Edit: adding sample plans:
select t.Name, t.type from sys.tables t where t.type = 'U'
union all
select t.Name, t.type from sys.objects t where t.type = 'U'
select * from (
select t.Name, t.type from sys.tables t
union all
select t.Name, t.type from sys.objects t
) dt
where dt.type = 'U'
Execution Plans are:
both are same and 50%