M
M
Myroslav Zozulia2015-08-04 09:17:36
MySQL
Myroslav Zozulia, 2015-08-04 09:17:36

InnoDb deadlock. How to fix?

Hello!
I have been fighting with the deadlock problem for several days in a row. And I can't find an alternative. The matter is that I catch it on request of type INSERT .. SELECT .
The whole request looks like this:

INSERT INTO `items` (quantity) VALUES(SELECT count(*) FROM `items`)

So it was necessary to be perverted because, quantity value - depends on the row counter from the same table. And if we divide this query into two (separately take the counted rows and write them separately - I run into the problem that this COUNT is incorrect, and there are times when the base gives the same value twice).
In the case of the request that I demonstrated above, everything works fine. But there is one BUT that I want to fix. Sometimes, an error occurs (40001 - 1213 - Deadlock found when trying to get lock; try restarting transaction)
Transactions do not help. Stuck and can't find a solution.
Thanks in advance for your replies.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Skahin, 2015-08-04
@pihel

Try to select value into mysql variable https://dev.mysql.com/doc/refman/5.0/en/user-varia... and then insert it.
Sequential execution should solve the problem.

P
Puma Thailand, 2015-08-04
@opium

Keep the quantity separate and increment it on inserts and deletes.
Your decision is that you need to chop off your hands.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question