一条SQL,查询很慢,show profile 命令,显示Page_faults_major很大,该如何优化呢?+--------------------+----------+----------+------------+-------------------+-------------------+
| Status             | Duration | CPU_user | CPU_system | Page_faults_major | Page_faults_minor |
+--------------------+----------+----------+------------+-------------------+-------------------+
| (initialization)   | 0.000088 | 0        | 0          |                 0 |                 0 | 
| Opening tables     | 0.000026 | 0        | 0          |                 0 |                 0 | 
| System lock        | 0.000009 | 0        | 0          |                 0 |                 0 | 
| Table lock         | 0.000012 | 0        | 0          |                 0 |                 0 | 
| init               | 0.000021 | 0        | 0          |                 0 |                 0 | 
| optimizing         | 0.000009 | 0        | 0          |                 0 |                 0 | 
| statistics         | 0.00002  | 0        | 0          |                 0 |                 0 | 
| preparing          | 0.000014 | 0        | 0          |                 0 |                 0 | 
| executing          | 0.000006 | 0        | 0          |                 0 |                 0 | 
| Sending data       | 1.31424  | 1.28     | 0.01       |                 0 |              1640 | 
| end                | 0.000011 | 0        | 0          |                 0 |                 0 | 
| query end          | 0.000004 | 0        | 0          |                 0 |                 0 | 
| freeing items      | 0.000012 | 0        | 0          |                 0 |                 0 | 
| closing tables     | 0.000009 | 0        | 0          |                 0 |                 0 | 
| logging slow query | 0.000003 | 0        | 0          |                 0 |                 0 | 
+--------------------+----------+----------+------------+-------------------+-------------------+

解决方案 »

  1.   

    数据跨页,IO 大,explain显示下查询计划,还有表结构
      

  2.   


    mysql> explain select count(1) from tbl_acd_cdr c left outer join tbl_acd_phase p use index(idx_p_ptype_state_beginTime) on p.ucid=c.ucid  where p.state=3 and p.ptype=2;
    +----+-------------+-------+--------+-----------------------------+-----------------------------+---------+-------------+--------+-------------+
    | id | select_type | table | type   | possible_keys               | key                         | key_len | ref         | rows   | Extra       |
    +----+-------------+-------+--------+-----------------------------+-----------------------------+---------+-------------+--------+-------------+
    |  1 | SIMPLE      | p     | ref    | idx_p_ptype_state_beginTime | idx_p_ptype_state_beginTime | 8       | const,const | 100076 | Using where | 
    |  1 | SIMPLE      | c     | eq_ref | idx_c_ucid                  | idx_c_ucid                  | 77      | js.p.ucid   |      1 | Using index | 
    +----+-------------+-------+--------+-----------------------------+-----------------------------+---------+-------------+--------+-------------+能看出撒么?非常感谢