MySQL 中 MIN 列值和 CHAR 列上的 SELECT 非常慢
我有理由确信这个问题的答案在于有一个不同的索引。我有一个查询速度慢得不合理,但只有当它采用以下完整形式时,如果我删除查询的部分内容,它的速度就会非常快,我怎样才能让它变得更好?
慢:
SELECT json
FROM requests
WHERE spider = 'foo'
AND load_count = ( SELECT MIN( load_count ) FROM requests )
AND load_count < 50
LIMIT 500;
解释:
+----+-------------+----------+------+-------------------------+--------------+---------+-------+--------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+-------------------------+--------------+---------+-------+--------+------------------------------+
| 1 | PRIMARY | requests | ref | load_count,spider_index | spider_index | 90 | const | 200845 | Using where |
| 2 | SUBQUERY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+----------+------+-------------------------+--------------+---------+-------+--------+------------------------------+
数据库结构:
CREATE TABLE `requests` (
`added` int(11) NOT NULL AUTO_INCREMENT,
`url` char(255) NOT NULL,
`spider` char(30) NOT NULL,
`referer` char(255) DEFAULT NULL,
`json` text NOT NULL,
`load_count` int(11) NOT NULL DEFAULT '0',
`processed` tinyint(1) NOT NULL DEFAULT '0',
`invalid` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`added`),
UNIQUE KEY `url` (`url`),
KEY `load_count` (`load_count`),
KEY `spider_index` (`spider`)
) ENGINE=MyISAM AUTO_INCREMENT=5285840 DEFAULT CHARSET=utf8
像 Neo 建议的那样更新索引后,我得到了巨大的改进:
+----+-------------+----------+------+-------------------+-------------------+---------+-------------+------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+-------------------+-------------------+---------+-------------+------+------------------------------+
| 1 | PRIMARY | requests | ref | spider_load_count | spider_load_count | 94 | const,const | 1487 | Using where |
| 2 | SUBQUERY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+----------+------+-------------------+-------------------+---------+-------------+------+------------------------------+
I'm reasonable sure the answer to this lies in having a different index. I have a query that's unreasonably slow, but only when it's in the following complete form, if I remove parts of the query it's blazing fast, how can I make it better?
Slow:
SELECT json
FROM requests
WHERE spider = 'foo'
AND load_count = ( SELECT MIN( load_count ) FROM requests )
AND load_count < 50
LIMIT 500;
EXPLAIN:
+----+-------------+----------+------+-------------------------+--------------+---------+-------+--------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+-------------------------+--------------+---------+-------+--------+------------------------------+
| 1 | PRIMARY | requests | ref | load_count,spider_index | spider_index | 90 | const | 200845 | Using where |
| 2 | SUBQUERY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+----------+------+-------------------------+--------------+---------+-------+--------+------------------------------+
Database structure:
CREATE TABLE `requests` (
`added` int(11) NOT NULL AUTO_INCREMENT,
`url` char(255) NOT NULL,
`spider` char(30) NOT NULL,
`referer` char(255) DEFAULT NULL,
`json` text NOT NULL,
`load_count` int(11) NOT NULL DEFAULT '0',
`processed` tinyint(1) NOT NULL DEFAULT '0',
`invalid` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`added`),
UNIQUE KEY `url` (`url`),
KEY `load_count` (`load_count`),
KEY `spider_index` (`spider`)
) ENGINE=MyISAM AUTO_INCREMENT=5285840 DEFAULT CHARSET=utf8
After updating my index like Neo suggested I get drastic improvements:
+----+-------------+----------+------+-------------------+-------------------+---------+-------------+------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+-------------------+-------------------+---------+-------------+------+------------------------------+
| 1 | PRIMARY | requests | ref | spider_load_count | spider_load_count | 94 | const,const | 1487 | Using where |
| 2 | SUBQUERY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+----------+------+-------------------+-------------------+---------+-------------+------+------------------------------+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这又如何呢?
拥有蜘蛛场索引可能会对您有所帮助。
What about this?
And having index on spider field may help you.
一些评论/建议:
A few comments/suggestions: