[AWS] AWS Redshift 실행된 쿼리 Slow query 확인

 

l  Version : AWS Redshift, Redshift Serverless

 

AWS Redshift에서 특정 시간이상 실행된 쿼리 또는 사용자에 의한 취소 쿼리 목록 실행시간, 실행된 쿼리 등을 확인하는 방법에 대해서 알아본다.

 

아래 스크립트는 3 이상 실행된 쿼리 목록을 확인하여, WHERE 부분을 수정하여 사용자에 필요한 시간으로 변경하여 사용할 있다. 조회 결과는 오래 수행된 쿼리의 내림 차순으로 표시되며, S3 spectrum 사용할 경우 외부 테이블에 대한 사용 정보도 함께 나태낸다. 쿼리 결과에서 elapsed_time 전체 쿼리가 수행에 걸린 시간을 나타내므로 해당 시간이 클수록 느린 쿼리라고 판단할 있다.

select
        a.query_id,
        a.database_name,
        a.query_type,
        a.start_time,
        a.end_time,
        (a.elapsed_time * 1.0) / 1000 / 1000 as elapsed_time_sec,
        (a.execution_time * 1.0) / 1000 / 1000 as execution_time_sec,
        a.returned_rows,
        a.returned_bytes,
        a.query_text,
        b.source_type, -- Spectrum : S3, 연합쿼리 : PG
        b.duration as external_query_duration,
        b.total_partitions as s3_partition,
        b.qualified_partitions as s3_scan_partiton,
        b.scanned_files as s3_scan_file,
        b.returned_rows as s3_returned_rows,
        b.returned_bytes as s3_returned_bytes,
        b.file_format as s3_file_formant,
        b.file_location as s3_file_location,
        b.external_query_text as external_query_text,
        a.result_cache_hit,
        (a.queue_time * 1.0) / 1000 /1000  as queue_time_sec,
        a.status,
        a.error_message,
        a.user_id,
        a.transaction_id,
        a.session_id
from SYS_QUERY_HISTORY as a
        left outer join SYS_EXTERNAL_QUERY_DETAIL as b on a.query_id = b.query_id
where (a.elapsed_time * 1.0) / 1000 / 1000 > 3 -- 마이크로세컨을 세컨으로 계산하도록 변경. 숫자 변경하여 사용
        and a.status in ('success', 'canceled') -- 사용자 쿼리가 성공 또는 사용자에 의한 취소 쿼리만 조회
/* 사용 가능 status value
planning, queued, running, returning, failed, canceled, success
*/
        and a.start_time >= '2023-01-11 01:00' -- UTC 시간
        and a.end_time <= '2023-01-11 23:00' -- UTC 시간
order by elapsed_time desc

 

[참고 자료]

l  쿼리 계획 : https://docs.aws.amazon.com/ko_kr/redshift/latest/dg/c-the-query-plan.html

l  쿼리 요약 분석 : https://docs.aws.amazon.com/ko_kr/redshift/latest/dg/c-analyzing-the-query-summary.html

l  쿼리 계획 분석 : https://docs.aws.amazon.com/ko_kr/redshift/latest/dg/c-analyzing-the-query-plan.html

l  쿼리 히스토리 : https://docs.aws.amazon.com/ko_kr/redshift/latest/dg/SYS_QUERY_HISTORY.html

l  외부 쿼리 상세 보기 : https://docs.aws.amazon.com/ko_kr/redshift/latest/dg/SYS_EXTERNAL_QUERY_DETAIL.html

l  쿼리 세부 정보 : https://docs.aws.amazon.com/ko_kr/redshift/latest/dg/SYS_QUERY_DETAIL.html

l  서버리스 사용량 확인 : https://docs.aws.amazon.com/ko_kr/redshift/latest/dg/SYS_SERVERLESS_USAGE.html

 

 

 

2023-02-19 / Sungwook Kang / http://sungwookkang.com

 

 

