MySQL:加速两个表的连接
我有一个简单的查询连接,在表MySQL:加速两个表的连接,mysql,Mysql,我有一个简单的查询连接,在表ads和make之间,表ads大约有71k行,而make只有80行 但是下面这个简单的查询平均需要0.98-1.1秒。有没有办法加快速度 SELECT t2.*, COUNT(*) AS count FROM `ads` AS t JOIN (`make` AS t2) ON (t.make_id=t2.id) WHERE t.pending!=1 GROUP BY t2.make
ads
和make
之间,表ads
大约有71k行,而make
只有80行
但是下面这个简单的查询平均需要0.98-1.1秒。有没有办法加快速度
SELECT t2.*, COUNT(*) AS count
FROM `ads` AS t
JOIN (`make` AS t2)
ON (t.make_id=t2.id)
WHERE t.pending!=1
GROUP BY t2.make
ORDER BY `count` DESC
LIMIT 24
更新:
添加了EXPLAIN
,我明白了
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | SIMPLE | t2 | ALL | PRIMARY | NULL | NULL | NULL | 80 | Using temporary; Using filesort
1 | SIMPLE | t | ref | pending,make_id| make_id| 1 |dev.t.make_id| 1010 | Using where
好吧,我不知道为什么这比加入更有效
`SELECT t2.make, COUNT(t.make_id) AS Count
FROM `ads` AS t, `make` AS t2
WHERE t.make_id=t2.id AND t.pending!=1
GROUP BY t.make_id
ORDER BY Count DESC
LIMIT 24`
但现在它只需0.3秒是的。清空桌子!这会加快行动,但更重要的是。你应该检查
explain
你有没有试着把explain
放在它前面,看看什么东西慢?是否为t2。是否为编制索引?t.make\u id
索引了吗?t2.id
索引了吗?t.pending
索引了吗?另外,您是按计数排序的,因此如果您有很多结果(您需要在执行限制之前进行排序),这也会很昂贵。@Nanne一个包含80
行的文件/内存排序不应该是问题(他是按t2.make
进行分组的)。OP:关于您的解释,您可以看到它对t
使用了什么文件排序,因为您的列没有索引。索引t.pending
和t.make\u id
@h2oooooo更新的问题,包括解释
结果