I have a table below:
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;
I want to know, if I execute the following two SQLs, do they have the same performance?
mysql> select * from student where name='John' and city='NewYork';
mysql> select * from student where city='NewYork' and name='John';
Involved question:
I execute explain on the two of them, the result is below:
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 |
+----+-------------+---------+-------+---------------+---------+---------+-------------+------+-------+
mysql> explain 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 |
+----+-------------+---------+-------+---------------+---------+---------+-------------+------+-------+
No. The order of columns in the WHERE clause does not matter at all.
The order of filters in the WHERE clause does not matter. The SFDC Optimizer evaluates all filters to look for the indexed and most selective one.
The optimal index for your queries can be summarized: The first columns in the index must be all the = columns in the WHERE , in -any_ order. The last columns in the index must be the ORDER BY columns in the same order, and either be all ASC or all DESC . (MySQL 8.0 has an exception here.)
column order does not matter. This is purely a convenience feature. just to allow you to restructure your database table the way you like after it has been created.
If, given an index on
(name,city)
, I execute the following two SQLs, do they have the same performance?
where name='John' and city='NewYork'
where city='NewYork' and name='John'
Yes.
The query planner doesn't care about the order of WHERE
clauses. If both your clauses filter on equality, the planner can use the index. SQL is a declarative language, not procedural. That is, you say what you want, not how to get it. It's a little counterintuitive for many programmers.
It can also use the (name,city)
index for WHERE name LIKE 'Raymo%'
because name
is first in the index. It cannot use that index for WHERE city = 'Kalamazoo'
, though.
It can use the index for WHERE city LIKE 'Kalam%' AND name = 'Raymond'
. In that case it uses the index to find the name value, then scans for matching cities.
If you had an index on (city,name)
you could also use that for WHERE city = 'Kalamazoo' AND name = 'Raymond'
. If both indexes exist, the query planner will pick one, probably based on some kind of cardinality consideration.
Note. If instead you have two different indexes on city
and name
, the query planner can't (as of mid-2017) use more than one of them to satisfy WHERE city = 'Kalamazoo' AND name = 'Raymond'
.
http://use-the-index-luke.com/ for good info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With