简体   繁体   English

简单的InnoDB表上的简单MySQL INSERT查询需要40 + ms

[英]Simple MySQL INSERT query on simple InnoDB table takes 40+ms

I'm wondering what's exact cause that makes insert queries on mysql/innodb to last at least 40ms on machine with fairly strong cpu. 我想知道是什么原因导致mysql / innodb上的插入查询在具有相当强大的CPU的机器上持续至少40ms。 "Equivalent" query runs <10ms on same MyISAM table (tables are without any foreign keys). “等效”查询在相同的MyISAM表上运行<10ms(表没有任何外键)。 Timings are from MySQL console. 时间来自MySQL控制台。

This is "as simple as possible" db structure for reproduction. 这是“尽可能简单”的db结构用于再现。

CREATE TABLE `test_table_innodb` (
  `id` int NOT NULL AUTO_INCREMENT,
  `int_column` int NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `test_table_myisam` (
  `id` int NOT NULL AUTO_INCREMENT,
  `int_column` int NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

I'm running same query from mysql console (which auto-commits transactions in case of InnoDB). 我正在从mysql控制台运行相同的查询(在InnoDB的情况下自动提交事务)。 No other queries are executed on machine at the time and the results are: 当时没有其他查询在机器上执行,结果是:

 mysql> insert into test_table_myisam (int_column) values (5);
 Query OK, 1 row affected (0.00 sec)

 mysql> insert into test_table_innodb (int_column) values (5);
 Query OK, 1 row affected (0.06 sec)

Is transaction overhead making query to run that longer against InnoDB table? 交易开销是否使查询对InnoDB表运行时间更长? Or? 要么?

There are three aspects that to be considered with each auto-committed INSERT 每个自动提交的INSERT需要考虑三个方面

ASPECT #1. ASPECT#1。 Overhead 高架

InnoDB supports MVCC and Transaction Isolation as an ACID-compliant storage engine. InnoDB支持MVCC和事务隔离作为符合ACID标准的存储引擎。 In order to accommodate this, a copy of a row before changes are committed is written into the Undo Tablespace section of the System Tablespace file ibdata1 . 为了适应这种情况,将提交更改之前的行副本写入系统表空间文件ibdata1的“撤消表空间”部分。 What would be written if you are running an INSERT? 如果你正在运行INSERT会写什么? A copy of a blank row . 一个空白行的副本 That way, a rollback simply removes the attempt to INSERT. 这样,回滚只会删除INSERT尝试。 When an INSERT in committed, the copy of the blank in the Undo Tablespace is expunged. 当INSERT in committed时,撤消表空间中的空白副本将被清除。

ASPECT #2. ASPECT#2。 Clustered Index 聚集指数

For every InnoDB table, there exists an internal default row index called gen_clust_index . 对于每个InnoDB表,都存在一个名为gen_clust_index的内部默认行索引 This is created regardless of the presence or absence of a PRIMARY KEY. 无论是否存在PRIMARY KEY,都会创建此项。 Since your table has a PRIMARY KEY of id, the gen_clust_index is constructed to be associated with the row containing a unique id field. 由于您的表具有id的PRIMARY KEY,因此gen_clust_index被构造为与包含唯一id字段的行相关联。

ASPECT #3. ASPECT#3。 Configuration 组态

Believe it or not, there are times when MySQL 4.1 out-of-the-box is faster than MySQL 5.5. 信不信由你,MySQL 4.1开箱即用的速度比MySQL 5.5快。 Sounds shocking, doesn't it? 听起来令人震惊,不是吗? Percona actually benchmarked several versions of MySQL and found this to be the case. Percona实际上对几个版本的MySQL进行了基准测试,发现情况就是如此。

I wrote about this in DBA StackExchange before 我之前在DBA StackExchange中写过这篇文章

The CPU is not the factor here. CPU不是这里的因素。 The factor is the disk . 因素是磁盘。 In innodb the command need to be write to log , so if the log disk is the same disk or disk is not fragment or disk is slow than you will have a big difference. 在innodb中,命令需要写入日志,所以如果日志磁盘是相同的磁盘或磁盘不是片段或磁盘慢,那么你将有很大的不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM