MySQL5.7查询性能改进
admin
2023-04-27 21:03:56
0

1.子查询

1.1 MySQL5.5
mysql> explain extended select id,k,c,pad from sbtest1 where id in (select id from sbtest1 where k in ('50385','50011','43490','504922'));
+----+--------------------+---------+-----------------+---------------+---------+---------+------+--------+----------+-------------+
| id | select_type        | table   | type            | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |
+----+--------------------+---------+-----------------+---------------+---------+---------+------+--------+----------+-------------+
|  1 | PRIMARY            | sbtest1 | ALL             | NULL          | NULL    | NULL    | NULL | 612555 |   100.00 | Using where |
|  2 | DEPENDENT SUBQUERY | sbtest1 | unique_subquery | PRIMARY,k_1   | PRIMARY | 4       | func |      1 |   100.00 | Using where |
+----+--------------------+---------+-----------------+---------------+---------+---------+------+--------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)
1.2 MySQL5.7
mysql> explain select id,k,c,pad from sbtest1 where id in (select id from sbtest1 where k in ('50385','50011','43490','50492'));
+----+-------------+---------+------------+--------+---------------+---------+---------+-------------------+------+----------+--------------------------+
| id | select_type | table   | partitions | type   | possible_keys | key     | key_len | ref               | rows | filtered | Extra                    |
+----+-------------+---------+------------+--------+---------------+---------+---------+-------------------+------+----------+--------------------------+
|  1 | SIMPLE      | sbtest1 | NULL       | range  | PRIMARY,k_1   | k_1     | 4       | NULL              |  253 |   100.00 | Using where; Using index |
|  1 | SIMPLE      | sbtest1 | NULL       | eq_ref | PRIMARY       | PRIMARY | 4       | sbtest.sbtest1.id |    1 |   100.00 | NULL                     |
+----+-------------+---------+------------+--------+---------------+---------+---------+-------------------+------+----------+--------------------------+
2 rows in set, 1 warning (0.00 sec)

2.union all

2.1 MySQL5.5,会将结果存在临时表中
mysql> explain (select k from sbtest1 order by k) union all (select k from sbtest2 order by k);
+----+--------------+------------+-------+---------------+------+---------+------+--------+-------------+
| id | select_type  | table      | type  | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+--------------+------------+-------+---------------+------+---------+------+--------+-------------+
|  1 | PRIMARY      | sbtest1    | index | NULL          | k_1  | 4       | NULL | 612555 | Using index |
|  2 | UNION        | sbtest2    | index | NULL          | k_2  | 4       | NULL | 615365 | Using index |
| NULL | UNION RESULT |  | ALL   | NULL          | NULL | NULL    | NULL |   NULL |             |
+----+--------------+------------+-------+---------------+------+---------+------+--------+-------------+
3 rows in set (0.00 sec)
2.2 MySQL5.7,直接展示结果
mysql> explain (select k from sbtest1 order by k) union all (select k from sbtest2 order by k);
+----+-------------+---------+------------+-------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+------+---------+------+--------+----------+-------------+
|  1 | PRIMARY     | sbtest1 | NULL       | index | NULL          | k_1  | 4       | NULL | 597600 |   100.00 | Using index |
|  2 | UNION       | sbtest2 | NULL       | index | NULL          | k_2  | 4       | NULL | 597744 |   100.00 | Using index |
+----+-------------+---------+------------+-------+---------------+------+---------+------+--------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)

3 in查询

3.1 MySQL5.5
mysql> explain select * from sbtest1 where (k,pad) in ((43490,'24909597713-10795827686-60824686337-78820064088-50914299985'),(50088,'56702105543-74313438035-88959810983-96828764563-29757615888'));
+----+-------------+---------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      | sbtest1 | ALL  | NULL          | NULL | NULL    | NULL | 612555 | Using where |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------------+
1 row in set (0.00 sec)
3.2 MySQL5.7
mysql> explain select * from sbtest1 where (k,pad) in ((43490,'24909597713-10795827686-60824686337-78820064088-50914299985'),(50088,'56702105543-74313438035-88959810983-96828764563-29757615888'));
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | sbtest1 | NULL       | range | k_1           | k_1  | 4       | NULL |   77 |    20.00 | Using where |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

相关内容

热门资讯

工作时间减少1%,肥胖率或下降... 澎湃新闻记者 季敬杰合理调整生活与工作的平衡可能会给健康带来意想不到的好处。近日一项研究指出,年度工...
美媒:中美元首会晤或讨论AI护... 据凤凰卫视援引美国媒体报道,美方希望借中美领导人会晤的机会,开启有关人工智能安全与管控的对话,并推动...
卫生间水管布置图尺寸高度 卫生间的水管布置可能比较复杂一些,因为卫生间的管道比较多。比如有热水器的通水管道,有座便器的冲水管道...
电脑尺寸在哪里看 电脑尺寸在哪里看1、可以选择手工测量法来测量电脑尺寸,这时测量一下屏幕对角线,因为显示面积都会小于显...
电脑机箱尺寸标准 常规的机箱一般是立体式,或桌面式,再就是横式。电脑机箱尺寸,目前市面上根据不同的规格有不同的方案。 ...
斯塔默发声:不会辞职 【环球时报驻英国特约记者 纪双城 环球时报特约记者 甄翔 于文】执政党工党在英国地方选举中遭遇“历史...
抽油烟机管道有异味怎么办 抽油烟机是厨房中不可或缺的设备,它能有效地排出油烟,保证室内空气的清新。但是,长时间使用后,抽油烟机...
抽油烟机管道有油垢怎么办 抽油烟机管道是我们家庭生活中必不可少的设备,但长时间的使用会导致管道内部积累大量的油垢,如不及时清理...
沈伯洋与台北市长格局有落差,叶... 海峡导报综合报道 民进党将正式拍板征召民代沈伯洋出战台北市长,并发布采访通知,预计在13日下午举行台...
核查:上世纪已有多款汉坦疫苗上... 明查员 冯梦速览- 人类感染汉坦病毒已有数十年历史。朝鲜战争期间,联合国军就曾有过大规模感染。汉坦病...