I have a cluster of 3 percona db servers, and this table:
CREATE TABLE `metric` (
`metricid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`host` varchar(50) NOT NULL,
`userid` int(10) unsigned DEFAULT NULL,
`name` varchar(255) NOT NULL,
`sampleid` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`metricid`),
UNIQUE KEY `unique-metric` (`userid`,`host`,`name`,`sampleid`)
) ENGINE=InnoDB AUTO_INCREMENT=1000000000000 DEFAULT CHARSET=utf8
Each server (ids 1,2,3) runs this query every second:
insert into metric set metricid = $serverId$now, host = $now, name = $serverId
(e.g. insert into metric set metricid = 31396887217, host = 1396887217, name = 3
)
And pretty fast I'm running into "ERROR 1213 (40001) at line 1: Deadlock found when trying to get lock; try restarting transaction" - given that I provide ids that don't overlap since each server makes an id with different prefix, why do deadlocks happen at all?