简体   繁体   中英

Slow insert in MySQL innoDB table when adding secondary key

I have a buffing problem with an InnoDB table that I cannot figure out:

  • I have a table with 5 columns, of which 4 columns make up the primary key and there are 3 more secondary keys; in that table bulk inserts take very long time - up to 400 sec for 100,000 records (that is just 250 records/sec)
  • If all secondary indexes are dropped, the inserts are very fast - 20 sec for 100,000 records (5000 records/sec or 20 times faster)
  • 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.

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. What's weirdest of all is that the same table, with all the indexes added, with MyISAM engine always works at 5000 records/sec.

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..."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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