mysql sql语句求优化
[code=SQL]SELECT tid,count(*) as count
FROM `pw_posts`
WHERE fid=77 GROUP BY tid
ORDER BY count DESC LIMIT 10
[/code]
其中tid ,fid分别有建了索引,表里有160万条数据,速度挺慢的,用explain显示如下
SIMPLE pw_posts ref fid fid 2 const 38112 Using where; Using temporary; Using filesort
------解决方案--------------------
------解决方案--------------------
语句本身没有问题,但是设计可以改善
你应该先缓存数据
insert into `pw_posts_stat`(tid, fid, count)
select tid, fid, count(*) as count
from `pw_posts`
group by tid, fid
然后每次查询的时候就会很快了
select tid, count from `pw_posts_stat` where fid=77 order by count desc limit 10
select tid, count from `pw_posts_stat` where fid=77 order by count desc limit 11, 10
select tid, count from `pw_posts_stat` where fid=66 order by count desc limit 10
SmartB2B 是一款基于PHP、MySQL、Smarty的B2B行业电子商务网站管理系统,系统提供了供求模型、企业模型、产品模型、人才招聘模型、资讯模型等模块,适用于想在行业里取得领先地位的企业快速假设B2B网站,可以运行于Linux与Windows等多重服务器环境,安装方便,使用灵活。 系统使用当前流行的PHP语言开发,以MySQL为数据库,采用B/S架构,MVC模式开发。融入了模型化、模板









