MySQL 子查询优化案例
开发人员给了一个sql ,结构如下delete from B where ID in (select NID from H where guid='xxx');
内部sql满足条件的结果集只有一条,但是整个删除操作执行了将近1分钟,如果是将结果集放在括号里或者将in改为= ,执行的速度可以实现毫秒级别
但是如果内部查询结果集多于一行,采用第一种方案的话需要更改程序,后来又试了一种更改为join,速度也是极快。
测试表,t1.id上有索引,t2.id无索引
mysql> select * from t1; mysql> select * from t2;
+------+------+----------+ +------+---------+
| id | name | class_id | | id | name |
+------+------+----------+ +------+---------+
| 1 | aa | NULL | | 2 | myname2 |
| 2 | aa | NULL | | 6 | myname5 |
| 3 | dd | NULL | +------+---------+
| 6 | cc | NULL | 2 rows in set (0.01 sec)
+------+------+----------+
4 rows in set (0.00 sec)
使用子查询及改为join后的执行计划
mysql> explain delete from t1 where id in (select id from t2 where name='aa');
+----+--------------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | DELETE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 4 | 100.00 | Using where |
| 2 | DEPENDENT SUBQUERY | t2 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 50.00 | Using where |
+----+--------------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
2 rows in set (0.00 sec) mysql> explain delete t1.* from t1 inner join t2 where t1.id=t2.id and t2.name='aa';
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-------------+
| 1 | SIMPLE | t2 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 50.00 | Using where |
| 1 | DELETE | t1 | NULL | ref | idx_id | idx_id | 5 | const | 1 | 100.00 | Using where |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-------------+
2 rows in set (0.01 sec)
对于子查询的执行计划可以看出先对t1进行全表扫描,然后执行select id from t2 where name='aa' and t1.id=t2.id ,如果有值则删除t.* where id=t1.id
而对于改为join的sql来说,优化器会很智能的选取小表来作为驱动表,然后再走索引删除t1.* , 而对于子查询官方文档解释为由外向内执行
为了更加直观的看两种方式的执行过程,打开回话级别的profiling
mysql> show profiles;
+----------+------------+------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+------------------------------------------------------------------------------+
| 3 | 0.00137075 | delete from t1 where id in (select id from t2 where name='aa') |
| 4 | 0.00211725 | explain delete t1.* from t1 inner join t2 where t1.id=t2.id and t2.name='aa' |
| 5 | 0.00132050 | delete t1.* from t1 inner join t2 where t1.id=t2.id and t2.name='aa' |
+----------+------------+------------------------------------------------------------------------------+ mysql> show profile for query 3 mysql> show profile for query 5
-> ; -> ;
+----------------------+----------+ +--------------------------------+----------+
| Status | Duration | | Status | Duration |
+----------------------+----------+ +--------------------------------+----------+
| starting | 0.000388 | | starting | 0.000360 |
| checking permissions | 0.000026 | | checking permissions | 0.000013 |
| checking permissions | 0.000008 | | checking permissions | 0.000007 |
| Opening tables | 0.000105 | | checking permissions | 0.000004 |
| init | 0.000152 | | init | 0.000005 |
| System lock | 0.000083 | | Opening tables | 0.000048 |
| updating | 0.000084 | | init | 0.000048 |
| optimizing | 0.000031 | | deleting from main table | 0.000022 |
| statistics | 0.000083 | | System lock | 0.000028 |
| preparing | 0.000052 | | optimizing | 0.000043 |
| executing | 0.000013 | | statistics | 0.000144 |
| Sending data | 0.000114 | | preparing | 0.000144 |
| executing | 0.000009 | | executing | 0.000009 |
| Sending data | 0.000017 | | Sending data | 0.000246 |
| executing | 0.000005 | | deleting from reference tables | 0.000073 |
| Sending data | 0.000019 | | end | 0.000012 |
| executing | 0.000006 | | end | 0.000010 |
| Sending data | 0.000018 | | query end | 0.000016 |
| end | 0.000019 | | closing tables | 0.000015 |
| query end | 0.000020 | | freeing items | 0.000037 |
| closing tables | 0.000021 | | cleaning up | 0.000039 |
| freeing items | 0.000054 | +--------------------------------+----------+
| cleaning up | 0.000046 | 21 rows in set, 1 warning (0.00 sec)
+----------------------+----------+
23 rows in set, 1 warning (0.01 sec)
我第一眼关注的是两条语句senting data的次数,子查询对应的sending data是4次,子查询先对外部表进行全表扫描,结果集是4行,然后进行循环遍历拿出每一行与内部查询进行关联,共执行了4次内部查询,并且每次都对内部查询的结果集做一下判断是否有值,如果有值则再进行删除
小小的记录一下,在优化器的探索之路上慢慢爬
MySQL 子查询优化案例的更多相关文章
- mysql 子查询优化
今天用到要查询七天内都没有装机的门店信息,首先想到了用not in,先把装机的userid查出来,然后再id not in,但是这样就必须使用子查询,数据量少还可以,数据量大了的话,肯定效率特别低,因 ...
- MySQL子查询优化实例
优化:子查询改写成关联查询 线上遇到问题,查询较慢,如为对应SQL的查询执行计划: localhost.\G . row *************************** id: select_ ...
- mysql子查询案例
源SQL如下: 创建数据表 CREATE TABLE IF NOT EXISTS tdb_goods( goods_id SMALLINT UNSIGNED PRIMARY KEY AUTO_ ...
- mysql子查询优化
,,,) ) LIMIT 第一种方式in where:2000ms SELECT COUNT(*) AS tp_count FROM xxx_b2c_orders o ,,,) and from xx ...
- 【MySQL】MySQL中针对大数据量常用技术_创建索引+缓存配置+分库分表+子查询优化(转载)
原文地址:http://blog.csdn.net/zwan0518/article/details/11972853 目录(?)[-] 一查询优化 1创建索引 2缓存的配置 3slow_query_ ...
- MySQL查询原理及其慢查询优化案例分享(转)
MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能出色,但所谓“好马配好鞍”,如何能够更 好的使用它,已经成为开发工程师的必修课,我们经常会从职 ...
- MySQL实验 子查询优化双参数limit
MySQL实验 子查询优化双参数limit 没想到双参数limit还有优化的余地,为了亲眼见到,今天来亲自实验一下. 实验准备 使用MySQL官方的大数据库employees进行实验,导入该示例库 ...
- Mysql单表访问方法,索引合并,多表连接原理,基于规则的优化,子查询优化
参考书籍<mysql是怎样运行的> 非常推荐这本书,通俗易懂,但是没有讲mysql主从等内容 书中还讲解了本文没有提到的子查询优化内容, 本文只总结了常见的子查询是如何优化的 系列文章目录 ...
- [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时
案例梳理时间:2013-9-25 写在前面的话: 在慢查优化1和2里都反复强调过 explain 的重要性,但有时候肉眼看不出 explain 结果如何指导优化,这时候还需要有一些其他基础知识的佐助, ...
随机推荐
- How to detect the presence of the Visual C++ 2010 redistributable package
Question: I have seen your previous blog posts that describe how to detect the presence of the Visua ...
- option标签selected="selected"属性失效的问题
要在select标签上面加上autocomplete="off"关闭自动完成,不然浏览器每次刷新后将自动选择上一次关闭时的option,这样默认属性selected="s ...
- Oracle数据仓库创建教程
Oracle数据仓库创建教程.如何创建一个数据仓库,创建实例,以为毕业设计要求,最近开始Oracle的数仓建模实践,详细记录了图形界面下的 Oracle database 12C 数据仓库创建过程. ...
- WebService学习之旅(一)使用JAX-WS发布WebService
JAX-WS全称Java™ API for XML Web Services,是随着JDK1.6及其后续版本发布的方便Java程序员开发WebService应用的一组API,通常简称为JWS,目前版本 ...
- webstorm使用总结
1.webstorm显示ES6语法错误,和nodejs语法错误未提示的问题,只需要在 此处解决ES6语法错误问题: 此处解决不支持node语法的问题: 然后就显示正常啦.
- Lucene-安装和运行Demo程序
Lucene是什么 Lucene是一款高性能.可扩展的信息检索工具库.- Lucene In Action Lucene版本:7.1 一.下载安装包 https://lucene.apache.org ...
- 【MATLAB 从零到进阶】day2 矩阵 数组
访问矩阵元素 >> A=[1,2,3;4,5,6;7,8,9]; >> x=A(2,3)% 双下标访问 x = 6 >> x=A(2)% 单下标访问 x = 4 单 ...
- Exception in thread "main" java.lang.NoSuchMethodError: org.apache.http.entity.ContentType.withCharset(Ljava/lang/String;)Lorg/apache/http/entity/ContentType;
解决方案是:第一点先检查一下使用的包是否冲突,是否是版本号一致.第二点是增加一个包 忙活了好久才解决了这个异常,小小的激动一下啊啊
- js 判断是什么浏览器、是否为谷歌浏览器
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...
- 通过例子理解 k8s 架构【转】
为了帮助大家更好地理解 Kubernetes 架构,我们部署一个应用来演示各个组件之间是如何协作的. 执行命令 kubectl run httpd-app --image=httpd --replic ...