我有一个看起来像这样的表:
| calls | CREATE TABLE `calls` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`request_id` int(10) unsigned NOT NULL,
`ct` int(10) unsigned DEFAULT NULL,
`wt` int(10) unsigned DEFAULT NULL,
`cpu` int(10) unsigned DEFAULT NULL,
`mu` int(10) unsigned DEFAULT NULL,
`pmu` int(10) unsigned DEFAULT NULL,
`caller_id` int(10) unsigned DEFAULT NULL,
`callee_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `caller_id` (`caller_id`,`request_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3164057 DEFAULT CHARSET=utf8 |
和一个简单的查询:
mysql> EXPLAIN SELECT
-> AVG(`c1`.`wt`) `wt`,
-> AVG(`c1`.`cpu`) `cpu`,
-> AVG(`c1`.`mu`) `mu`,
-> AVG(`c1`.`pmu`) `pmu`
-> FROM
-> `calls` `c1`;
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
| 1 | SIMPLE | c1 | ALL | NULL | NULL | NULL | NULL | 3161147 | |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
1 row in set (0.00 sec)
mysql> SELECT
-> AVG(`c1`.`wt`) `wt`,
-> AVG(`c1`.`cpu`) `cpu`,
-> AVG(`c1`.`mu`) `mu`,
-> AVG(`c1`.`pmu`) `pmu`
-> FROM
-> `calls` `c1`;
+-----------+----------+------------+------------+
| wt | cpu | mu | pmu |
+-----------+----------+------------+------------+
| 2285.2079 | 428.2061 | 30567.4517 | 24925.7182 |
+-----------+----------+------------+------------+
1 row in set (1.61 sec)
服务器非常快(24 GB RAM)。最相关的my.cnf
(完整的 my.cnf)是:
query_cache_type=0
query_cache_size=0
key_buffer_size=50M
sort_buffer_size=10M
innodb_buffer_pool_size=1G
read_rnd_buffer_size=1M
join_buffer_size=4M
max_connections=400
table_cache=2000
table_definition_cache=2000
我可以做些什么来优化查询吗?只有 5,278,808 条记录,我似乎不太可能达到硬件限制。
我还尝试将整个表格移动到另一个相同的ENGINE=MEMORY
表格。时间缩短了大约 30%。但是,这仍然很慢。