[英]mysql query scanning all rows
Guys trying to wrap my head around mysql query on why it is scanning all rows in table 伙计们试图绕过mysql查询,以了解为什么它正在扫描表中的所有行
I have 2 tables topic_entry and topic_user 我有2个表topic_entry和topic_user
CREATE TABLE `topic_entry` (
`entry_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`topic_id` bigint(20) unsigned NOT NULL,
`entry_created` datetime NOT NULL,
`entry_object` text,
`level` tinyint(4) NOT NULL DEFAULT '3',
PRIMARY KEY (`entry_id`),
KEY `entry_created` (`entry_created`),
KEY `level` (`level`),
KEY `topic_id_2` (`topic_id`,`entry_id`)
) ENGINE=MyISAM;
CREATE TABLE `topic_user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`topic_id` bigint(20) unsigned NOT NULL,
`user_id` varchar(100) NOT NULL,
`private` enum('y','n') DEFAULT NULL,
`freq` enum('a','d','w') DEFAULT NULL,
`topic_id_freq` varchar(10) DEFAULT NULL,
`casematch` enum('0','1') DEFAULT '0',
`textmatch` enum('0','1') DEFAULT '0',
`topic_name_case` varchar(100) DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `topic_id_user_id` (`topic_id`,`user_id`),
KEY `user_id` (`user_id`),
KEY `topic_id_freq` (`topic_id_freq`)
) ENGINE=MyISAM;
This is the query that i am trying to run 这是我要运行的查询
explain
select te.topic_id,te.entry_id
from topic_entry te
WHERE te.topic_id in (select topic_id
from topic_user where user_id ='xyz')
AND te.level=4
AND te.entry_id > 0
ORDER BY te.entry_id DESC
LIMIT 5;
The explain output shows that it is scanning all rows 说明输出显示它正在扫描所有行
| 1 | PRIMARY| te | range | PRIMARY,level | PRIMARY | 8 | NULL| **722978** | Using where |
| 2 | DEPENDENT SUBQUERY | topic_user | unique_subquery | topic_id_user_id,user_id | topic_id_user_id | 310 | func,const | 1 | Using index; Using where |
Try this: 尝试这个:
EXPLAIN
SELECT te.topic_id,te.entry_id
FROM topic_entry te
JOIN topic_user tu ON te.topic_id = tu.topic_id AND tu.user_id = 'xyz'
WHERE te.level=4
AND te.entry_id > 0
ORDER BY te.entry_id DESC
LIMIT 5;
Try changing the sequence of keys in your WHERE clause: 尝试更改WHERE子句中的键顺序:
WHERE te.topic_id in (select topic_id from topic_user where user_id ='xyz') AND te.level=4 AND te.entry_id > 0
to 至
WHERE te.topic_id in (select topic_id from topic_user where user_id ='xyz') AND te.entry_id > 0 AND te.level=4
I am not 100% sure (and I can not test it at the moment) but it may help. 我不确定100%(目前无法测试),但可能会有所帮助。
The reason being that MySQL tries and fails to map your keys 原因是MySQL尝试无法映射您的密钥
topic_id, level, entry_id
to KEY topic_id_2
and fails because of the extra field. topic_id, level, entry_id
到KEY topic_id_2
,由于附加字段而失败。
PS and "Sly Raskal" has even better solution, or you can try to combine both PS和“ Sly Raskal”具有更好的解决方案,或者您可以尝试将两者结合起来
What about 关于什么
explain extended
select te.topic_id,te.entry_id
from topic_entry te LEFT JOIN topic_user tu ON
te.topic_id = tu.topic_id AND tu.user_id = 'xyz'
WHERE 1 = 1
AND te.level=4
AND te.entry_id > 0
ORDER BY te.entry_id DESC
LIMIT 5;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.