2
我有2个表 - profile
与1,500,000记录和indicator_trades
与12,000,000记录。以下查询返回0个结果,大约需要10秒才能完成。MySQL查询不正确使用索引?
SELECT `trd`.`symbol`, `p`.`type`
FROM `indicator_trades` AS `trd`
INNER JOIN `profile` AS `p` ON `p`.`symbol` = `trd`.`symbol`
WHERE `start_date` >= '2014-09-17' AND `p`.`type` = 2
DESCRIBE结果:
+----+-------------+-------+--------+-----------------------+------------+---------+--------------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+-----------------------+------------+---------+--------------------------+------+-------------+
| 1 | SIMPLE | trd | range | IDX_symbol,start_date | start_date | 4 | NULL | 3662 | Using where |
| 1 | SIMPLE | p | eq_ref | IDX_symbol,type | IDX_symbol | 34 | barchart_data.trd.symbol | 1 | Using where |
+----+-------------+-------+--------+-----------------------+------------+---------+--------------------------+------+-------------+
10秒似乎是一个异常长的时间,这个查询时,它使用的密钥,但它只是花这么长时间从该1200万,大肆削减3662个记录indicator_trades
表?
SHOW上创建两个表:
CREATE TABLE `indicator_trades` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`symbol` varchar(32) NOT NULL,
`indicator_code` varchar(10) NOT NULL,
`start_date` date DEFAULT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `IDX_symbol` (`symbol`) USING BTREE,
KEY `start_date` (`start_date`),
KEY `indicator_code` (`indicator_code`)
) ENGINE=InnoDB AUTO_INCREMENT=12582721 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT |
CREATE TABLE `profile` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`symbol` varchar(32) NOT NULL,
`type` int(11) DEFAULT NULL,
`lastupdate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `IDX_symbol` (`symbol`) USING BTREE,
KEY `type` (`type`),
KEY `exchange` (`exchange`)
) ENGINE=InnoDB AUTO_INCREMENT=320948248 DEFAULT CHARSET=latin1
它需要连接1200万行。我认为这是内部连接,使它变慢。 – 2014-10-08 20:28:13
@ S.Pols不应该只需要连接3662条记录,因为优化器应该在加入之前减少WHERE的数据集? – 2014-10-08 20:36:08
这听起来像'innodb_buffer_pool_size'变量是非常低的,你正在击中磁盘而不是RAM。 'SHOW VARIABLES LIKE'%buffer_pool%';'show? – 2014-10-09 07:33:56