AWS Athena 쿼리 실행 시 일부 쿼리에서 Limit 무시하는 현상에 대해

728x90

AWS Athena

AWS Athena에서 쿼리를 실행할 때 쿼리에 Limit을 추가할 때 limit을 무시하고 쿼리가 출력되거나 의도하지 않은 값이 리턴되는 경우가 있어서 찾아본 결과. 아래와 같은 답변이 보여서 공유합니다.

 

예컨대 Athena는 쿼리 실행 시, 쿼리 동작을 계획할 동안 파티션된 파일을 목록화(list) 하는데 이 시점에서 파일의 Row 개수를 정확히 알지 못하기 때문에 쿼리를 실행해서 관련 된 모든 데이터를 읽은 다음 10개 째의 Row가 출력되는 시점에 작업을 중단하는 방식을 취하고 있는 듯 합니다.

 

따라서 Limit 형태보다는 where 조건으로 Athena가 쿼리해야 될 대상을 명확하게 하는 게 좀 더 효율적입니다.

 

Athena plans a query and then executes it. During planning it lists the partitions and all the files in those partitions. However, it does not know anything about the files, how many records they contain, etc.
When you say LIMIT 10 you're telling Athena you want at most 10 records in the result, and since you don't have any grouping or ordering you want 10 arbitrary records.
However, during the planning phase Athena can't know which partitions have files in them, and how many of those files it will need to read to find 10 records. Without listing the partition locations it can't know they're not all empty, and without reading the files it can't know they're not all empty too.
Therefore Athena first has to get the list of partitions, then list each partition's location on S3, even if you say you only want 10 arbitrary records.
In this case there are so many partitions that Athena short-circuits and says that you probably didn't mean to run this kind of query. If the table had fewer partitions Athena would execute the query and each worker would read as little as possible to return 10 records and then stop – but each worker would produce 10 records, because the worker can't assume that other workers would return any records. Finally the coordinator will pick the 10 records out of all the results form all workers to return as the final result.
Athena는 쿼리를 계획한 다음 실행합니다. 계획하는 동안 파티션과 해당 파티션의 모든 파일을 나열합니다. 그러나 파일, 파일에 포함된 레코드 수 등에 대해서는 알지 못합니다.
'LIMIT 10'이라고 말하면 Athena에 결과에 최대 10개의 레코드가 포함되기를 원하고 그룹화 또는 정렬이 없기 때문에 임의의 레코드 10개를 원한다고 말하는 것입니다.
그러나 계획 단계에서 Athena는 파일이 있는 파티션과 10개의 레코드를 찾기 위해 읽어야 하는 파일의 수를 알 수 없습니다. 파티션 위치를 나열하지 않고 모두 비어 있지 않다는 것을 알 수 없으며 파일을 읽지 않고는 모두 비어 있지 않다는 것을 알 수 없습니다.
따라서 Athena는 10개의 임의 레코드만 원한다고 해도 먼저 파티션 목록을 가져온 다음 S3에 각 파티션의 위치를 나열해야 합니다.
이 경우 파티션이 너무 많아서 Athena는 단락을 하고 이러한 종류의 쿼리를 실행할 의도가 없었다고 말합니다. 테이블에 파티션이 더 적은 경우 Athena는 쿼리를 실행하고 각 작업자는 가능한 한 적게 읽고 10개의 레코드를 반환한 다음 중지합니다. 그러나 작업자는 다른 작업자가 어떤 레코드도 반환할 것이라고 가정할 수 없기 때문에 각 작업자는 10개의 레코드를 생성합니다. . 마지막으로 코디네이터는 모든 작업자의 모든 결과에서 10개의 레코드를 선택하여 최종 결과로 반환합니다.

 

Ref.

Athena ignore LIMIT in some queries

SELECT

728x90