AWS, Redshift, Serverless, SYS_QUERY_HISTORY, SlowQuery, 슬로우 쿼리 확인

[AWS] AWS Redshift Serverless 사용량을 확인하여 빌링비용 예상하기

 

l  Version : AWS Redshift Serverless

 

AWS Redshift Serverless 사용한 RPU (Redshift Serverless 리소스 단위로 RPU 라는 것을 사용한다.) 만큼의 비용이 발생하는 구조이다. 그렇다면 사용자가 쿼리를 실행하였을 , 얼마만큼의 RCU 사용하였는지 확인하여 예상 빌링 비용을 산출하는 방법에 대해서 알아본다.

 

AWS Redshift Serverless 환경에서 쿼리를 실행을 완료한 다음, SYS_SERVERLESS_USAGE 라는 시스템 테이블을 확인하면, 서버가 실행된 시간과 사용된 리소스 사용량을 확인할 있다.

select * from sys_serverless_usage

 

 

이름 데이터 형식 설명
start_time timestamp 시작 시간
end_time timestamp 완료된 시간
compute_seconds double precision 시간 동안 사용된 누적 컴퓨팅 단위(RPU) 이며, 유휴 시간이 포함되어 있음.
compute_capacity double precision 시간 동안 할당된 평균 컴퓨팅 단위(RPU)
data_storage integer 시간 동안 사용된 평균 데이터 스토리지 공간(MB) 으로 사용된 데이터 스토리지는 데이터가 데이터베이스에서 로드되거나 삭제될 동적으로 변경 있음.
cross_region_transferred_data integer 시간 동안 리전간 데이터 공유를 위해 전송된 누적 데이터(byte) .

 

아래 스크립트는 1 사용량에 따른 비용을 계산하는 예제이다. SYS_SERVERLESS_USAGE 시스템 테이블을 쿼리하여 사용량을 추적할 있으며, 쿼리가 처리된 기간의 근사값을 계산할 있다. 현재 서비스 중인 리전의 RPU 가격을 확인하여 대입하면 된다. 아래 스크립트에서는 서울 리전의 RPU 가격을 대입한 결과 이다.

Select
  trunc(start_time) "Day",
  sum(compute_seconds)/60/60 as sum_compute_seconds,
  (sum(compute_seconds)/60/60) * 0.438 as price -- <Price for 1 RPU>
from sys_serverless_usage
group by trunc(start_time)
order by 1

 

 

Redshift Serverless 사용한 만큼 비용이 발생하는 구조이지만, 쿼리가 실패 하였을 경우에는 비용이 발생하지 않는다. 하지만 사용자 요청에 의해 취소되는 작업에 대해서는 취소될 까지 사용된 리소스에 대해서는 비용이 발생한다. 따라서, 실행중인 쿼리를 캔슬하거나 쿼리 타임아웃으로 인해 쿼리가 취소 되지 않도록 시간을 적절히 조절하여 의도하지 않은 불필요한 비용이 발생하지 않도록 한다.

 

Redshift에서 S3 저장된 데이터를 직접 수행할 있도록 하는 Amazon Redshift Spectrum 사용할 경우, 스캔한 바이트 수에 대해 비용이 부과되며 10MB 기준으로 반올림하여 처리 된다. DDL문에 대해서는 파티셔닝 관리와 실패한 쿼리에 대해서는 비용이 부과되지 않는다.

 

AWS S3에서는 Amazon Redshift Serverless 외부 데이터 쿼리는 별도로 청구되지 않으며, Amazon Redshift 서버리스에 대한 청구 금액(RPU) 포함되어 있다.

 

 

[참고자료]

l  https://docs.aws.amazon.com/ko_kr/redshift/latest/dg/SYS_SERVERLESS_USAGE.html

l  https://aws.amazon.com/ko/redshift/pricing/

 

 

 

2023-01-09 / Sungwook Kang / http://sungwookkang.com

 

 

AWS, Redshift, Serverless, SYS_SERVERLESS_USAGE

+ Recent posts