【发布时间】:2017-05-14 06:11:50
【问题描述】:
我正在尝试优化涉及两个表的左连接,但我无法通过可能的索引来加快速度。 表 1 包含 2171289 行:
text_metadata_for_nzcorpus | CREATE TABLE `text_metadata_for_nzcorpus` (
`text_id` varchar(255) NOT NULL,
`newspaper` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`year` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`month` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`day` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`section` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`subsection` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`topics` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`words` int(11) NOT NULL DEFAULT '0',
`cqp_begin` bigint(20) unsigned NOT NULL DEFAULT '0',
`cqp_end` bigint(20) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`text_id`),
KEY `newspaper` (`newspaper`),
KEY `year` (`year`),
KEY `month` (`month`),
KEY `day` (`day`),
KEY `section` (`section`),
KEY `subsection` (`subsection`),
KEY `topics` (`topics`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
第二个表只包含8584行:
db_dist_fb8ddyk760 | CREATE TABLE `db_dist_fb8ddyk760` (
`text_id` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`beginPosition` int(11) DEFAULT NULL,
`endPosition` int(11) DEFAULT NULL,
`refnumber` mediumint(9) NOT NULL AUTO_INCREMENT,
KEY `refnumber` (`refnumber`),
KEY `text_id` (`text_id`)
) ENGINE=InnoDB AUTO_INCREMENT=16384 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
我需要运行以下类型的查询:
SELECT md.day as handle, count(db.text_id) as hits,
count(distinct db.text_id) as files FROM text_metadata_for_nzcorpus as md
LEFT JOIN db_dist_fb8ddyk760 as db on md.text_id = db.text_id
GROUP BY md.day;
这目前需要 5 秒以上的时间来处理。由于它是在网页上显示输出之前需要运行的众多查询之一,因此如果可能的话,我想加快速度。这是“解释”的输出:
+----+-------------+-------+-------+---------------+---------+---------+----------------------+---------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+----------------------+---------+--------------------------+
| 1 | SIMPLE | md | index | day | day | 768 | NULL | 2452080 | Using index |
| 1 | SIMPLE | db | ref | text_id | text_id | 768 | cqpweb_db.md.text_id | 1 | Using where; Using index |
+----+-------------+-------+-------+---------------+---------+---------+----------------------+---------+--------------------------+
任何有用的建议将不胜感激。 (我不是系统的开发人员,也不负责代码本身 - 但如果可以改进,我想向程序员提供输入......)
非常感谢! 塞巴斯蒂安
【问题讨论】:
标签: mysql optimization indexing left-join