MySQL 优化 INSERT 速度因索引而变慢

2021-12-21 00:00:00 indexing sql insert mysql

MySQL 文档说:

假设 B 树索引,表的大小减慢了 log N 索引的插入速度.

这是否意味着对于每个新行的插入,插入速度将减慢 log N 的因子,其中 N,我假设是行数?即使我只在一个查询中插入所有行?即:

INSERT INTO mytable VALUES (1,1,1), (2,2,2), (3,3,3), .... ,(n,n,n)

其中 n 是 ~70,000

我目前在具有以下结构的表中有大约 147 万行:

CREATE TABLE mytable (`id` INT,`值` MEDIUMINT(5),`日期`日期,PRIMARY_KEY(`id`,`date`)) 引擎 = InnoDB

当我在事务中以上述方式插入时,提交时间约为 275 秒.我该如何优化这一点,因为每天都会添加新数据,而且插入时间只会不断减慢.

此外,除了查询可能有帮助之外,还有什么可以帮助的吗?也许是一些配置设置?

可能的方法 1 - 删除索引

我读到在插入之前删除索引可能有助于插入速度.插入后,我再次添加索引.但这里唯一的索引是主键,我认为删除它不会有太大帮助.此外,当主键被删除时,所有的选择查询都会非常缓慢.

我不知道任何其他可能的方法.

以下是在表中插入约 60,000 行、约 147 万行的一些测试:

使用上述简单查询: 146 秒

使用 MySQL 的 LOAD DATA infile : 145 秒

使用 MySQL 的 LOAD DATA infile 并按照 David Jashi 在他的回答中的建议拆分 csv 文件: 60 个文件 136 秒,每个文件 1000 行,6 个文件 136 秒,每个文件 10,000 行

删除和重新添加主键:删除键需要 11 秒,插入数据需要 0.8 秒,但重新添加主键需要 153 秒,总共需要约 165 秒

解决方案

如果您想要快速插入,首先需要的是合适的硬件.这假设有足够的 RAM、SSD 而非机械驱动器和相当强大的 CPU.

既然你使用 InnoDB,你想要的是优化它,因为默认配置是为慢速和旧机器设计的.

这是有关配置 InnoDB 的精彩阅读

在那之后,你需要知道一件事——那就是数据库如何在内部做事,硬盘如何工作等等.我将在以下描述中简化机制:

事务是MySQL等待硬盘确认写入数据.这就是为什么机械驱动器上的事务处理很慢的原因,它们每秒可以执行 200-400 次输入-输出操作.翻译过来,这意味着您可以在机械驱动器上使用 InnoDB 每秒获得​​ 200 次左右的插入查询.当然,这是简单的解释,只是为了概述正在发生的事情,这不是交易背后的完整机制.

由于查询(尤其是与表大小对应的查询)的字节数相对较小 - 您实际上在单个查询上浪费了宝贵的 IOPS.

如果您在单个事务中包含多个查询(100 或 200 或更多,没有确切的数量,您必须测试),然后提交它 - 您将立即实现每秒更多的写入.

Percona 的人在相对便宜的硬件上实现了每秒 15k 次插入.即使每秒插入 5k 次也不错.像你这样的表很小,我在一个类似的表上做了测试(多 3 列),我设法在没有明显问题的情况下获得了 10 亿条记录,使用 16GB 内存机器和 240GB SSD(1 个驱动器,没有 RAID,用于测试目的).

TL;DR:- 按照上面的链接,配置您的服务器,获取 SSD,在 1 个事务中包装多个插入并获利.并且不要关闭索引然后再打开,它并不总是适用,因为在某些时候您将花费处理和 IO 时间来构建它们.

MySQL Docs say :

The size of the table slows down the insertion of indexes by log N, assuming B-tree indexes.

Does this mean that for insertion of each new row, the insertion speed will be slowed down by a factor of log N where N, I assume is number of rows? even if I insert all rows in just one query? i.e. :

INSERT INTO mytable VALUES (1,1,1), (2,2,2),  (3,3,3), .... ,(n,n,n)

Where n is ~70,000

I currently have ~1.47 million rows in a table with the following structure :

CREATE TABLE mytable (
   `id` INT,
   `value` MEDIUMINT(5),
   `date` DATE,
   PRIMARY_KEY(`id`,`date`)
) ENGINE = InnoDB

When I insert in the above mentioned fashion in a transaction, the commit time taken is ~275 seconds. How can I optimize this, since new data is to be added everyday and the insert time will just keep on slowing down.

Also, is there anything apart from just queries that might help? maybe some configuration settings?

Possible Method 1 - Removing Indices

I read that removing indices just before insert might help insert speed. And after inserts, I add the index again. But here the only index is primary key, and dropping it won't help much in my opinion. Also, while the primary key is dropped , all the select queries will be crippling slow.

I do not know of any other possible methods.

Edit : Here are a few tests on inserting ~60,000 rows in the table with ~1.47 mil rows:

Using the plain query described above : 146 seconds

Using MySQL's LOAD DATA infile : 145 seconds

Using MySQL's LOAD DATA infile and splitting the csv files as suggested by David Jashi in his answer: 136 seconds for 60 files with 1000 rows each, 136 seconds for 6 files with 10,000 rows each

Removing and re-adding primary key : key removal took 11 seconds, 0.8 seconds for inserting data BUT 153 seconds for re-adding primary key, totally taking ~165 seconds

解决方案

If you want fast inserts, first thing you need is proper hardware. That assumes sufficient amount of RAM, an SSD instead of mechanical drives and rather powerful CPU.

Since you use InnoDB, what you want is to optimize it since default config is designed for slow and old machines.

Here's a great read about configuring InnoDB

After that, you need to know one thing - and that's how databases do their stuff internally, how hard drives work and so on. I'll simplify the mechanism in the following description:

A transaction is MySQL waiting for the hard drive to confirm that it wrote the data. That's why transactions are slow on mechanical drives, they can do 200-400 input-output operations per second. Translated, that means you can get 200ish insert queries per second using InnoDB on a mechanical drive. Naturally, this is simplified explanation, just to outline what's happening, it's not the full mechanism behind transaction.

Since a query, especially the one corresponding to size of your table, is relatively small in terms of bytes - you're effectively wasting precious IOPS on a single query.

If you wrap multiple queries (100 or 200 or more, there's no exact number, you have to test) in a single transaction and then commit it - you'll instantly achieve more writes per second.

Percona guys are achieving 15k inserts a second on a relatively cheap hardware. Even 5k inserts a second isn't bad. The table such as yours is small, I've done tests on a similar table (3 columns more) and I managed to get to 1 billion records without noticeable issues, using 16gb ram machine with a 240GB SSD (1 drive, no RAID, used for testing purposes).

TL;DR: - follow the link above, configure your server, get an SSD, wrap multiple inserts in 1 transactions and profit. And don't turn indexing off and then on, it's not applicable always, because at some point you will spend processing and IO time to build them.

相关文章