“The total number of locks exceeds the lock table size” Deleting 267 Records

后端 未结 3 526
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 18:27

I\'m trying to delete 267 records out of about 40 million. The query looks like:

delete from pricedata
where
pricedate > \'20120413\'

p

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-17 19:00

    It seems that you don't have an index on pricedate (or MySQL does not use this index for some reason).

    With REPEATABLE READ (the default transaction isolation level), InnoDB places shared locks on the records read and filtered out by the query and it seems you don't have enough space for 40M locks.

    To work around this problem use any of these solutions:

    1. Create the index on pricedate if it's not there (may take time)

    2. Break your query into smaller chunks:

      DELETE
      FROM    pricedata
      WHERE   pricedate > '20120413'
              AND id BETWEEN 1 AND 1000000
      
      DELETE
      FROM    pricedata
      WHERE   pricedate > '20120413'
              AND id BETWEEN 1000001 AND 2000000
      

      etc. (change the id ranges as needed). Note that each statement should be run in its own transaction (don't forget to commit after each statement if AUTOCOMMIT is off).

    3. Run the DELETE query with READ COMMITTED transaction isolation level. It will make InnoDB lift locks from the records as soon as they are read. This will not work if you are using binary log in statement mode and don't allow binlog-unsafe queries (this is the default setting).

提交回复
热议问题