다음을 통해 공유


외부 조인 구현

외부 조인은 고유하게 컴파일된 저장 프로시저에서 지원되지 않습니다. 다음 샘플에서는 고유하게 컴파일된 저장 프로시저에서 왼쪽 외부 조인의 기능을 구현하는 방법을 보여줍니다.

샘플은 테이블 변수를 사용하여 조인의 왼쪽에 있는 커서를 시뮬레이션하고 테이블 변수를 사용하여 단일 결과 집합을 생성합니다. 이는 데이터 행의 추가 복사본을 만드는 것과 관련된 제한된 수의 행을 처리할 때만 적합합니다.

t1_type 형식의 변수(@outer)는 while 루프를 사용하여 커서를 시뮬레이트하는 t1의 행을 반복하는 데 사용됩니다. 그런 다음 t1t2_join_type 형식의 변수 @result 를 사용하여 결과 집합을 생성합니다.

이 해결 방법의 성능을 테스트하여 애플리케이션에서 예상대로 작동하는지 확인해야 합니다.

-- original query:  
select   
   t1.c1 as t1c1,  
   t1.c2 as t1c2,  
   t2.c2 as t2c2,  
   t2.c3 as t2c3  
   from t1 left join t2 on t1.c2=t2.c3  
GO  
  
create table dbo.t1  
(c1 int not null primary key nonclustered,  
c2 int not null) with (memory_optimized=on)  
  
create table dbo.t2  
(c2 int not null primary key nonclustered,  
c3 int not null) with (memory_optimized=on)  
  
INSERT t1 VALUES (1,2)  
INSERT t1 VALUES (2,3)  
INSERT t1 VALUES (3,2)  
INSERT t2 VALUES (2,3)  
INSERT t2 VALUES (4,3)  
GO  
  
create type dbo.t1_type as table  
(  
   id int identity not null primary key nonclustered hash with (bucket_count=1024),  
   c1 int,  
   c2 int  
) with (memory_optimized=on)  
GO  
  
create type dbo.t1t2_join_type as table  
(  
   t1c1 int not null index ix_t1c1,  
   t1c2 int not null,  
   t2c2 int,  
   t2c3 int  
) with (memory_optimized=on)  
GO  
  
-- ====== scenario: generic left join  
  
-- stored procedure including the workaround  
create procedure dbo.usp_left_join  
with native_compilation, execute as owner, schemabinding  
as  
begin atomic with (transaction isolation level = snapshot, language = N'us_english')  
  
   DECLARE @outer dbo.t1_type  
   DECLARE @result dbo.t1t2_join_type  
  
   -- populate the variable used for iterating over the outer rows  
   INSERT @outer(c1, c2) select c1,c2 from dbo.t1  
  
   DECLARE @i int = 1  
   DECLARE @max int = scope_identity()  
  
   DECLARE @t1c1 int  
   DECLARE @t1c2 int  
  
   while @i <= @max  
   begin     
      select @t1c1 = c1, @t1c2 = c2 from @outer where id = @i  
  
      INSERT @result select @t1c1, @t1c2, c2, c3 from dbo.t2 where c3 = @t1c2  
  
      if @@rowcount = 0   
         INSERT @result (t1c1, t1c2) VALUES (@t1c1, @t1c2)  
  
      set @i += 1  
   end  
  
   select   
      t1c1,  
      t1c2,  
      t2c2,  
      t2c3  
   from @result  
end  
GO  
  
exec dbo.usp_left_join  

또한 참조하십시오

네이티브 컴파일된 저장 프로시저의 마이그레이션 문제
메모리 내 OLTP에서 지원되지 않는 Transact-SQL 구문.