生成表和索引的sql:
DROP SCHEMA IF EXISTS sakila;
CREATE SCHEMA sakila;
USE sakila;
CREATE TABLE payment (
payment_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
-- 取消注释下面的列, 会对explain的结果产生影响
-- customer_id SMALLINT UNSIGNED NOT NULL,
amount DECIMAL(5,2) NOT NULL,
payment_date DATETIME NOT NULL,
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
constraint id_UNIQUE
unique (payment_id)
);
alter table payment add index
idx_union_index_test (amount, payment_date, last_update);
explain:
explain select * from payment where
payment_date ='2006-02-15 22:12:32'
and last_update = '2006-02-15 22:12:32';
疑问:
如果注释掉customer_id列, 执行explain发现是使用了索引 idx_union_index_test 的, 但是根据"匹配最左前缀"原则, 应该无法利用到索引的啊
如果取消注释customer_id列, 再次生成表和索引并explain, 这个时候发现type=ALL了
为什么会产生这样不同的结果呢?
你的表里有数据吗?