What is the main purpose of using CROSS APPLY ? (使用CROSS APPLY的主要目的是什么?)
I have read (vaguely, through posts on the Internet) that cross apply
can be more efficient when selecting over large data sets if you are partitioning. (我已经读过(模糊地通过Internet上的帖子),如果您正在分区,则在选择大型数据集时, cross apply
会更有效。) (Paging comes to mind) ((想起分页))
I also know that CROSS APPLY
doesn't require a UDF as the right-table. (我也知道, CROSS APPLY
不需要UDF作为右表。)
In most INNER JOIN
queries (one-to-many relationships), I could rewrite them to use CROSS APPLY
, but they always give me equivalent execution plans. (在大多数INNER JOIN
查询(一对多关系)中,我可以重写它们以使用CROSS APPLY
,但是它们总是给我等效的执行计划。)
Can anyone give me a good example of when CROSS APPLY
makes a difference in those cases where INNER JOIN
will work as well? (在CROSS APPLY
在INNER JOIN
也能正常工作的情况下, CROSS APPLY
时,谁能给我一个很好的例子?)
Edit: (编辑:)
Here's a trivial example, where the execution plans are exactly the same. (这是一个简单的示例,其中执行计划完全相同。) (Show me one where they differ and where cross apply
is faster/more efficient) ((向我展示它们的不同之处和cross apply
的更快/更有效的地方))
create table Company (
companyId int identity(1,1)
, companyName varchar(100)
, zipcode varchar(10)
, constraint PK_Company primary key (companyId)
)
GO
create table Person (
personId int identity(1,1)
, personName varchar(100)
, companyId int
, constraint FK_Person_CompanyId foreign key (companyId) references dbo.Company(companyId)
, constraint PK_Person primary key (personId)
)
GO
insert Company
select 'ABC Company', '19808' union
select 'XYZ Company', '08534' union
select '123 Company', '10016'
insert Person
select 'Alan', 1 union
select 'Bobby', 1 union
select 'Chris', 1 union
select 'Xavier', 2 union
select 'Yoshi', 2 union
select 'Zambrano', 2 union
select 'Player 1', 3 union
select 'Player 2', 3 union
select 'Player 3', 3
/* using CROSS APPLY */
select *
from Person p
cross apply (
select *
from Company c
where p.companyid = c.companyId
) Czip
/* the equivalent query using INNER JOIN */
select *
from Person p
inner join Company c on p.companyid = c.companyId
ask by Jeff Meatball Yang translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…