Mysql 一个非常简单的更新InnoDB查询占用了太多时间

Mysql 一个非常简单的更新InnoDB查询占用了太多时间,mysql,sql,innodb,Mysql,Sql,Innodb,基本上我有一个简单的查询: UPDATE beststat SET rawView = rawView + 1 WHERE bestid = 139664 AND period = 201205 LIMIT 1 需要1秒 此表(beststat)当前有~1mil记录,其大小为:68MB。我有4GB RAM和innodb缓冲池大小=104857600,带有:Mysql:5.1.49-3 这是我数据库中唯一的InnoDB表(其他为MyISAM) 我在bestid和period上

基本上我有一个简单的查询:

UPDATE beststat 
  SET rawView = rawView + 1 
  WHERE bestid = 139664 AND period = 201205 
  LIMIT 1
需要1秒

此表(beststat)当前有~1mil记录,其大小为:68MB。我有4GB RAM
innodb缓冲池大小
=104857600,带有:Mysql:5.1.49-3

这是我数据库中唯一的InnoDB表(其他为MyISAM)


我在bestid和period上有
唯一键索引

CREATE TABLE `beststat` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `bestid` int(11) unsigned NOT NULL,
 `period` mediumint(8) unsigned NOT NULL,
 `view` mediumint(8) unsigned NOT NULL DEFAULT '0',
 `rawView` mediumint(8) unsigned NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`),
 UNIQUE KEY `bestid` (`bestid`,`period`)
) ENGINE=InnoDB AUTO_INCREMENT=2020577 DEFAULT CHARSET=utf8

给出:

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  beststat    const   bestid  bestid  7   const,const 1    

有什么帮助吗?

您的查询必须扫描整个表才能进行更新


在(bestid,period)上添加复合索引或将查询更改为使用id。

当您首次访问innodb表时,它将显示的时间包括将索引数据加载到缓冲池所需的时间。考虑进一步激发的时间序列。

如果您发布的详细信息仅为秒或更高版本的查询执行时间线

检查表是否已分段。 对于一个不完整的表,实际查找更新的次数超出了预期 对

如果情况并非如此,请查看以下变量

1) innodb_flush_log_at_trx_commit
   In general there will be 30 - 40% degraded performance with    
   innodb_flush_log_at_trx_commit set to 1 than it is set to 2

2) innodb_flush_method.
   Default fsync will perfrom worse than the O_DIRECT and O_SYNC

最后,当您使用SQL\u NO\u缓存执行查询continuouslu时,查看分析信息并查看服务器[sar或iostat]上的IO
LIMIT 1
子句似乎没有必要。您是否对查询运行了
解释
?是否正确使用索引?@llion:您无法解释更新/插入查询。我怀疑限制1是否会影响绩效,您是否可以解释从betstat选择rawView,其中bestid=139664,period=201205限制1
?有了这个大小的表,您可以考虑在代码> BestED < /C>中。您是否应该在服务器上发布IO(磁盘R/W可能是一个问题),同时您继续激发我在下面的答案中提到的相同的更新查询。Gorden Linoff…为什么查询应该扫描整个表,尽管它有唯一的键。。。我不认为唯一键不能满足索引需求,因为它就像主键一样,允许空值。戈登:我有一个综合指数,这是我的错。当我看代码的时候,我显然错过了唯一的关键一行。我将索引与约束分开声明,以避免歧义。
1) innodb_flush_log_at_trx_commit
   In general there will be 30 - 40% degraded performance with    
   innodb_flush_log_at_trx_commit set to 1 than it is set to 2

2) innodb_flush_method.
   Default fsync will perfrom worse than the O_DIRECT and O_SYNC