When should I use cross apply over inner join?

前端 未结 14 1311
忘了有多久
忘了有多久 2020-11-22 06:51

What is the main purpose of using CROSS APPLY?

I have read (vaguely, through posts on the Internet) that cross apply can be more efficient when selectin

14条回答
  •  醉梦人生
    2020-11-22 07:19

    Well I am not sure if this qualifies as a reason to use Cross Apply versus Inner Join, but this query was answered for me in a Forum Post using Cross Apply, so I am not sure if there is an equalivent method using Inner Join:

    Create PROCEDURE [dbo].[Message_FindHighestMatches]
    
    -- Declare the Topical Neighborhood
    @TopicalNeighborhood nchar(255)
    

    AS BEGIN

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON
    
    Create table  #temp
    (
        MessageID         int,
        Subjects          nchar(255),
        SubjectsCount    int
    )
    
    Insert into #temp Select MessageID, Subjects, SubjectsCount From Message
    
    Select Top 20 MessageID, Subjects, SubjectsCount,
        (t.cnt * 100)/t3.inputvalues as MatchPercentage
    
    From #temp 
    
    cross apply (select count(*) as cnt from dbo.Split(Subjects,',') as t1
                 join dbo.Split(@TopicalNeighborhood,',') as t2
                 on t1.value = t2.value) as t
    cross apply (select count(*) as inputValues from dbo.Split(@TopicalNeighborhood,',')) as t3
    
    Order By MatchPercentage desc
    
    drop table #temp
    

    END

提交回复
热议问题