本帖最后由 colour_trace 于 2010-12-01 01:45:43 编辑

解决方案 »

  1.   

    create index xxx on 帖子表(板块id,创建时间)
      

  2.   

    给出你的以下结果。show create table  帖子表;
    show index from  帖子表;
    explain select  帖子id, 帖子标题,帖子公开性, 创建日期,创建人,回复数 from 帖子表  where 是否已结帖=0或1  and 板块id=数字  and 创建时间>'2010-**-**' and 创建时间<'2010-**-**'  order by 创建时间 desc  limit startrow,30;
      

  3.   

    创建时间,板块id
    建立复合索引试试
    explain 你的SQL语句
      

  4.   


    下面是表结构:
    CREATE TABLE `oritexts` (
      `OriTextID` int(11) NOT NULL auto_increment,
      `questionTypeID` int(11) default NULL,
      `title` varchar(500) NOT NULL,
      `author` varchar(200) NOT NULL,
      `magazine` varchar(100) NOT NULL,
      `pubyear` varchar(200) NOT NULL,
      `UserID` int(11) NOT NULL,
      `LibID` int(11) default NULL,
      `ExpertID` int(11) default NULL,
      `IsOpen` tinyint(1) NOT NULL,
      `Email` varchar(30) default NULL,
      `IsResolved` tinyint(1) NOT NULL,
      `BonusPoint` int(11) default NULL,
      `CreateDate` datetime NOT NULL,
      `Supplementary` longblob,
      `isEnglish` tinyint(1) NOT NULL,
      `UpdateDate` datetime NOT NULL,
      `quserip` varchar(32) default NULL,
      `Urlink` varchar(500) default NULL,
      `inVolume` varchar(200) default NULL,
      `timeIndex` decimal(25,0) NOT NULL,
      `reason` varchar(200) default NULL,
      `qikan` varchar(200) default NULL,
      `beizhu` varchar(200) default NULL,
      `page` varchar(200) default NULL,
      `IsUsp` tinyint(1) NOT NULL,
      `illStatus` tinyint(1) default NULL,
      `applyIllUserID` int(11) default NULL,
      `applyIllDate` datetime default NULL,
      `processIllUserID` int(11) default NULL,
      `processIllDate` datetime default NULL,
      `illNo` varchar(50) default NULL,
      `username` varchar(20) NOT NULL COMMENT 'user nick name from users table',
      `answercount` int(11) default '0',
      `ifupload` tinyint(1) NOT NULL default '0',
      PRIMARY KEY  (`OriTextID`),
      KEY `Expert_oritexts_FK1` (`ExpertID`),
      KEY `Librarylist_oritexts_FK1` (`LibID`),
      KEY `Users_oritexts_FK1` (`UserID`),
      KEY `question_type_FK1` (`questionTypeID`),
      KEY `oritexts_index` (`OriTextID`,`UpdateDate`),
      CONSTRAINT `Expert_oritexts_FK1` FOREIGN KEY (`ExpertID`) REFERENCES `expert` (`UserID`),
      CONSTRAINT `Librarylist_oritexts_FK1` FOREIGN KEY (`LibID`) REFERENCES `librarylist` (`LibID`),
      CONSTRAINT `question_type_FK1` FOREIGN KEY (`questionTypeID`) REFERENCES `questiontype` (`QuestionTypeID`),
      CONSTRAINT `Users_oritexts_FK1` FOREIGN KEY (`UserID`) REFERENCES `users` (`UserID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=273722 DEFAULT CHARSET=utf8;show index from oritexts结果如下:
    oritexts 0 PRIMARY 1 OriTextID A 289803 BTREE
    oritexts 1 Expert_oritexts_FK1 1 ExpertID A 6 YES BTREE
    oritexts 1 Librarylist_oritexts_FK1 1 LibID A 6 YES BTREE
    oritexts 1 Users_oritexts_FK1 1 UserID A 20700 BTREE
    oritexts 1 question_type_FK1 1 questionTypeID A 8 YES BTREE
    oritexts 1 oritexts_index 1 OriTextID A 289803 BTREE
    oritexts 1 oritexts_index 2 UpdateDate A 289803 BTREE explain select oritext.oriTextId as oritextid, oritext.title as title, oritext.isOpen as isopen, oritext.createDate as createdate,oritext.isEnglish as isEnglish, oritext.username as qusername,  oritext.answercount as textanswerscount, oritext.updateDate as textanswerdate, oritext.magazine as magazine, oritext.pubyear as pubyear, oritext.urlink as urlink  FROM Oritexts oritext  WHERE 1=1  AND oritext.isResolved = 0  AND oritext.updateDate >= '2010-11-20 00:00:00' AND oritext.updateDate <= '2010-11-21 23:59:59'  AND oritext.isOpen = 1  ORDER BY oritext.updateDate desc limit 20,30  结果如下:1 SIMPLE oritext ALL 289803 Using where; Using filesort辛苦各位了
      

  5.   

    create index xxx on oritexts(UpdateDate)另外你的这个 and 板块id=数字  根本就没有出现。所以提问的时候尽可能使用你的原始语句,否则一个总结转换,就把很多东西给转没了。问题说明越详细,回答也会越准确!参见如何提问。(提问的智慧
      

  6.   

    在UpdateDate、isResolved、isOpen上建立复合索引试试
      

  7.   

    非常感谢楼上各位,给UpdateDate做索引,效率就大大提高了