You write a tidy SQL query: join Customers to Orders to Products to Reviews, add a filter, ask for a result. To you it reads as one sentence. To the database it is a question with a thousand answers, because joins can be performed in any order — and the order changes everything.
Join two small tables first and you carry a tiny intermediate result through the rest of the work. Join two huge tables first and you build a monster that every later step has to drag along. Same answer at the end, wildly different cost in between — often a difference of thousands of times in time and memory.
Deciding the cheapest order is the single most important job of the query optimizer, the piece of every database that turns your SQL into an actual plan. And it turns out that finding the truly cheapest order is one of the hard problems of computer science.
Comments
Loading comments...