Redshift Specturm: Create table with sortkey and distkey from a union all query

0

Hey there,

I have a create table query with sortkey and distkey defined, the source data comes from s3. The query looks like this

create  table "database_name"."schema_name"."new_table_name"
    diststyle key distkey (id)
    compound sortkey(event_at)
as (
     SELECT
        id
        , event_at
        , ...
    FROM "database_name"."schema_name"."source_table_a"
    WHERE ....
    
    UNION ALL
    
     SELECT
        id
        , event_at
        , ...
    FROM "database_name"."schema_name"."source_table_a"
    WHERE ....
  )

When I run the query, I always receive database error: Table database_name_schema_name_source_table_a_60bb138c76a18 not found. This 60bb138c76a18 suffix is always different when I run the query.

It works when:

  • remove sort key and dist key from table creation
  • remove union all

Does anyone experience the same issue? Thanks in advacne.

  • I'm not sure why this particular error is happening but I'm curious as to why you are using UNION ALL. Scanning that external table twice will take take twice as long and cost twice as much. Can you remove the UNION ALL and use CASE statements instead? But if you can't resolve this with enhancing the SQL, you should open a support case.

  • Hi,

    Have you tried to execute the SELECT query with UNION ALL without the CREATE TABLE?

    Regards,

  • Hey @Ziad, Yes, with table creation, query works fine.

  • Hey Jon, This is a simplified query, I remove some complexity to better focus on the issue. Thanks for your input.

질문됨 5달 전212회 조회
1개 답변
1

Hi,

I have tested multiple similar use cases (CTAS + sortkey + distkey + union all + Spectrum tables). They executed successfully without any error message. I recommend in this case to open a support ticket for further analysis so you can share with the support team the real query.

Regards,

AWS
전문가
Ziad
답변함 5달 전
  • Hey Ziad, thanks a lot for testing out. Seems like something specific only for my certain table. I will reach out to support team.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