BigQuery เบื้องต้น
Matt Forrest
Field CTO
มีหลักการเพิ่มประสิทธิภาพหลัก 3 ข้อ:
SELECT * และเลือกเฉพาะคอลัมน์ที่ต้องการWHERE กรองข้อมูลตั้งแต่เนิ่นๆ และทำบ่อยๆINT64WITH filter_my_data AS (SELECT
-- Filter data with
-- WHERE in the CTE first
)
SELECT
-- This query will run
-- faster with less data
JOIN a USING (user_id)
ใน BigQuery ให้ใช้
BOOLINTFLOATDATE ชนิดข้อมูลที่ใช้กับ WHERE แบบ STRING หรือ BYTE ไม่เหมาะสม
ไม่เหมาะสม
SELECT user_id, date_ordered
FROM dataset.table
WHERE product = 'shoes'
เหมาะสม
SELECT user_id, date_ordered
FROM dataset.table
WHERE product_id = 1234
ORDER BY ควรอยู่ที่ส่วนท้ายสุด (ด้านนอกสุด) ของคิวรีเสมอORDER BY ภายใน window clause เท่านั้นไม่เหมาะสม
WITH order_total AS (SELECT
user_id,
sum(product_price) as order_sum
FROM orders
GROUP BY user_id
-- Order by is not at the end of the query
ORDER BY last_purchase_date
)
SELECT order_total.order_sum,
users.user_name
FROM dataset.users users
JOIN order_total USING (user_id);
เหมาะสม
WITH order_total AS (SELECT
user_id,
last_purchase_date
sum(product_price) as order_sum
GROUP BY user_id
)
SELECT order_total.order_sum,
users.user_name
FROM dataset.users users
JOIN a USING (user_id)
-- Order by should always be at the end
ORDER BY orders_total.last_purchase_date;
EXISTSCOUNT สำหรับกรณีนี้SELECT EXISTS (
-- Write the main query as a subquery within the exists call
SELECT
user_id
FROM
dataset.table
WHERE
product_category = 'home_goods'
AND status = 'Closed Account'
);
APPROX_TOP_SUM หรือ APPROX_COUNT_DISTINCTWHERE clause ด้วยBigQuery เบื้องต้น