Mysql 不使用索引的Join和索引完全相同

Mysql 不使用索引的Join和索引完全相同,mysql,indexing,Mysql,Indexing,我不确定为什么这个查询没有使用表world\u cities上的索引。索引是相同的,顺序也是相同的。数据类型的类型和长度相同。唯一的区别是键名 那么为什么不使用该键呢? 如果我运行查询,我会得到:318824行(2分钟51.30秒) 以下是我所做的: explain select * from zip_codes zc inner join world_cities wc on zc.city = wc.city and zc.country = wc.country

我不确定为什么这个查询没有使用表
world\u cities
上的索引。索引是相同的,顺序也是相同的。数据类型的类型和长度相同。唯一的区别是
键名

那么为什么不使用该键呢?
如果我运行查询,我会得到:
318824行(2分钟51.30秒)

以下是我所做的:

explain select * from zip_codes zc
inner join world_cities wc on 
    zc.city = wc.city 
    and zc.country = wc.country 
    and zc.region = wc.region;

+----+-------------+-------+------+---------------------+---------------------+---------+---------------------------------------------------------------+---------+-------------+
| id | select_type | table | type | possible_keys       | key                 | key_len | ref                                                           | rows    | Extra       |
+----+-------------+-------+------+---------------------+---------------------+---------+---------------------------------------------------------------+---------+-------------+
|  1 | SIMPLE      | wc    | ALL  | city_country_region | NULL                | NULL    | NULL                                                          | 3173958 |             |
|  1 | SIMPLE      | zc    | ref  | country_region_city | country_region_city | 165     | largedbapi.wc.city,largedbapi.wc.country,largedbapi.wc.region |       1 | Using where |
+----+-------------+-------+------+---------------------+---------------------+---------+---------------------------------------------------------------+---------+-------------+


mysql> show indexes from world_cities where Key_name like '%city%';
+--------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table        | Non_unique | Key_name            | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| world_cities |          1 | city_country_region |            1 | city        | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |
| world_cities |          1 | city_country_region |            2 | country     | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |
| world_cities |          1 | city_country_region |            3 | region      | A         |        NULL |     NULL | NULL   | YES  | BTREE      |         |
+--------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+


mysql> show indexes from zip_codes where Key_name like '%city%';
+-----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table     | Non_unique | Key_name            | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| zip_codes |          1 | country_region_city |            1 | city        | A         |      218424 |     NULL | NULL   | YES  | BTREE      |         |
| zip_codes |          1 | country_region_city |            2 | country     | A         |      218424 |     NULL | NULL   | YES  | BTREE      |         |
| zip_codes |          1 | country_region_city |            3 | region      | A         |      436849 |     NULL | NULL   | YES  | BTREE      |         |
+-----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

世界城市上的城市/国家/地区键的基数为空,而要使用非空基数。在上面运行分析应该可以修复它

ANALYZE TABLE world_cities

还可以查看有关空基数的更多信息。

谢谢!我现在正在运行ANALYZE,我们将看到happensIt仍然没有使用它应该能够使用的键。您正在运行的确切查询是什么?等等,您知道联接已经在使用您的索引,对吗?