2013-08-24 42 views
0

我不確定爲什麼這個查詢沒有使用表world_cities上的索引。索引相同且順序相同。數據類型具有相同的類型和相同的長度。唯一的區別是key_name加入不使用索引和索引完全一樣

那麼爲什麼它不使用密鑰?
如果我運行查詢,我得到:318824 rows in set (2 min 51.30 sec)

這裏是我做了什麼:在world_cities

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  |   | 
+-----------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 

回答

0

你city_country_region鍵具有空基數,而一個非空一個需要它被使用。運行分析應該解決它。

ANALYZE TABLE world_cities 

另請參閱this post以瞭解有關NULL基數的更多信息。

+0

謝謝!我現在正在運行ANALYZE,我們將看到會發生什麼 –

+0

它仍然不使用密鑰 –

+0

它應該能夠。什麼是你正在運行的確切查詢?等等,你知道這個連接已經在使用你的索引了,對吧? – vollie