简体   繁体   English

添加辅助键时,MySQL innoDB表中的插入缓慢

[英]Slow insert in MySQL innoDB table when adding secondary key

I have a buffing problem with an InnoDB table that I cannot figure out: 我的InnoDB表有一个抛光问题,我无法弄清楚:

  • I have a table with 5 columns, of which 4 columns make up the primary key and there are 3 more secondary keys; 我有一个包含5列的表,其中4列构成主键,另外还有3个辅助键。 in that table bulk inserts take very long time - up to 400 sec for 100,000 records (that is just 250 records/sec) 在该表中,批量插入会花费很长时间-100,000条记录最多需要400秒(即250条记录/秒)
  • If all secondary indexes are dropped, the inserts are very fast - 20 sec for 100,000 records (5000 records/sec or 20 times faster) 如果所有二级索引都被删除,则插入速度非常快-100,000条记录需要20秒(5000条记录/秒或快20倍)
  • If I add back even one secondary key, on a single bigint user-id column which is also a part of the primary key (I often need to search for the data of specific users), the inserts immediately become 3-4 times slower. 如果我在单个bigint用户ID列上又添加了一个辅助键,该列也是主键的一部分(我经常需要搜索特定用户的数据),则插入会立即变慢3-4倍。

What is going on here? 这里发生了什么? I suspect the answer is in the way InnoDB stores indexes, but after a lot of reading, I can't figure it out. 我怀疑答案是InnoDB存储索引的方式,但是经过大量阅读,我无法弄清楚。 What's weirdest of all is that the same table, with all the indexes added, with MyISAM engine always works at 5000 records/sec. 最奇怪的是,使用MyISAM引擎的同一个表(添加了所有索引)始终以5000条记录/秒的速度工作。

I don't know why. 我不知道为什么 But the MySQL website provides a performance tuning tip for inserting into InnoDB with secondary keys (which implies to me that this is just the way it is and you have to deal with it): "If you have UNIQUE constraints on secondary keys, you can speed up table imports by temporarily turning off the uniqueness checks during the import session..." 但是MySQL网站提供了一个性能调优技巧,用于使用辅助键将其插入InnoDB(这对我意味着这就是事实,因此您必须加以处理): “如果您对辅助键具有唯一约束,则可以通过在导入会话期间暂时关闭唯一性检查来加快表导入速度。”

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

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