现在我用的mysql数据库里面有200多万条数据 
一个表的统计查询   就用了  6 min 30.61 sec
 select count(*) from audit;
+----------+
| count(*) |
+----------+
|  2319436 | 
+----------+
1 row in set (6 min 30.61 sec)这样的速度也太慢了吧 ,我感觉mysql不应该这样的 ,不知道会是什么原因引起的 ,请各位大虾指点下。

解决方案 »

  1.   

    表的存储引擎是什么?表中是否已经有主键或者其它索引?建议提供以下信息show table status like 'audit';
    show create table audit;
    show index from audit;
    check table audit;
      

  2.   

    show index from audit;
    +-------+------------+----------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+
    | Table | Non_unique | Key_name                   | Seq_in_index | Column_name       | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +-------+------------+----------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+
    | audit |          0 | PRIMARY                    |            1 | id                | A         |     2387362 |     NULL | NULL   |      | BTREE      |         | 
    | audit |          1 | appname_apptype_tfgn_index |            1 | appname           | A         |         120 |     NULL | NULL   | YES  | BTREE      |         | 
    | audit |          1 | appname_apptype_tfgn_index |            2 | apptype           | A         |         120 |     NULL | NULL   | YES  | BTREE      |         | 
    | audit |          1 | appname_apptype_tfgn_index |            3 | groupName         | A         |         120 |     NULL | NULL   |      | BTREE      |         | 
    +-------+------------+----------------------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+
      

  3.   

    show table status like 'audit';
    +-------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+----------------+----------+----------------+----------------------+
    | Name  | Engine | Version | Row_format | Rows    | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation      | Checksum | Create_options | Comment              |
    +-------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+----------------+----------+----------------+----------------------+
    | audit | InnoDB |      10 | Compact    | 2371272 |            125 |   297861120 |               0 |     77824000 |         0 |           NULL | 2009-06-23 15:32:51 | NULL        | NULL       | gbk_chinese_ci |     NULL |                | InnoDB free: 8192 kB | 
    +-------+--------+---------+------------+---------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+----------------+----------+----------------+----------------------+
    1 row in set (3.53 sec)
      

  4.   

    explain select count(*) from audit; 看一下结果。理论上应该直接利用primay 索引。另外建议你能 
    check table audit; 
    analyze table audit; 这个在一楼已经提过了,但你没有提供这方面的信息。
      

  5.   

    explain select count(*) from audit;
    +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra       |
    +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+
    |  1 | SIMPLE      | audit | index | NULL          | PRIMARY | 8       | NULL | 2925746 | Using index | 
    +----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+
    1 row in set (0.00 sec)
      

  6.   

    check table audit; 
    很慢 很慢
      

  7.   

    +-------------+-------+----------+----------+
    | Table       | Op    | Msg_type | Msg_text |
    +-------------+-------+----------+----------+
    | cm_db.audit | check | status   | OK       | 
    +-------------+-------+----------+----------+
    1 row in set (1 min 56.36 sec)
      

  8.   

    然后analyze 一下,之后MYSQL就应该会快了。
      

  9.   

    ID为主键,
    SELECT COUNT(ID) FROM audit 试试
      

  10.   

    MYSQL的MYISAM在某些情况下,索引文件会与实际的数据文件不匹配,会造成这种现象,但不不清楚你的具体是什么问题。
      

  11.   

    就是用MYSQL的工具检查表是否有问题
      

  12.   


    这个解释对于analyze table 来说并不正确。analyze 并不检查表是否有问题,只是重新分析一下键的分布情况。
    check table/ repaire table 是检查和修复的语句。
      

  13.   

    myisam的表优化一下就可以了。
    innodb的表,你就别指望能快了。在9000万行的表上,小型机运行count 也是需要几分钟甚至10几分钟的。