我需要优化MySQL查询。我有一张桌子'投注'少于100.000条记录。我需要显示当月的所有记录。
SELECT betting.id
FROM betting, members
WHERE members.status = 1
and members.id = betting.user
and betting.date between '2016-09-01' and '2016-09-30'
and betting.status = 1
and betting.type = 1;
当我尝试解释命令时:
SIMPLE betting range user,date,type,type_2 type_2 7 NULL 4966 Using where
SIMPLE members eq_ref PRIMARY PRIMARY 4 betting.user 1 Using where
有没有机会减少数据库搜索?问题是显示此结果的页面调用过于频繁,导致大量CPU成本。不幸的是我无法缓存结果,因为它们需要生效。有什么想法吗?
答案 0 :(得分:1)
以下是以更容易(至少对我而言)理解的格式编写的查询:
SELECT b.id
FROM betting b join
members m
ON m.id = b.user
WHERE m.status = 1
b.date between '2016-09-01' and '2016-09-30' and
b.status = 1 and
b.type = 1;
我认为最好的索引策略是:betting(status, type, date, user)
和members(id, status)
。