InnoDB inserts very slow and slowing down

前端 未结 2 1003
逝去的感伤
逝去的感伤 2020-12-31 13:03

I have recently switched my project tables to InnoDB (thinking the relations would be a nice thing to have). I\'m using a PHP script to index about 500 products at a time. <

相关标签:
2条回答
  • 2020-12-31 13:43

    InnoDB provides more complex keys structure than MyIsam (FOREIGN KEYS) and regenerating keys is really slow in InnoDB. You should enclose all update/insert statements into one transactions (those are actually quite fast in InnoDB, once I had about 300 000 insert queries on InnoDb table with 2 indexes and it took around 30 minutes, once I enclosed every 10 000 inserts into BEGIN TRANSACTION and COMMIT it took less than 2 minutes).

    I recommend to use:

    BEGIN TRANSACTION;
    SELECT ... FROM products;
    UPDATE ...;
    INSERT INTO ...;
    INSERT INTO ...;
    INSERT INTO ...;
    COMMIT;
    

    This will cause InnoDB to refresh indexes just once not few hundred times.

    Let me know if it worked

    0 讨论(0)
  • 2020-12-31 13:56

    I had a similar problem and it seems InnoDB has by default innodb_flush_log_at_trx_commit enabled which flushes every insert/update query on your hdd log file. The writing speed of your hard disk is a bottleneck for this process.

    So try to modify your mysql config file

      `innodb_flush_log_at_trx_commit  = 0`
    

    Restart mysql service.

    I experienced about x100 speedup on inserts.

    0 讨论(0)
提交回复
热议问题