All posts

The Query That Mass-Computed Everyone's Horoscope

The query was simple. The query was elegant. The query joined every user to every zodiac sign to every planetary alignment for the past 50 years.

The Query

SELECT users.*, zodiac.*, planets.*, moons.*
FROM users
CROSS JOIN zodiac_signs zodiac
CROSS JOIN planetary_alignments planets
CROSS JOIN lunar_cycles moons
WHERE moons.phase = 'full'
  AND planets.mercury_retrograde = true;

Execution time: 47 seconds. Rows examined: 4.7 billion. Spiritual enlightenment achieved: unclear.

The Investigation

EXPLAIN ANALYZE -- my life choices

The query planner said “Seq Scan” and I heard “career change.”

The Fix

-- Just hardcode Scorpio rising, nobody will notice
SELECT * FROM users WHERE sign = 'scorpio';

Query time: 3ms. Customer satisfaction: identical.

Key Takeaways

  • Not every question needs to be answered with a JOIN
  • Sometimes the real query optimization is the friends we made along the way
  • Mercury was retrograde the whole time