我有一個MySQL查詢運行非常緩慢,因爲它沒有使用連接鍵上的索引。我的表結構的優化器沒有使用索引有什麼問題?MySQL查詢不使用索引
mysql> EXPLAIN SELECT * FROM indicator_performance_averages
LEFT OUTER JOIN `prof` ON (`prof`.`symbol` = indicator_performance_averages.`symbol`)
WHERE (indicator_performance_averages.`symbol` = 'ZCN13');
+----+-------------+--------------------------------+-------+---------------+------------+---------+-------+---------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------------------------------+-------+---------------+------------+---------+-------+---------+-------+
| 1 | SIMPLE | indicator_performance_averages | const | idx_symbol | idx_symbol | 98 | const | 1 | |
| 1 | SIMPLE | prof | ALL | NULL | NULL | NULL | NULL | 1102075 | |
+----+-------------+--------------------------------+-------+---------------+------------+---------+-------+---------+-------+
mysql> DESCRIBE indicator_performance_averages;
+--------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| symbol | varchar(32) | NO | UNI | NULL | |
| date | date | YES | | NULL | |
| last_update | timestamp | YES | | NULL | |
+--------------------+------------------+------+-----+---------+----------------+
mysql> DESCRIBE prof;
+---------------+---------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------------+------+-----+---------------------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| symbol | varchar(32) | NO | UNI | NULL | |
| name | varchar(128) | NO | | NULL | |
| lastupdate | datetime | NO | | 0000-00-00 00:00:00 | |
+---------------+---------------------+------+-----+---------------------+----------------+
編輯:添加SHOW CREATE TABLE兩個表:
CREATE TABLE `indicator_performance_averages` (
`id` int(11) unsigned NOT NULL auto_increment,
`symbol` varchar(32) character set utf8 NOT NULL,
`date` date default NULL,
`last_update` timestamp NULL default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_symbol` (`symbol`)
) ENGINE=InnoDB AUTO_INCREMENT=6719 DEFAULT CHARSET=latin1
CREATE TABLE `prof` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`symbol` varchar(32) NOT NULL,
`name` varchar(128) NOT NULL,
`lastupdate` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `IDX_symbol` (`symbol`),
) ENGINE=InnoDB AUTO_INCREMENT=37736071 DEFAULT CHARSET=latin1
MySQl在JOIN上使用'indicator_performance_averages.idx_symbol'索引。它只會在JOIN上使用一個索引,因爲它已經知道另一端的值。 – 2013-07-11 21:21:20
使用什麼字符集編碼各自的「符號」列? – eggyal
我們期望'prof.symbol'上的唯一鍵在計劃的possible_keys列中顯示。這裏沒有足夠的信息來調試問題。我認爲我們需要'SHOW CREATE TABLE'的輸出來表示這兩個表,以便發現問題。由於這是一個VARCHAR列,可能的嫌疑犯是字符集或排序規則的區別。 – spencer7593