优化的 GROUP BY 与 ORDER BY
在 GROUP BY 和 ORDER BY 中以正确顺序放置字段对性能至关重要。若这些子句中使用的是 SORTKEY,此点就更为关键。
本练习是课程的一部分
Redshift 入门
练习说明
- 在 SELECT 子句中分别将日期中的年与月提取为独立列。
- 在语句中以正确顺序添加
ORDER BY字段month与year。
交互式实操练习
通过完成这段示例代码来试试这个练习。
-- Extract the year and month from the date as separate elements
SELECT ___(___, date) as year,
___('month', ___) as month,
COUNT(
CASE WHEN hourlywindspeed != '0' THEN 1
ELSE NULL
END
)
FROM public_intro_redshift.coffee_county_weather
GROUP BY year, month
-- Add the ORDER BY fields in the proper order
ORDER BY ___ DESC, ___ DESC;