mysql查询太慢了

时间:2014-11-17 09:07:10

标签: mysql performance

我的简单MYSQL查询太慢,我的表有550k行数据,基于Innodb,当我运行查询时:

mysql> select count(*) from program;
+----------+
| count(*) |
+----------+
|   542500 |
+----------+
1 row in set (19.16 sec)

我的表定义如下:

CREATE TABLE `program` (
  `id` int(32) unsigned NOT NULL AUTO_INCREMENT,
  `apik` varchar(16) DEFAULT NULL,
  `programId` varchar(64) DEFAULT NULL,
  `status` int(10) DEFAULT '0',
  `created` int(16) DEFAULT NULL,
  `updated` int(16) DEFAULT NULL,
  `style` varchar(8) DEFAULT NULL,
  `channel` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=552331 DEFAULT CHARSET=utf8

我找到了这些信息:

mysql> show global status like 'key%';
+------------------------+---------+
| Variable_name          | Value   |
+------------------------+---------+
| Key_blocks_not_flushed | 0       |
| Key_blocks_unused      | 1714736 |
| Key_blocks_used        | 0       |
| Key_read_requests      | 0       |
| Key_reads              | 0       |
| Key_write_requests     | 0       |
| Key_writes             | 0       |
+------------------------+---------+

1 个答案:

答案 0 :(得分:0)

这里的问题可能是InnoDB

MyISAM存储总行数,InnoDB不会这样做,因此必须进行全扫描,这需要很长时间。看看手册kann是否可以帮助您:

来自manual

  

InnoDB没有保留表中的内部行数,因为   并发事务可能会“看到”不同的行数   同时。要处理SELECT COUNT(*)FROM t语句,InnoDB会扫描   表的索引,如果索引不是,则需要一些时间   完全在缓冲池中。如果你的桌子不经常改变,   使用MySQL查询缓存是一个很好的解决方案。要获得快速计数,   你必须使用你自己创建的柜台,让你的   应用程序根据插入和删除它更新它。如果   近似行数就足够了,可以使用SHOW TABLE STATUS。   参见14.2.12.1节,“InnoDB Performance Tuning Tips”