设为首页 加入收藏

TOP

MySQL 5.7使用or是否会用到索引并提高查询效率的理解(四)
2019-03-02 22:09:36 】 浏览:325
Tags:MySQL 5.7 使用 是否 用到 索引 提高 查询 效率 理解
---------+------+---------+-------+--------+----------+-------------+
|  1 | PRIMARY    | sbtest1 | NULL      | ref  | k_1          | k_1  | 4      | const |    113 |  100.00 | NULL        |
|  2 | UNION      | sbtest1 | NULL      | ALL  | NULL          | NULL | NULL    | NULL  | 986400 |    10.00 | Using where |
+----+-------------+---------+------------+------+---------------+------+---------+-------+--------+----------+-------------+


3.接下来我们看看多表关联查询


mysql> explain select a.pad,b.pad from sbtest1 a,sbtest2 b where a.id=b.id and (a.c='123' or b.c='1234');               
+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+--------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key    | key_len | ref      | rows  | filtered | Extra      |
+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+--------+----------+-------------+
|  1 | SIMPLE      | a    | NULL      | ALL    | PRIMARY,c_1  | NULL    | NULL    | NULL      | 986400 |  100.00 | NULL        |
|  1 | SIMPLE      | b    | NULL      | eq_ref | PRIMARY,c_2  | PRIMARY | 4      | test.a.id |      1 |  100.00 | Using where |
+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+--------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)


mysql> explain select a.pad,b.pad from sbtest1 a,sbtest2 b where a.id=b.id and (a.c='123' or a.c='1234');
+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-----------------------+
| id | select_type | table | partitions | type  | possible_keys | key    | key_len | ref      | rows | filtered | Extra                |
+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-----------------------+
|  1 | SIMPLE      | a    | NULL      | range  | PRIMARY,c_1  | c_1    | 120    | NULL      |    2 |  100.00 | Using index condition |
|  1 | SIMPLE      | b    | NULL      | eq_ref | PRIMARY      | PRIMARY | 4      | test.a.id |    1 |  100.00 | NULL                  |
+----+-------------+-------+------------+--------+---------------+---------+---------+-----------+------+----------+-----------------------+
2 rows in set, 1 warning (0.00 sec)


mysql>


可以看出在多表查询的情况下or条件如果不在同一个表内执行计划表a的查询不走索引。


我们试试看用union all来进行改写


mysql> explain select a.pad,a.c from sbtest1 a,sbtest2 b where a.id=b.id and a.c='123' union all select a.pad

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇数据库监控(Zabbix) 下一篇关于 ORA-279 signalled during: ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目