以下查询将导致“检查每个记录的范围(索引映射:0x7)”。 我期望,因为有一个lft + rght索引,它将使用此索引。我已经在文章Mysql range check instead of index usage on inner join上找到了,但是此表使用了自己的结构,因此列类型完全相同。我使用的是MySQL 5.7。
DROP TABLE IF EXISTS test.tree;
CREATE TABLE test.tree (
lft INT(10) UNSIGNED NOT NULL,
rght INT(10) UNSIGNED NOT NULL,
INDEX ix_left_right (lft, rght),
INDEX ix_left (lft),
INDEX ix_right (rght)
);
EXPLAIN
SELECT X.*
FROM test.tree X
LEFT JOIN test.tree Y ON X.lft BETWEEN Y.lft AND Y.rght;
结果
{
"table": "UnknownTable",
"rows":
[
{
"id": 1,
"select_type": "SIMPLE",
"table": "X",
"partitions": null,
"type": "index",
"possible_keys": null,
"key": "ix_left_right",
"key_len": "8",
"ref": null,
"rows": 1,
"filtered": 100.00,
"Extra": "Using index"
},
{
"id": 1,
"select_type": "SIMPLE",
"table": "Y",
"partitions": null,
"type": "ALL",
"possible_keys": "ix_left_right,ix_left,ix_right",
"key": null,
"key_len": null,
"ref": null,
"rows": 1,
"filtered": 100.00,
"Extra": "Range checked for each record (index map: 0x7)"
}
]
}
有人知道我在做什么吗?