Skip to content Skip to sidebar Skip to footer

Which Approach Is Better To Retrieve Data From A Database

I am confused about selecting two approaches. Scenario there are two tables Table 1 and Table 2 respectively. Table 1 contains user's data for example first name, last name etc Tab

Solution 1:

Your two approaches will have about the same performance (slow because of N+1 queries). It would be faster to do a single query like this:

select *
from T1
left join T2 on ...
orderby T1.PrimaryKey

Your client app can them interpret the results and have all data in a single query. An alternative would be:

select *, 1as Tag
from T1
union all
select *, 2as Tag
from T2
orderby T1.PrimaryKey, Tag

This is just pseudo code but you could make it work.

The union-all query will have surprisingly good performance because sql server will do a "merge union" which works like a merge-join. This pattern also works for multi-level parent-child relationships, although not as well.

Post a Comment for "Which Approach Is Better To Retrieve Data From A Database"