Mysql 为什么不是';没有使用索引吗?
Mysql 为什么不是';没有使用索引吗?,mysql,indexing,Mysql,Indexing,EXPLAIN为我提供了以下信息: SELECT `Nen Straatnaam` as street, `Nen Woonplaats` as city, Gemeente, Postcode, acn_distinct.zipcodes, acn_distinct.lat, acn_distinct.lng FROM `acn_distinct` INNER JOIN crimes as c ON `Nen Woonplaats` = c.place AND c.street
EXPLAIN
为我提供了以下信息:
SELECT `Nen Straatnaam` as street, `Nen Woonplaats` as city, Gemeente,
Postcode, acn_distinct.zipcodes, acn_distinct.lat, acn_distinct.lng
FROM `acn_distinct` INNER JOIN crimes as c
ON `Nen Woonplaats` = c.place AND c.street_check = 0
ORDER BY street ASC
那么它为什么不使用建议的索引呢?它使用的是一个索引,
idx\u street\u check
,但是,性能会很糟糕,因为它还创建了一个临时表,并使用了filesort
,两者都是臭名昭著的罪魁祸首
问题是为什么它不使用犯罪索引。place
。我会尝试在(地点、街道检查)上创建多列索引
但更重要的是,我认为您的表模式非常非常糟糕。您的联接格式不正确:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE c ref idx_place,idx_street_check,fulltext_place idx_street_check 1 const 67556 Using temporary; Using filesort
1 SIMPLE acn_distinct ref ID_nen_woonplaats ID_nen_woonplaats 768 crimes.c.place 42 Using index condition
当你在表a和表B之间进行连接时,你应该这样连接:a.x=B.y。但在这种情况下,你甚至不是指表a。你是用a的行数乘以B的特定子集。有一个问题我可能是错的,但连接条件似乎像是where子句;acn_distinct table在特定列上连接犯罪表?它使用索引;explain info中的
键
列提供了mysql正在使用的索引。@collone三十二不应该使用索引编写,或者在第一行上也使用索引条件吗?@Gordon Linoff:有对第二个表的引用Nen Woonplants
是第二个表中的一列。它不是限定的,所以我们不能从SQL文本中确定(这是一个示例,使用表别名限定每个列引用的最佳实践将有助于读者)。解释输出显示名为ID\u nen\u woonplaats
的索引用于访问第二个表,引用为c.place,确认这是该表中的一列。@Buar使用索引表示使用了ID\u nen\u woonplaats
此处可以用作索引(这就是为什么它位于键
列中的原因),但不能用作覆盖索引,这就是为什么使用索引
不会出现的原因。对于答案,Thnx将尝试添加(地点、街道检查)索引。那么我应该如何格式化这个查询呢?只需添加acn\u distinct
Nen Woonplaats
=c.place即可将两个表从各自的表中连接到一列中。Nen Woonplaats
是否是表中的一列acn_distinct
?请将descripe acn_distinct
的输出添加到您的帖子中。@Ktm5124:是的,这是acn_distinct
中的一列。解释输出证实了这一点;索引名ID\u nen\u woonplaats
带有c.place
中的引用。此示例说明了为什么使用表别名限定每个列引用是最佳做法。
FROM `acn_distinct` INNER JOIN crimes as c
ON `Nen Woonplaats` = c.place AND c.street_check = 0