大的MySQL表,选择非常慢



我在MySQL中有一个大表(在MAMP中运行),它有2800万行,大小为3.1GB。结构

    CREATE TABLE `termusage` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `termid` bigint(20) DEFAULT NULL,
  `date` datetime DEFAULT NULL,
  `dest` varchar(255) DEFAULT NULL,
  `cost_type` tinyint(4) DEFAULT NULL,
  `cost` decimal(10,3) DEFAULT NULL,
  `gprsup` bigint(20) DEFAULT NULL,
  `gprsdown` bigint(20) DEFAULT NULL,
  `duration` time DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `termid_idx` (`termid`),
  KEY `date_idx` (`date`),
  KEY `cost_type_idx` (`cost_type`),
  CONSTRAINT `termusage_cost_type_cost_type_cost_code` FOREIGN KEY (`cost_type`) REFERENCES `cost_type` (`cost_code`),
  CONSTRAINT `termusage_termid_terminal_id` FOREIGN KEY (`termid`) REFERENCES `terminal` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=28680315 DEFAULT CHARSET=latin1

下面是SHOW TABLE STATUS的输出:

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    
'termusage', 'InnoDB', '10', 'Compact', '29656469', '87', '2605711360', '0', '2156920832', '545259520', '28680315', '2011-08-16 15:16:08', NULL, NULL, 'latin1_swedish_ci', NULL, '', ''

我试着运行下面的select语句:

    select u.id from termusage u
    where u.date between '2010-11-01' and '2010-12-01'

需要35分钟才能返回结果(大约1400万行)-这是使用MySQL workbench。

我有以下MySQL配置设置:

Variable_name              Value
bulk_insert_buffer_size    8388608
innodb_buffer_pool_instances   1
innodb_buffer_pool_size    3221225472
innodb_change_buffering    all
innodb_log_buffer_size     8388608
join_buffer_size               131072
key_buffer_size            8388608
myisam_sort_buffer_size    8388608
net_buffer_length              16384
preload_buffer_size            32768
read_buffer_size               131072
read_rnd_buffer_size       262144
sort_buffer_size               2097152
sql_buffer_result              OFF

最后我尝试运行一个更大的查询-连接几个表和组一些数据,所有基于变量- customer id -

select c.id,u.termid,u.cost_type,count(*) as count,sum(u.cost) as cost,(sum(u.gprsup) + sum(u.gprsdown)) as gprsuse,sum(time_to_sec(u.duration)) as duration 
from customer c
inner join terminal t
on (c.id = t.customer)
inner join termusage u
on (t.id = u.termid)
where c.id = 1 and u.date between '2011-03-01' and '2011-04-01' group by c.id,u.termid,u.cost_type

这将返回最多8行(因为只有8个独立的cost_type),但是在termusage表中没有多少(少于100万)行需要计算的情况下,这个查询运行得很好,但是当termusage表中的行数很大时,这个查询将花费很长时间——我如何减少选择时间呢?

使用LOAD Data方法每月一次将数据从CSV文件添加到termusage表中,因此不需要对插入进行如此调整。

编辑:Show explain on main query:

id,select_type,table,type,possible_keys,key,key_len,ref,rows,Extra
1,SIMPLE,c,const,PRIMARY,PRIMARY,8,const,1,"Using index; Using temporary; Using filesort"
1,SIMPLE,u,ALL,"termid_idx,date_idx",NULL,NULL,NULL,29656469,"Using where"
1,SIMPLE,t,eq_ref,"PRIMARY,customer_idx",PRIMARY,8,wlnew.u.termid,1,"Using where"

看起来你在问两个问题,对吗?

第一个查询花费这么长时间的最有可能的原因是因为它是io绑定的。将1400万条记录从磁盘传输到MySQL工作台需要很长时间。

你试过把第二个查询通过"解释"吗?是的,您只能得到8行—但是SUM操作可能会对数百万条记录求和。

我假设"客户"one_answers"终端"表被适当地索引了?当你在termusage上连接主键时,这应该非常快。

您可以尝试删除按日期限制的where子句,而是在选择中添加IF语句,以便如果日期在这些边界内,则返回值,否则返回零值。SUM当然只对这个范围内的值求和,其他的都为零。

获取比你需要的更多的行听起来有点荒谬,但是我们最近在一个Oracle数据库上观察到这取得了相当大的改进。当然,这取决于许多其他因素,但这可能值得一试。

您也可以考虑将表格按年或月划分。所以你有termusage_2010, termusage_2011,…或者类似的东西。

不是一个很好的解决方案,但如果您的表相当大,它可能在较小的服务器上有用。

最新更新