데이터 엔지니어 기술 블로그

[⚡AWS] Redshift 상관관계가 있는 서브 쿼리 오류 본문

AWS

[⚡AWS] Redshift 상관관계가 있는 서브 쿼리 오류

jun_yeong_park 2021. 5. 27. 14:43
반응형

개요

This type of correlated subquery pattern is not supported due to internal error

레드시프트에서 쿼리를 보낼 때 위와 같은 에러를 반환할 때가 있다. Redshift는 쿼리 플래너에서 쿼리 재작성을 이용하여 상관관계가 있는 패턴 등을 최적화한다. MySQL과는 처리해주는 방식이 다르기 때문에 지원하지 않는 상관관계를 가진 서브쿼리들이 있다.

 

지원하지 않는 서브쿼리

1번) 건너뛰기 수준의 상관관계 참조

event 테이블은 가장 상위 레벨에 있으나 가장 하위의 서브쿼리에 event 테이블이 상관관계로 사용되고 있다.

 

select event.eventname
from event
where not exists (
        select *
        from listing
        where not exists (
                select *
                from sales
                where event.eventid = sales.eventid
            )
    );

 

2번) ON절의 일부로 서브쿼리에서 상관관계 참조

select *
from category
    left join event on category.catid = event.catid
    and eventid = (
        select max(eventid)
        from sales
        where sales.eventid = event.eventid
    );

 

3번) Null에 민감한 상관관계 참조

select attrelid
from stv_locks sl,
    pg_attribute
where sl.table_id = pg_attribute.attrelid
    and 1 not in (
        select 1
        from pg_opclass
        where sl.lock_owner = opcowner
    );

 

4번) 윈도우 함수를 포함하는 서브쿼리 내에서 상관관계 참조

select listid,
    qtysold
from sales s
where qtysold not in (
        select sum(numtickets) over()
        from listing l
        where s.listid = l.listid
    );

 

5번) GROUP BY 열에서 상관관계를 가진 서브쿼리 결과를 참조

select listing.listid,
    (
        select count (sales.listid)
        from sales
        where sales.listid = listing.listid
    ) as list
from listing
group by list,
    listing.listid;

 

6번) IN 조건으로 집계 함수(MIN, MAX 제외) 및 GROUP BY 절이있는 서브쿼리의 상관 관계 참조

select *
from listing
where listid in (
        select sum(qtysold)
        from sales
        where numtickets > 4
        group by salesid
    );

 

반응형
Comments