模拟环境:
   往表中插入100万条相同的数据。该表有6个索引。
   查询的时候,会有where,也有order by。目前暂且排序一个字段。
结果发现,查询很慢。查询效率以分钟级别计算。
  在用explain查看select语句的时候    id  select_type  table   type    possible_keys                key        key_len  ref       rows  Extra                      
------  -----------  ------  ------  ---------------------------  ---------  -------  ------  ------  ---------------------------
     1  SIMPLE       btitle  ref     BoardID_1,BoardID_2,deleted  BoardID_2  1002     const    73468  Using where; Using filesort
 
从语句中看,好像不能在优化了。
对这样的情况,该怎么处理?

解决方案 »

  1.   

    索引情况是什么,SQL语句是什么
      

  2.   


    Table     Non_unique  Key_name   Seq_in_index  Column_name  Collation  Cardinality  Sub_part  Packed  Null    Index_type  Comment
    --------  ----------  ---------  ------------  -----------  ---------  -----------  --------  ------  ------  ----------  -------
    bbstitle           0  PRIMARY               1  id           A                47539    (NULL)  (NULL)          BTREE              
    bbstitle           1  BoardID_2             1  BoardID_2    A               (NULL)       333  (NULL)  YES     BTREE              
    bbstitle           1  titleType             1  titleType    A               (NULL)    (NULL)  (NULL)  YES     BTREE              
    bbstitle           1  deleted               1  deleted      A               (NULL)    (NULL)  (NULL)  YES     BTREE              
    bbstitle           1  BoardID_1             1  BoardID_1    A               (NULL)       333  (NULL)  YES     BTREE              
    bbstitle           1  user_id               1  user_id      A               (NULL)       333  (NULL)  YES     BTREE              SQL语句 大概是这样.
    select * from bbstitle where BoardID_1=? and BoardID_2=? and deleted=0 group by top desc,date desc limit ?,2;
      

  3.   

    select * from bbstitle where BoardID_1=? and BoardID_2=? and deleted=0 group by top desc,date desc limit ?,2;这个是SQL语句?
    不是标准的SQL语句
      

  4.   

    创建索引如下create index xx on bbstitle(BoardID_1,BoardID_2,deleted,top,date)
    另外你的语句写错了吧。建议贴问题出来 时严谨一些,否则别人分析反而是浪费时间。select * from bbstitle 
    where BoardID_1=? 
    and BoardID_2=? 
    and deleted=0 
    group by top desc,date desc limit ?,2;GROUP BY 还是ORDER BY ?!
      

  5.   

    是的,搞错了.谢谢啊.不是分组.是排序才对.group by 修改成 order by 
    select * from bbstitle where BoardID_1=? and BoardID_2=? and deleted=0 group by top desc,date desc limit ?,2;另外,我昨天想到把需要排序的字段抽取出来,然后放到另外一张表A.
    查询的时候,先查询表A,对表A,进行排序.然后在查询具体表B.是否行得通?
    这样带来一个问题.连接数据库过多.