You write a perfectly reasonable SQL query joining a few tables, and it runs in 5 milliseconds. A colleague writes a query that asks the same question and it takes 5 minutes. Same data, same answer â wildly different speed. What happened?
The database had to decide how to compute your query: which table to scan first, what order to join the others, which indexes to use. There are many possible execution plans for the same query, and they can differ in cost by a factor of a thousand or more. Picking a good one is the job of the query optimizer â and it's invisible, automatic, and crucial.
The heart of it is join ordering, and it's NP-hard: as you add tables, the number of possible orders explodes factorially. Every relational database â Postgres, MySQL, SQL Server â wrestles this hard problem millions of times a day, before you even see your results.
Comments
Loading comments...