I have a table thus:
CREATE TABLE `DeviceGrants` (
`DeviceId` BINARY(16) NOT NULL
COMMENT "128-bit UID",
`Grant` ENUM("AcceleratedRead") NOT NULL
COMMENT "The kind of grant this is",
`ExpiryTime` DATETIME NOT NULL
COMMENT "The date/time at which this grant will expire",
PRIMARY KEY(`DeviceId`, `Grant`),
KEY (`ExpiryTime`),
FOREIGN KEY (`DeviceId`) REFERENCES `Devices` (`DeviceId`)
ON DELETE CASCADE
) ENGINE=InnoDB;
(Grant
may only take one value right now, but this list is very likely to grow in future versions so the column is in place for forward-compatibility.)
I've witnessed a deadlock between this (I think):
INSERT INTO `DeviceGrants` (`DeviceId`, `Grant`, `ExpiryTime`)
VALUES(
UNHEX('<x>'),
'AcceleratedRead',
NOW() + INTERVAL 60 SECOND
)
ON DUPLICATE KEY UPDATE
`ExpiryTime` = NOW() + INTERVAL 60 SECOND
and this:
DELETE FROM `DeviceGrants` WHERE `ExpiryTime` <= NOW()
Now, following the advice in this excellent answer, I wanted to avoid the deadlock by rewriting the second statement. However, since the table has no single-column, auto-increment primary key (which is semantically needless), I'm not sure how to go about it.
What is my best bet here?