一创建表 create table tt1(id int primary key, c1 int);create table tt2(id int primary key, c2 int);insert into tt1 value
一创建表
create table tt1(id int primary key, c1 int);
create table tt2(id int primary key, c2 int);
insert into tt1 values(1,1),(2,2),(3,3),(4,4);
insert into tt2 values(1,2),(2,2);
二 执行计划与问题
mysql> explain extended select tt1.c1, (select tt2.c2 from tt2 where c2=10) from tt1, tt2;
+----+-------------+-------+-------+---------------+---------+---------+------+------+----------+---------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+----------+---------------------------------------+
| 1 | primary | tt2 | index | null | primary | 4 | null | 2 | 100.00 | using index |
| 1 | primary | tt1 | all | null | null | null | null | 4 | 100.00 | using join buffer (block nested loop) |
| 2 | subquery | tt2 | all | null | null | null | null | 2 | 100.00 | using where |
+----+-------------+-------+-------+---------------+---------+---------+------+------+----------+---------------------------------------+
3 rows in set, 1 warning (0.00 sec)
这个语句的执行顺序是怎么执行的,就是按照执行计划这样从上到下执行吗?
三 分析
---id值为1的两个,,是from子句中的tt1和tt2,用块嵌套循环连接算法做内连接,tt2是外表,所以先启动的是tt2;后启动的是tt1。
---id值为2的是目标列中的子查询,后于from子句中的表执行。发生在连接后要求目标列的值发给客户端阶段。
---理论上是可以优化的,方法为:目标列只有一列和where条件相同,所以,可以推知此子查询的结果是2。
---但是,mysql没有优化这样的子查询。
---另外,对于这里的子查询,如果其结果返回多行,则mysql会报告错误:error 1242 (21000): subquery returns more than 1 row。
--------------------------------------分割线 --------------------------------------
ubuntu 14.04下安装mysql
《mysql权威指南(原书第2版)》清晰中文扫描版 pdf
ubuntu 14.04 lts 安装 lnmp nginx\php5 (php-fpm)\mysql
ubuntu 14.04下搭建mysql主从服务器
ubuntu 12.04 lts 构建高可用分布式 mysql 集群
ubuntu 12.04下源代码安装mysql5.6以及python-mysqldb
mysql-5.5.38通用二进制安装
--------------------------------------分割线 --------------------------------------
本文永久更新链接地址: