为什么索引加法在此处的解释语句中没有任何变化?
Why index addition changed nothing in the explain statement here?
这是创建脚本 table:
CREATE DATABASE IF NOT EXISTS sailors;
USE sailors;
DROP TABLE IF EXISTS reserves;
DROP TABLE IF EXISTS sailors;
DROP TABLE IF EXISTS boats;
CREATE TABLE sailors
(
sid INTEGER NOT NULL ,
sname VARCHAR(20) NOT NULL ,
rating INTEGER NULL ,
age DECIMAL(5,2) NULL,
CONSTRAINT sailors_pk PRIMARY KEY (sid)
);
CREATE TABLE boats
(
bid INTEGER NOT NULL ,
bname VARCHAR(20) NOT NULL ,
color VARCHAR(10) NOT NULL ,
CONSTRAINT boats_pk PRIMARY KEY (bid)
);
CREATE TABLE reserves
(
sid INTEGER NOT NULL ,
bid INTEGER NOT NULL ,
day DATE NOT NULL ,
CONSTRAINT reserves_pk PRIMARY KEY (sid,bid,day)
, foreign key (sid) references sailors (sid)
, foreign key (bid) references boats (bid)
);
INSERT INTO sailors VALUES(22, 'Dustin', 7, 45.0);
INSERT INTO sailors VALUES(29, 'Brutus', 1, 33.0);
INSERT INTO sailors VALUES(31, 'Lubber', 8, 55.5);
INSERT INTO sailors VALUES(32, 'Audy', 8, 25.5);
INSERT INTO sailors VALUES(58, 'Rusty', 10, 35.5);
INSERT INTO sailors VALUES(64, 'Horatio',7, 35.0);
INSERT INTO sailors VALUES(71, 'Zorba', 10, 16.0);
INSERT INTO sailors VALUES(74, 'Horatio',9, 35.0);
INSERT INTO sailors VALUES(85, 'Art', 3, 25.5);
INSERT INTO sailors VALUES(95, 'Bob', 3, 63.5);
INSERT INTO boats VALUES(101, 'Interlake', 'blue');
INSERT INTO boats VALUES(102, 'Interlake', 'red');
INSERT INTO boats VALUES(103, 'Clipper', 'green');
INSERT INTO boats VALUES(104, 'Marine', 'red');
INSERT INTO boats VALUES(105, 'Clipper', 'white');
INSERT INTO reserves VALUES(22, 101, DATE '2012-10-10');
INSERT INTO reserves VALUES(22, 102, DATE '2012-10-10');
INSERT INTO reserves VALUES(22, 103, DATE '2012-08-10');
INSERT INTO reserves VALUES(22, 104, DATE '2012-07-10');
INSERT INTO reserves VALUES(31, 102, DATE '2012-10-11');
INSERT INTO reserves VALUES(31, 103, DATE '2012-06-11');
INSERT INTO reserves VALUES(31, 104, DATE '2012-12-11');
INSERT INTO reserves VALUES(64, 101, DATE '2012-05-09');
INSERT INTO reserves VALUES(64, 102, DATE '2012-08-09');
INSERT INTO reserves VALUES(74, 103, DATE '2012-08-09');
这是包含 EXPLAIN
语句及其结果的脚本。
EXPLAIN SELECT * FROM sailors ORDER BY rating;
# id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows, filtered, Extra
1, SIMPLE, sailors, , ALL, , , , , 10, 100.00, Using filesort
现在我正在添加索引并希望 EXPLAIN
语句结果随后发生变化:
CREATE INDEX rating ON sailors(rating);
CREATE INDEX age ON sailors(age);
现在我 运行 EXPLAIN
声明并看到我没有改变任何东西:
EXPLAIN SELECT * FROM sailors ORDER BY rating;
# id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows, filtered, Extra
1, SIMPLE, sailors, , ALL, , , , , 10, 100.00, Using filesort
为什么 EXPLAIN
语句没有任何变化?我期望的是一些性能改进。
使用二级索引(例如INDEX(rating)
)涉及在索引BTree 和数据BTree 之间跳动。确定这比简单地读取所有数据然后对其进行排序要慢。截断值各不相同,但大约是 table 的 20%——也就是说,如果您有一个 WHERE
子句过滤掉不到 20% 的数据(和 WHERE
和 ORDER BY
都可以一起处理),那么优化器会 可能 使用索引。
"filesort" 一词用词不当。它不一定涉及 "file" -- 它通常在 RAM 中完成。
这是创建脚本 table:
CREATE DATABASE IF NOT EXISTS sailors;
USE sailors;
DROP TABLE IF EXISTS reserves;
DROP TABLE IF EXISTS sailors;
DROP TABLE IF EXISTS boats;
CREATE TABLE sailors
(
sid INTEGER NOT NULL ,
sname VARCHAR(20) NOT NULL ,
rating INTEGER NULL ,
age DECIMAL(5,2) NULL,
CONSTRAINT sailors_pk PRIMARY KEY (sid)
);
CREATE TABLE boats
(
bid INTEGER NOT NULL ,
bname VARCHAR(20) NOT NULL ,
color VARCHAR(10) NOT NULL ,
CONSTRAINT boats_pk PRIMARY KEY (bid)
);
CREATE TABLE reserves
(
sid INTEGER NOT NULL ,
bid INTEGER NOT NULL ,
day DATE NOT NULL ,
CONSTRAINT reserves_pk PRIMARY KEY (sid,bid,day)
, foreign key (sid) references sailors (sid)
, foreign key (bid) references boats (bid)
);
INSERT INTO sailors VALUES(22, 'Dustin', 7, 45.0);
INSERT INTO sailors VALUES(29, 'Brutus', 1, 33.0);
INSERT INTO sailors VALUES(31, 'Lubber', 8, 55.5);
INSERT INTO sailors VALUES(32, 'Audy', 8, 25.5);
INSERT INTO sailors VALUES(58, 'Rusty', 10, 35.5);
INSERT INTO sailors VALUES(64, 'Horatio',7, 35.0);
INSERT INTO sailors VALUES(71, 'Zorba', 10, 16.0);
INSERT INTO sailors VALUES(74, 'Horatio',9, 35.0);
INSERT INTO sailors VALUES(85, 'Art', 3, 25.5);
INSERT INTO sailors VALUES(95, 'Bob', 3, 63.5);
INSERT INTO boats VALUES(101, 'Interlake', 'blue');
INSERT INTO boats VALUES(102, 'Interlake', 'red');
INSERT INTO boats VALUES(103, 'Clipper', 'green');
INSERT INTO boats VALUES(104, 'Marine', 'red');
INSERT INTO boats VALUES(105, 'Clipper', 'white');
INSERT INTO reserves VALUES(22, 101, DATE '2012-10-10');
INSERT INTO reserves VALUES(22, 102, DATE '2012-10-10');
INSERT INTO reserves VALUES(22, 103, DATE '2012-08-10');
INSERT INTO reserves VALUES(22, 104, DATE '2012-07-10');
INSERT INTO reserves VALUES(31, 102, DATE '2012-10-11');
INSERT INTO reserves VALUES(31, 103, DATE '2012-06-11');
INSERT INTO reserves VALUES(31, 104, DATE '2012-12-11');
INSERT INTO reserves VALUES(64, 101, DATE '2012-05-09');
INSERT INTO reserves VALUES(64, 102, DATE '2012-08-09');
INSERT INTO reserves VALUES(74, 103, DATE '2012-08-09');
这是包含 EXPLAIN
语句及其结果的脚本。
EXPLAIN SELECT * FROM sailors ORDER BY rating;
# id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows, filtered, Extra
1, SIMPLE, sailors, , ALL, , , , , 10, 100.00, Using filesort
现在我正在添加索引并希望 EXPLAIN
语句结果随后发生变化:
CREATE INDEX rating ON sailors(rating);
CREATE INDEX age ON sailors(age);
现在我 运行 EXPLAIN
声明并看到我没有改变任何东西:
EXPLAIN SELECT * FROM sailors ORDER BY rating;
# id, select_type, table, partitions, type, possible_keys, key, key_len, ref, rows, filtered, Extra
1, SIMPLE, sailors, , ALL, , , , , 10, 100.00, Using filesort
为什么 EXPLAIN
语句没有任何变化?我期望的是一些性能改进。
使用二级索引(例如INDEX(rating)
)涉及在索引BTree 和数据BTree 之间跳动。确定这比简单地读取所有数据然后对其进行排序要慢。截断值各不相同,但大约是 table 的 20%——也就是说,如果您有一个 WHERE
子句过滤掉不到 20% 的数据(和 WHERE
和 ORDER BY
都可以一起处理),那么优化器会 可能 使用索引。
"filesort" 一词用词不当。它不一定涉及 "file" -- 它通常在 RAM 中完成。