Quantcast
Channel: MySQL Forums - InnoDB
Viewing all articles
Browse latest Browse all 1954

SELECT result inconsistent with INSERT duplicate error (2 replies)

$
0
0
Hi!

I use an InnoDb table that sometimes receives a lot of INSERTs at a time. Possibly from different Apache/PHP instances on parallel connections. Each time an instance comes to an "item" that it has not processed before, it must insert a new entry into another database. Therefore one colum ('col') must manually increase using MAX(col) as reference. Another colum ('project') is fixed.

There is a UNIQUE index on col+project. And although the search for the MAX(col) and the INSERT are within a transaction, I receive a DUPLICATE KEY error:

(transaction start)
BEGIN

(check if another instance already created the entry)
SELECT SQL_NO_CACHE col FROM `ofb_variables` WHERE ((inputType='item') AND (inputID=449710) AND (inputIndex=0)) AND (project=1234)

(if not found: Find the increment value)
SELECT SQL_NO_CACHE col FROM `ofb_variables` WHERE `project`=1234 ORDER BY col DESC LIMIT 1

(insert with value + 1)
INSERT INTO `ofb_variables` SET `col`=59,`inputType`='item',`inputID`=221710,`inputIndex`=0,`project`=1234

(returns an error)
MySQL error 1062: Duplicate entry '1234-59' for key 1

(close transaction)
COMMIT


To my understanding, the transaction should prevent any other connection to insert a row into the table before this procedure does. Therefore the duplicate key error shall not be possible. But it seems, I somewhere got wrong. Can you give me any hints on this problem?

Thank you
Dominik


CREATE TABLE `ofb_variables` (
`project` INT(11) NOT NULL,
`col` INT(10) UNSIGNED NOT NULL,
`inputType` ENUM('question','item') NOT NULL COLLATE utf8_bin,
`inputID` BIGINT(20) UNSIGNED NOT NULL,
`inputIndex` TINYINT(4) NOT NULL DEFAULT '0',
`label` VARCHAR(64) NULL DEFAULT NULL COLLATE utf8_bin,
UNIQUE INDEX `pcol` (`project`, `col`),
UNIQUE INDEX `input` (`project`, `inputType`, `inputID`, `inputIndex`),
UNIQUE INDEX `plabel` (`project`, `label`),
CONSTRAINT `projectVariableC` FOREIGN KEY (`project`) REFERENCES `ofb_projects` (`id`) ON DELETE CASCADE
)
COLLATE=utf8_bin
ENGINE=InnoDB
ROW_FORMAT=DEFAULT

Viewing all articles
Browse latest Browse all 1954

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>