sql 语句优化

在最近需求中,加入了大量的统计:其中有个统计是统计某日帖子的被评论人数的,

1. 刚开始为了实现功能,简单得写了下:

select count(*) as num,post_uid from (select count(uid),post_uid from b where psot_id in(x,x) group by uid) a group by post_uid

写完后感觉不舒服,从效率、和阅读等方面考虑,决定优化一下:

select count(distict(uid)) as num,post_uid from a where * group bu * 

2. 查看服务器慢日志发现有条sql (select * from a where * limit 1000),其中还是循环去取的,优化为

select field1,feild2 from a where * limig 1000

过段时间再看,慢日志中已经没有了。

综合考虑,服务器在cpu占用率不高的情况下,如果负载过高,那就先排查程序和sql吧,其中还有写慢日志的sql,在后续中会结合实际场景进行优化。

By jff

原文地址:https://www.cnblogs.com/widgetbox/p/13410503.html