CTE를 사용한 최적화
CTE는 메인 쿼리에서 데이터를 처리하기 전에 집계 같은 연산을 먼저 수행해, 쿼리를 최적화하는 데에도 도움이 됩니다.
예를 들어, 주문 항목 수별 결제 횟수의 최댓값을 찾고 싶다고 합시다. 이 연습 문제에서는 CTE를 사용해 이를 효율적으로 구하는 방법을 살펴봅니다.
이 연습은 강의의 일부입니다
BigQuery 입문
연습 안내
- 서브쿼리와 메인 쿼리에 올바른 집계 함수를 추가하세요.
- 메인 쿼리에서
order_items열을 두 번 사용해 각 주문의 항목 수를 구하려면ARRAY_LENGTH를 사용하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
WITH payments AS (
SELECT
-- Use the correct aggregate to find the highest number of payments
___(payment_sequential) AS num_payments,
order_id
FROM ecommerce.ecomm_payments
-- Group the results by order
GROUP BY ___)
SELECT
-- Add the correct function to find the length or number of order items
___(o.order_items) AS num_items,
MAX(p.num_payments) AS max_payments
FROM ecommerce.ecomm_orders o
JOIN payments p
-- Add the correct keyword to join using the same column
___ (order_id)
-- Add the correct function to find the length or number of order items
GROUP BY ___(o.order_items)