1

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

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

需要1 秒

该表( beststat)目前有约 100 万条记录,其大小为:68MB。我有4GB RAMinnodb buffer pool size= 104,857,600,其中: Mysql: 5.1.49-3

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


unique key index当然有最佳状态和时期:

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

EXPLAIN SELECT rawView FROM beststat WHERE bestid =139664 AND period =201205 LIMIT 1

给出:

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

有什么帮助吗?

4

2 回答 2

0

当您第一次访问 innodb 表时,它将显示的时间包括将索引数据加载到缓冲池中所需的时间。考虑进一步解雇的时间表。

如果您发布的详细信息仅是几秒钟及以后的查询执行时间线,

检查表是否是碎片化的。在碎片表的情况下,对 UPDATE 的实际查找比预期的要多。

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

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_NO_CACHE 执行查询时,查看 PROFILING 信息并查看服务器上的 IO [sar OR iostat]

于 2012-05-12T15:29:37.097 回答
0

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

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

于 2012-05-12T15:09:39.163 回答