Learning how to see what indexes your query is using

There is a simple way to see how MySQL will run your query, and that is to append EXPLAIN to the front of your query. For example, you can see EXPLAIN being used in the following query:

USE lahmansbaseballdb;EXPLAIN SELECT playerid, g_all, g_batting, g_defense FROM appearances;

What EXPLAIN will do is give you a table of information about how it's going to run the query. The previous query will give you the results shown in the following screenshot:

Let's go through what each of these columns means:

  • id: This is the sequential number of the query this row belongs to. In this case, we ...

Get Learn SQL Database Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.