select distinct(a.circulationno) circulationno, a.userid userid, c.empname username, a.subject subject, a.content content, a.savingperiod savingperiod, a.attachflag attachflag, a.status status, a.createtime createtime, '1' timepass, '0' candelete from tb_circulation a, tb_circulationlabel b, tb_empbaseinfo c where a.circulationno=b.circulationno and a.status = '1' and a.userid=c.id and (a.userid=509 or (b.labeledflag = '1' and readerid=509)) order by a.createtime desctb_circulation表有8000条记录
tb_circulationlabel表有220000条记录
tb_empbaseinfo表有500条记录
已在a,b中circulationno字段建立索引
   现在执行上面这条sql语句需要5秒钟,系统响应显得很慢,以前大概是不到1秒,最近变慢了。大伙帮看看有没有办法来优化使它快一点。sql语句是固定在程序中了,不能动,现在只能从数据库着手。   另外,听说此版有位狼头大哥是高手,能否抽空赐教?谢谢!

解决方案 »

  1.   

    and readerid=509 中 这个字段是哪个表的呢?
      

  2.   

    and a.userid=c.id and (a.userid=509 or (b.labeledflag = '1' and readerid=509))这个业务逻辑 还有可能优化掉,尽量去掉or或者用union all连接起来取代or试试看吧。
      

  3.   

    忘记了,把你的explain贴出来,大家看看。
      

  4.   

    a.userid b.labeledflag 都啥字段类型?有索引么?加上试试
      

  5.   


    readerid是表tb_circulationlabel中的mysql> explain select distinct(a.circulationno) circulationno, a.userid userid, c.empname username, a.subject subject, a.content content, a.savingperiod savingperiod, a.attachflag attachflag, a.status status, a.createtime createtime, '1' timepass, '0' candelete from tb_circulation a, tb_circulationlabel b, tb_empbaseinfo c where a.circulationno=b.circulationno and a.status = '1' and a.userid=c.id and (a.userid=509 or (b.labeledflag = '1' and readerid=509)) order by a.createtime desc; 
    +-------+------+-------------------------------------------------------------------------------------------------+--------------------------------------------------------------+---------+-----------------+------+------------------------------------+
    | table | type | possible_keys                                                                                   | key                                                          | key_len | ref             | rows | Extra                              |
    +-------+------+-------------------------------------------------------------------------------------------------+--------------------------------------------------------------+---------+-----------------+------+------------------------------------+
    | c     | ALL  | PRIMARY                                                                                         | NULL                                                         | NULL    | NULL            |  624 | Using temporary; Using filesort    |
    | a     | ref  | PRIMARY,tb_circulation_userid_status_createtime_index,circulationno                             | tb_circulation_userid_status_createtime_index                |       5 | c.id,const      |    8 | Using where                        |
    | b     | ref  | tb_circulationlabel_readerid_index,tb_circulationlabel_circulationno_readerid_labeledflag_index | tb_circulationlabel_circulationno_readerid_labeledflag_index |       4 | a.circulationno |   17 | Using where; Using index; Distinct |
    +-------+------+-------------------------------------------------------------------------------------------------+--------------------------------------------------------------+---------+-----------------+------+------------------------------------+
    3 rows in set
      

  6.   


    分别是int,char
    已建索引
      

  7.   

    Using temporary; Using filesort 临时表太小了,所以动用了文件排序。
    show variables like ''1,增加临时表大小
    2,增加排序缓存。--查看临时表大小,byte单位
    show variables like '%tmp_table_size%'
    --查看排序缓存大小
    show variables like '%sort_buffer_size%'楼主看看这2个参数是多少?
      

  8.   

    mysql> show variables like '%tmp_table_size%'
    ;
    +----------------+----------+
    | Variable_name  | Value    |
    +----------------+----------+
    | tmp_table_size | 33554432 |
    +----------------+----------+
    1 row in setmysql> show variables like '%sort_buffer_size%'
    ;
    +-------------------------+---------+
    | Variable_name           | Value   |
    +-------------------------+---------+
    | myisam_sort_buffer_size | 8388608 |
    | sort_buffer_size        | 2097144 |
    +-------------------------+---------+
    2 rows in setmysql> 
      

  9.   

    tmp_table_size 太小了,才32M而已,你数据库服务器总内存有多大啊?建议设置到256M试试看。
      

  10.   

    最后参照my-huge.cnf调整了设置,现在查询该语句的时间已缩到1秒以内了,访问数据库的时候mysqld进程占用的内存和CPU比率上升了很多
      

  11.   


    恭喜,恭喜,你会调整mysql参数了,表明你已经初步窥探了mysql。