http://www.jb51.net/article/50427.htm

七.性能优化
1.显示(explicit) inner join VS 隐式(implicit) inner join

如:

复制代码代码如下:
select * from
table a inner join table b
on a.id = b.id;

VS

复制代码代码如下:
select a.*, b.*
from table a, table b
where a.id = b.id;

我在数据库中比较(10w数据)得之,它们用时几乎相同,第一个是显示的inner join,后一个是隐式的inner join。

2.left join/right join VS inner join

尽量用inner join.避免 LEFT JOIN 和 NULL.

在使用left join(或right join)时,应该清楚的知道以下几点:
(1). on与 where的执行顺序

ON 条件(“A LEFT JOIN B ON 条件表达式”中的ON)用来决定如何从 B 表中检索数据行。如果 B 表中没有任何一行数据匹配 ON 的条件,将会额外生成一行所有列为 NULL 的数据,在匹配阶段 WHERE 子句的条件都不会被使用。仅在匹配阶段完成以后,WHERE 子句条件才会被使用。它将从匹配阶段产生的数据中检索过滤。

所以我们要注意:在使用Left (right) join的时候,一定要在先给出尽可能多的匹配满足条件,减少Where的执行。如:

PASS

复制代码代码如下:
select * from A
inner join B on B.name = A.name
left join C on C.name = B.name
left join D on D.id = C.id
where C.status>1 and D.status=1;

Great

复制代码代码如下:
select * from A
inner join B on B.name = A.name
left join C on C.name = B.name and C.status>1
left join D on D.id = C.id and D.status=1

从上面例子可以看出,尽可能满足ON的条件,而少用Where的条件。从执行性能来看第二个显然更加省时。

(2).注意ON 子句和 WHERE 子句的不同

如作者举了一个列子:

复制代码代码如下:
mysql> SELECT * FROM product LEFT JOIN product_details
       ON (product.id = product_details.id)
       AND product_details.id=2;
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |  NULL |
|  2 |    200 |    2 |     22 |     0 |
|  3 |    300 | NULL |   NULL |  NULL |
|  4 |    400 | NULL |   NULL |  NULL |
+----+--------+------+--------+-------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM product LEFT JOIN product_details
       ON (product.id = product_details.id)
       WHERE product_details.id=2;
+----+--------+----+--------+-------+
| id | amount | id | weight | exist |
+----+--------+----+--------+-------+
|  2 |    200 |  2 |     22 |     0 |
+----+--------+----+--------+-------+
1 row in set (0.01 sec)

从上可知,第一条查询使用 ON 条件决定了从 LEFT JOIN的 product_details表中检索符合的所有数据行。第二条查询做了简单的LEFT JOIN,然后使用 WHERE 子句从 LEFT JOIN的数据中过滤掉不符合条件的数据行。

(3).尽量避免子查询,而用join

往往性能这玩意儿,更多时候体现在数据量比较大的时候,此时,我们应该避免复杂的子查询。如下:

PASS

复制代码代码如下:
insert into t1(a1) select b1 from t2 where not exists(select 1 from t1 where t1.id = t2.r_id); 

Great

复制代码代码如下:
insert into t1(a1)  
select b1 from t2  
left join (select distinct t1.id from t1 ) t1 on t1.id = t2.r_id   
where t1.id is null;  

最新文章

  1. git 保存本地更改而不需要推到远程
  2. 一个c++剧情脚本指令系统
  3. SGU223 - Little Kings(状态压缩DP)
  4. 学习DTD和Schema的几个例子
  5. [Q]升级/重新获取授权步骤
  6. mysqldump命令详解
  7. 【漏洞公告】CVE-2017-12615/CVE-2017-12616:Tomcat信息泄漏和远程代码执行漏洞
  8. SLAM入门之视觉里程计(2):两视图对极约束 基础矩阵
  9. mysql数据库基本使用(增删改查)
  10. Python 字典和集合基于哈希表实现
  11. Linux下复制文件
  12. Java核心-多线程-并发控制器-Semaphore信号量
  13. 微信小程序页面导航功能
  14. 《Orange’s》保护模式
  15. Executors创建的4种线程池的使用
  16. Linux配置防火墙端口 8080端口
  17. 【 HDU 2177 】取(2堆)石子游戏 (威佐夫博弈)
  18. java反射的性能问题
  19. c# DataTable 序列化json
  20. C#抽象类与抽象方法--就是类里面定义了函数而函数里面什么都没有做的类

热门文章

  1. 微信小程序的下拉刷新
  2. Struts2学习笔记04 之 拦截器
  3. Can't load standard profile: GRAY.pf
  4. linux命令(12):ping命令
  5. Django Ajax学习二之文件上传
  6. lr_Controller界面图
  7. 六十一 Web开发 使用Web框架
  8. Linux让程序后台运行命令之screen与nohup
  9. JSTL 1.1与JSTL 1.2之间的区别?如何下载JSTL 1.2?
  10. Intellij IDEA 去掉Mapper文件中的背景