我有这个查询
SELECT id FROM btn
WHERE parentid =0
AND (mem_id =ANY(SELECT mem_id FROM network WHERE frd_id='401') || mem_id ='401')
ORDER BY btn.date DESC LIMIT 0,20
&安培;这个查询
SELECT mem_id FROM net WHERE frd_id='401'
给我这样的结果
mem_id
34
45
633
24
22
我想优化上面的主要查询,目前扫描了13,373条btn
表
请建议我如何优化此查询?
thnks
答案 0 :(得分:1)
您需要索引搜索的值。所以,基于以上两点:
parentid, frd_id, mem_id
这应该有很大帮助...
答案 1 :(得分:0)
SELECT DISTINCT b.id FROM btn b
INNER JOIN network n ON (n.mem_id = b.mem_id)
WHERE b.parentid = '0'
AND ('401' IN (n.frd_id, n.mem_id))
ORDER BY b.date DESC
LIMIT 20 OFFSET 0
确保
上有索引btn.mem_id, btn.parentid, btn.date
network.mem_id, network.frd_id