Search code examples
mysqlquery-optimization

What the meaning of the `id` column of the mysql query explain?


let's say a query:

EXPLAIN
SELECT actor_id,
    (SELECT 1 FROM sakila.film_actor WHERE film_actor.actor_id =
        der_1.actor_id LIMIT 1)
FROM (
    SELECT actor_id
    FROM sakila.actor LIMIT 5
) AS der_1
UNION ALL
 SELECT film_id,
     (SELECT @var1 FROM sakila.rental LIMIT 1)
 FROM (
     SELECT film_id,
        (SELECT 1 FROM sakila.store LIMIT 1)
     FROM sakila.film LIMIT 5
 ) AS der_2;

which gives following query plan with 8 steps(rows):

+------+----------------------+------------+...
| id   | select_type          | table      |...
+------+----------------------+------------+...
| 1    | PRIMARY              | <derived3> |...
| 3    | DERIVED              | actor      |...
| 2    | DEPENDENT SUBQUERY   | film_actor |...
| 4    | UNION                | <derived6> |...
| 6    | DERIVED              | film       |...
| 7    | SUBQUERY             | store      |...
| 5    | UNCACHEABLE SUBQUERY | rental     |...
| NULL | UNION RESULT         | <union1,4> |...
+------+----------------------+------------+...

The official document defines the id column:

The SELECT identifier. This is the sequential number of the SELECT within the query. The value can be NULL if the row refers to the union result of other rows. In this case, the table column shows a value like <unionM,N> to indicate that the row refers to the union of the rows with id values of M and N.

According to the doc, the id is just the sequential number(sn for concise), is there any metaphors of this sn?

  • Is the sn value reflects the execution order of the query plan steps? The greater the sn, the earlier the exection order?
  • If the sn value is the same of two steps, which step will run first by mysql?

Solution

  • Database systems like MySQL have very elaborate query planning / optimizing modules built in to them. EXPLAIN reveals just a bit of the logic of optimization; in particular which indexes are relevant. EXPLAIN doesn't necessarily reveal how the server orders its operations.

    And, SQL is a declarative language. You use it to describe what you want, not how to get it.. This makes it different from most other programming languages, which are procedural. Your question about subquery execution order is a procedural question, not a declarative question.

    The execution order of subqueries, and their concurrency of execution, is an implementation detail, and may well change from release to release of the database software.