一个文章分类的系统,有一块数据要求将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;
全部取出来再处理感觉更费时间和资源. 还请知道的不吝赐教.

解决方案 »

  1.   

    你的方法应该是最好的方法了。没什么再优化的了。
    虽然下贴中有很多方法实现相同功能,但显然并不如你自己的这个效率高。http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
    [征集]分组取最大N条记录方法征集,及散分....
      

  2.   

    最好添加索引 article( status, type,date)
      

  3.   

    怎么不靠谱?索引情况如何
    在status, type,date上建立复合索引没有
      

  4.   


    sql已经足够了,还有的话就是 要是可能就将表分区下吧。
      

  5.   


    还有组合索引 status、type、date 建立了没有?建立的顺序是 依次是 status、type、date。
      

  6.   

    如果加止索引,SQL语句又可以利用,就行了
      

  7.   

    CREATE INDEX tt1 ON tt (status, type,date);
      

  8.   

    -- 添加组合索引语句
    alter table article add index in_3field(status, type, date);
      

  9.   

    你直接用语句create index xxx1 on article (status, type, date)就可以创建索引了。 MYSQL会自动利用这些索引的,不需要你人为指定。
    你的方案已经很好了,没有更好的解决办法。
      

  10.   

    谢谢热心的ACMAIN_CHM,我先试试