SQL Sub-query or INNER-JOIN?

后端 未结 4 931
执笔经年
执笔经年 2021-01-13 05:38

I\'ve the two following queries:

declare @UserId as int
set @UserId = 1

-- Query #1: Sub-query
SELECT
    u.[Id] ,
    u.[Name] ,
    u.[OrgId] AS Organizat         


        
4条回答
  •  余生分开走
    2021-01-13 06:37

    The relative cost of an execution plan isn't always a reliable indicator of performance.

    I assume from your sql that only 1 row should be returned. Providing that the UserId is a unique key on User, then the performance of your 2 approaches will be similar on most relational databases.

    Things to bear in mind would be:

    • if UserPreferences or SearchCriteria return more than 1 row, the first approach will raise an sql error, the second approach will return more than 1 row.
    • the apparent extra lookup in the first approach (UserPreferences selected twice) has no real effect because for the second lookup the record will already be in a buffer
    • if for some reason the User table is tablespace scanned, the first approach will be much faster

提交回复
热议问题