I’ve always been interested in how you can throw some SQL at database, and it nearly instantaneously returns your results in an orderly manner without thinking about it as anything other than a black box.
What is really going on?
I’m pretty sure it has something to do with how values are laid out regularly in memory, similar to an array; but aside from that, I don’t know much else.
How is SQL parsed in a manner to facilitate all of this?
The engine builds a such called query plan.
It’s a set of algorithms used to return the sets that you described logically with an
SQLquery.Almost each engine lets you see what query plan will it build for a certain query.
In
MySQLandPostgreSQL, you prepend your query with the wordEXPLAINIn
SQL Server, you runSET SHOWPLAN_TEXT ONbefore running the query or just pressCtrl-Lin the Management StudioIn
Oracle, you prepend the query withEXPLAIN PLAN FORand then issueSELECT * FROM (dbms_xplan.display)You may find interesting this article in my blog:
which addresses the same question.