#title 사용자 정의함수 병렬처리 문제 다음과 같이 함수를 만들고.. {{{ create function dbo.ufn_game_weekly (@bdt date, @edt date) returns table as return ( select 게임키 , sum(게임머니) 게임머니 from 팩트테이블 where 날짜 between @bdt and @edt group by 게임키 ) go select dbo.ufn_game_weekly ('20120801', '20120807') --이건 병렬처리한다. }}} 위에 함수를 이용하여 다음과 같이 주차별로 cross apply 했을 때, 병렬처리가 안되는 경우가 있다. 이렇게 함수를 이용한 이유는 group by 할 때 하나의 컬럼만으로 group 으로 묶기 위해서다. 불필요한 컬럼을 group by 절에서 없애기 위함이다. {{{ select a.begin_dt , a.end_dt , b.게임키 , b.게임머니 from ( select distinct convert(date, DATEADD(dd, CONVERT(int, convert(datetime, date_key), 112) % 7 * -1, date_key)) begin_dt , convert(date, DATEADD(dd, 7 - (CONVERT(int, convert(datetime, date_key), 112) % 7 + 1 ), date_key)) end_dt from dm.dim.date a --이건 날짜 테이블 where 1=1 and date_key between convert(char(8), DATEADD(dd, CONVERT(int, convert(datetime, '20120801'), 112) % 7 * -1, '20120801'), 112) and convert(char(8), DATEADD(dd, (CONVERT(int, convert(datetime, '20120904'), 112) % 7 + 1) * -1, '20120904'), 112) ) a cross apply reader.dbo.ufn_game_weekly(begin_dt, end_dt) b go }}} 이런 경우에 다음과 같이 함수 내부에서 xml로 결과를 만들고 리턴했다. 그러니 병렬처리 되더라. 테이블의 통계가 최신인지 아닌지는 확인하지 않았다. {{{ --drop function dbo.ufn_game_weekly create function dbo.ufn_game_weekly (@bdt date, @edt date) returns @rs table ( 게임키 int , 게임머니 bigint ) as declare @x xml set @x = ( select 게임키 , sum(게임머니) 게임머니 from 팩트테이블 where 날짜 between @bdt and @edt group by 게임키 for xml path('row') ) insert @rs select y.item.value('(게임키)[1]', 'smallint') , y.item.value('(게임머니)[1]', 'bigint') from (select @x xitem) x cross apply x.xitem.nodes('/row') as y(item) return; go }}} 씨발 씨발 하면서 노가다로 찾은 솔루션이다.