Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know what's behind the `auto_key0` index in MySQL?

Saying I have this query:

EXPLAIN SELECT *
FROM (
    SELECT "A" as a, i.n FROM (SELECT 1 AS n) AS i
    UNION ALL SELECT "B" as a, i.n FROM (SELECT 1 AS n) AS i) AS t
WHERE a = "B";

MySQL says

id  select_type table   partitions  type    possible_keys   key key_len ref rows    filtered    Extra
1   PRIMARY <derived2>  \N  ref <auto_key0> <auto_key0> 6   const   1   100.00  \N
2   DERIVED <derived3>  \N  system  \N  \N  \N  \N  1   100.00  \N
3   DERIVED \N  \N  \N  \N  \N  \N  \N  \N  \N  No tables used
4   UNION   <derived5>  \N  system  \N  \N  \N  \N  1   100.00  \N
5   DERIVED \N  \N  \N  \N  \N  \N  \N  \N  \N  No tables used

So MySQL generated an intermediate index <auto_key0> but what is behind this index? What columns are used in it? And is there a way I can set this index manually, and force MySQL to use some columns.

like image 236
Xenos Avatar asked Jan 28 '23 20:01

Xenos


1 Answers

EXPLAIN FORMAT=JSON SELECT ... will return something like

    key: <auto_key0>, used_key_parts: ['a'], key_length: 6, ref: ['const']

<auto_key0> is the index generated by the Optimizer for a derived table.

(There is also "Optimizer trace"; but that probably does not have this particular info.)

like image 77
Rick James Avatar answered Feb 12 '23 22:02

Rick James