COUNT(DISTINCT col) on a large unfiltered table forces a full sort
and distinct pass. Often a perf surprise on prod. Warn when there's no
WHERE, LIMIT, or GROUP BY restricting the scope.
Should fail
SELECT COUNT(DISTINCT user_id) FROM events;
Should pass
SELECT COUNT(DISTINCT user_id) FROM events WHERE event_date >= CURRENT_DATE - 7;
Implementation hints
- Multi-line rule.
- Look for
COUNT(DISTINCT ...) and require either WHERE or GROUP BY in
the same statement.
- Severity:
warning.
Estimated LOC: ~30 code + ~20 test.
COUNT(DISTINCT col)on a large unfiltered table forces a full sortand distinct pass. Often a perf surprise on prod. Warn when there's no
WHERE,LIMIT, orGROUP BYrestricting the scope.Should fail
Should pass
Implementation hints
COUNT(DISTINCT ...)and require either WHERE or GROUP BY inthe same statement.
warning.Estimated LOC: ~30 code + ~20 test.