select c.username,c.sex,c.attentions,c.fans,a.id,a.content,a.isnum,a.times,a.turm,a.keep,a.comments,a.uid,b.mini,b.mednum,b.maxs
from (wb_userinfo c left join wb_weibo a on c.uid = a.uid)
left join wb_picture b on a.id = b.wid where a.uid in(select follow from wb_follow where fans = 27) or a.uid = 27 order by a.times limit 0,10;
select c.username,c.sex,c.attentions,c.fans,a.id,a.content,a.isnum,a.times,a.turm,a.keep,a.comments,a.uid,b.mini,b.mednum,b.maxs
from (wb_userinfo c left join wb_weibo a on c.uid = a.uid)
left join wb_picture b on a.id = b.wid where a.uid in(select follow from wb_follow where fans = 27) or a.uid = 27 order by a.times limit 0,10;
有用or的可以考虑换成用union来尝试下
<code>(SELECT c.username,c.sex,c.attentions,c.fans,a.id,a.content,a.isnum,a.times,a.turm,a.keep,a.comments,a.uid,b.mini,b.mednum,b.maxs FROM (wb_userinfo c LEFT JOIN wb_weibo a ON c.uid = a.uid) LEFT JOIN wb_picture b ON a.id = b.wid WHERE a.uid IN(SELECT follow FROM wb_follow WHERE fans = 27) ) union (SELECT c.username,c.sex,c.attentions,c.fans,a.id,a.content,a.isnum,a.times,a.turm,a.keep,a.comments,a.uid,b.mini,b.mednum,b.maxs FROM (wb_userinfo c LEFT JOIN wb_weibo a ON c.uid = a.uid) LEFT JOIN wb_picture b ON a.id = b.wid WHERE a.uid = 27 ) ORDER BY a.times LIMIT 0,10;</code>
使用explain看看分析报告贴出来看下,ref的值与row是的值索引使用情况等,连表查询的话我正常建议是拆成简单查询来。
就sql而言,可优化的空间不大,建议拆成若干条简单的sql,在代码里实现最终业务逻辑。
PS:尽量避免在sql里出现or
不要用join
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号