Ray*_*Hsu 5 mysql sql indexing query-performance
我有下表:
CREATE TABLE `student` (
`name` varchar(30) NOT NULL DEFAULT '',
`city` varchar(30) NOT NULL DEFAULT '',
`age` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`name`,`city`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Run Code Online (Sandbox Code Playgroud)
我想知道,如果我执行以下两条SQL,它们的性能是否相同?
mysql> select * from student where name='John' and city='NewYork';
mysql> select * from student where city='NewYork' and name='John';
Run Code Online (Sandbox Code Playgroud)
涉及问题:
我对他们两个执行解释,结果如下:
mysql> explain select * from student where name='John' and city='NewYork';
+----+-------------+---------+-------+---------------+---------+---------+-------------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+-------------+------+-------+
| 1 | SIMPLE | student | const | PRIMARY | PRIMARY | 184 | const,const | 1 | NULL |
+----+-------------+---------+-------+---------------+---------+---------+-------------+------+-------+
Run Code Online (Sandbox Code Playgroud)
mysql> 解释 select * from student where city='NewYork' and name='John';
+----+-------------+---------+-------+---------------+---------+---------+-------------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------+---------+---------+-------------+------+-------+
| 1 | SIMPLE | student | const | PRIMARY | PRIMARY | 184 | const,const | 1 | NULL |
+----+-------------+---------+-------+---------------+---------+---------+-------------+------+-------+
Run Code Online (Sandbox Code Playgroud)
如果给定 上的索引
(name,city),我执行以下两条 SQL,它们是否具有相同的性能?
where name='John' and city='NewYork'
where city='NewYork' and name='John'
是的。
查询规划器不关心WHERE子句的顺序。如果您的两个子句都按相等性进行过滤,则规划器可以使用索引。SQL 是一种声明性语言,而不是过程性语言。也就是说,你说你想要什么,而不是如何得到它。对于许多程序员来说,这有点违反直觉。
它还可以使用(name,city)for 索引,WHERE name LIKE 'Raymo%'因为name它位于索引中的第一个。但它不能将该索引用于WHERE city = 'Kalamazoo'。
它可以使用索引WHERE city LIKE 'Kalam%' AND name = 'Raymond'。在这种情况下,它使用索引来查找名称值,然后扫描匹配的城市。
如果您有索引, (city,name)也可以将其用于WHERE city = 'Kalamazoo' AND name = 'Raymond'. 如果两个索引都存在,查询规划器将选择一个,可能基于某种基数考虑。
笔记。city相反,如果和上有两个不同的索引name,则查询规划器无法(截至 2017 年中期)使用其中多个索引来满足WHERE city = 'Kalamazoo' AND name = 'Raymond'。
http://use-the-index-luke.com/ 获取有用信息。