Explaining EXPLAIN
Fortunately, most SQL implementations provideFortunately, most SQL implementations provide a tool that describes how a query will be executed by the database engine: the keyword EXPLAIN. Note that the response of EXPLAIN will differ between SQL implementations or even between different versions of the same database. The following examples are from MySQL 8.0.31. At the end of this section, we will look at some examples of the response returned by the other major implementation of SQL, Postgres.
ActiveRecord offers us an easy way to access the keyword while still using its API: just call .explain when executing a query, and you’ll get the analysis instead. Let’s take an extremely simple query, fetching an object by one of its ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access