一个文章分类的系统,有一块数据要求将N个分类的最新N条数据去出来.我是这样写的,感觉很不靠谱,请问怎么处理的好.SELECT * FROM `article` WHERE status = 1 and type = 1 order by date desc limit 10;
SELECT * FROM `article` WHERE status = 1 and type = 2 order by date desc limit 10;
SELECT * FROM `article` WHERE status = 1 and type = 3 order by date desc limit 10;
......
SELECT * FROM `article` WHERE status = 1 and type = N order by date desc limit 10;
全部取出来再处理感觉更费时间和资源. 还请知道的不吝赐教.
SELECT * FROM `article` WHERE status = 1 and type = 2 order by date desc limit 10;
SELECT * FROM `article` WHERE status = 1 and type = 3 order by date desc limit 10;
......
SELECT * FROM `article` WHERE status = 1 and type = N order by date desc limit 10;
全部取出来再处理感觉更费时间和资源. 还请知道的不吝赐教.
虽然下贴中有很多方法实现相同功能,但显然并不如你自己的这个效率高。http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
[征集]分组取最大N条记录方法征集,及散分....
在status, type,date上建立复合索引没有
sql已经足够了,还有的话就是 要是可能就将表分区下吧。
还有组合索引 status、type、date 建立了没有?建立的顺序是 依次是 status、type、date。
alter table article add index in_3field(status, type, date);
你的方案已经很好了,没有更好的解决办法。