----------------------------
|
-- REGULAR TABLES
|
----------------------------
|
create table t1 (a int);
|
insert into t1 values (0),(1),(2),(3);
|
|
create table t2 (a int primary key, b int)
|
as select a, a as b from t1 where a in (1,2);
|
|
create table t3 (a int primary key, b int)
|
as select a, a as b from t1 where a in (1,3);
|
|
-- Elimination of a single table
|
explain select t1.a from t1 left join t2 on t2.a=t1.a;
|
+------+-------------+-------+------+---------------+------+---------+------+------+------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+------+---------------+------+---------+------+------+------------+
|
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 2 | |
|
| NULL | NULL | t2 | NULL | NULL | NULL | NULL | NULL | NULL | Eliminated |
|
+------+-------------+-------+------+---------------+------+---------+------+------+------------+
|
2 rows in set (1.924 sec)
|
|
-- Elimination of multiple tables
|
explain select t1.a from t1 left join (t2 join t3) on t2.a=t1.a and t3.a=t1.a;
|
+------+-------------+-------+------+---------------+------+---------+------+------+------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+------+---------------+------+---------+------+------+------------+
|
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 2 | |
|
| NULL | NULL | t3 | NULL | NULL | NULL | NULL | NULL | NULL | Eliminated |
|
| NULL | NULL | t2 | NULL | NULL | NULL | NULL | NULL | NULL | Eliminated |
|
+------+-------------+-------+------+---------------+------+---------+------+------+------------+
|
3 rows in set (1.626 sec)
|
|
----------------------------
|
-- DERIVED TABLES
|
----------------------------
|
create table t11 (a int not null, b int, key(a));
|
insert into t11 select A.seq, A.seq+B.seq from seq_1_to_10 A, seq_1_to_100 B;
|
|
create table t12 (pk int primary key, col1 int);
|
insert into t12 select seq, seq from seq_1_to_1000;
|
|
-- Whole derived table is eliminated
|
explain select t1.* from t1 left join
|
(select t11.a as a, count(*) as b
|
from t11 left join t12 on t12.pk=t11.b
|
group by t11.a) v2b on v2b.a=t1.a;
|
|
+------+-------------+------------+------+---------------+------+---------+------+------+------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+------------+------+---------------+------+---------+------+------+------------+
|
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 2 | |
|
| NULL | NULL | <derived2> | NULL | NULL | NULL | NULL | NULL | NULL | Eliminated |
|
| NULL | NULL | t12 | NULL | NULL | NULL | NULL | NULL | NULL | Eliminated |
|
| NULL | NULL | t11 | NULL | NULL | NULL | NULL | NULL | NULL | Eliminated |
|
+------+-------------+------------+------+---------------+------+---------+------+------+------------+
|
|
-- Only one table from the derived one is eliminated
|
explain select t1.* from t1 left join
|
(select t11.a as a, count(*) as b
|
from t11 left join t12 on t12.pk=t11.b
|
group by t11.a) v2b on v2b.b=t1.a;
|
+------+-------------+------------+-------+---------------+------+---------+----------------+------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+------------+-------+---------------+------+---------+----------------+------+-------------+
|
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 2 | |
|
| 1 | PRIMARY | <derived2> | ref | key0 | key0 | 8 | mdev28869.t1.a | 10 | Using where |
|
| 2 | DERIVED | t11 | index | NULL | a | 4 | NULL | 1000 | |
|
| NULL | NULL | t12 | NULL | NULL | NULL | NULL | NULL | NULL | Eliminated |
|
+------+-------------+------------+-------+---------------+------+---------+----------------+------+-------------+
|
The proposed format of the output is as follows. Suggestions/corrections are welcome.
----------------------------
-- REGULAR TABLES
----------------------------
-- Elimination of a single table
-- Elimination of multiple tables
----------------------------
-- DERIVED TABLES
----------------------------
-- Whole derived table is eliminated
-- Only one table from the derived one is eliminated
Same queries explained in JSON format
-- Elimination of a single table
| {
{
}
},
{
}
}
]
}
} |
-- Elimination of multiple tables
| {
{
}
},
{
}
},
{
}
}
]
}
} |
-- Whole derived table is eliminated (note that "eliminated" is printed instead of "materialized" for the derived table)
| {
{
}
},
{
{
}
},
{
}
}
]
}
}
}
]
}
} |
-- Only one table from the derived one is eliminated
| {
{
}
},
{
{
}
},
{
}
}
]
}
}
}
}
]
}
} |
If the proposed format is OK I'll move on with updating test cases and cleaning up the code.