MySQL和MariaDB查询执行计划之间的区别

时间:2014-04-23 12:11:09

标签: mysql mariadb

MySQL和MariaDB查询执行计划有区别吗?

如果是,哪一个更好?

CREATE TABLE `Table1` (
  `ID` int(11) NOT NULL,
  KEY `ID` (`ID`)
);

CREATE TABLE `Table2` (
  `ID` int(11) NOT NULL,
  KEY `ID` (`ID`)
);

CREATE TABLE `Table3` (
  `ID` int(11) NOT NULL,
  PRIMARY KEY (`ID`)
);

在Maria DB中,

MariaDB [truepay_psr]> explain select T1.ID FROM Table1 T1 LEFT JOIN (SELECT T1.ID FROM Table3 T1 LEFT JOIN Table2 T2 ON T1.ID = T2.ID WHERE T2.ID IS NULL) T2 ON T1.ID=T2.ID WHERE T2.ID IS NULL;
+------+-------------+-------+--------+---------------+---------+---------+-------------------+------+--------------------------+
| id   | select_type | table | type   | possible_keys | key     | key_len | ref               | rows | Extra                    |
+------+-------------+-------+--------+---------------+---------+---------+-------------------+------+--------------------------+
|    1 | SIMPLE      | T1    | index  | NULL          | ID      | 4       | NULL              |    1 | Using index              |
|    1 | SIMPLE      | T1    | eq_ref | PRIMARY       | PRIMARY | 4       | truepay_psr.T1.ID |    1 | Using where; Using index |
|    1 | SIMPLE      | T2    | ref    | ID            | ID      | 4       | truepay_psr.T1.ID |    1 | Using where; Using index |
+------+-------------+-------+--------+---------------+---------+---------+-------------------+------+--------------------------+
3 rows in set (0.01 sec)

在MySQL中,

mysql> explain select T1.ID FROM Table1 T1 LEFT JOIN (SELECT T1.ID FROM Table3 T1 LEFT JOIN Table2 T2 ON T1.ID = T2.ID WHERE T2.ID IS NULL) T2 ON T1.ID=T2.ID WHERE T2.ID IS NULL;
+----+-------------+------------+--------+---------------+---------+---------+------------+------+--------------------------------------+
| id | select_type | table      | type   | possible_keys | key     | key_len | ref        | rows | Extra                                |
+----+-------------+------------+--------+---------------+---------+---------+------------+------+--------------------------------------+
|  1 | PRIMARY     | <derived2> | system | NULL          | NULL    | NULL    | NULL       |    0 | const row not found                  | 
|  1 | PRIMARY     | T1         | index  | NULL          | ID      | 4       | NULL       |    1 | Using index                          | 
|  2 | DERIVED     | T1         | index  | NULL          | PRIMARY | 4       | NULL       |    1 | Using index                          | 
|  2 | DERIVED     | T2         | ref    | ID            | ID      | 4       | test.T1.ID |    1 | Using where; Using index; Not exists | 
+----+-------------+------------+--------+---------------+---------+---------+------------+------+--------------------------------------+
4 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:2)

您正在看到“表消除”优化的效果(MySQL计划有4行,而MariaDB只有3行)。 MariaDB计划应该是更好的计划,因为“要做的工作少”。两者都应该返回相同的结果。

此处详细说明了该功能:

http://s.petrunia.net/blog/?p=58

在这里:

https://mariadb.com/kb/en/mariadb/documentation/optimization-and-tuning/query-optimizations/table-elimination/

如果您希望MariaDB使用与MySQL相同的计划,您可以通过以下方式禁用表格消除优化来强制执行此操作:

SET optimizer_switch='table_elimination=off';