I have a table called SummaryTable and below is the show create table:
CREATE TABLE `SummaryTable` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`uniqueId` bigint(10) unsigned NOT NULL,
`date` varchar(20) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
`requested` bigint(20) unsigned DEFAULT '0',
`accepted` bigint(20) unsigned DEFAULT '0',
`sent` bigint(20) unsigned DEFAULT '0',
`failed` bigint(20) unsigned DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `SummaryTable_uniqueId_date` (`uniqueId`,`date`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Only 1 query goes to this table which is below:
INSERT INTO SummaryTable(userId,date,requested,accepted,sent,failed) values(?,?,?,?,?,?) ON DUPLICATE KEY UPDATE requested = requested + ?,accepted = accepted + ?, sent = sent + ?, failed = failed + ?;
I have multiple threads upserting into this table. This table currently has approx 0.5 million records. Initially when this table was created, all the upserts were quick and clean.
But as and how the table grew upto 0.5 million, all the upserts are experiencing LOCK WAIT TIMEOUT error
I thought may be the issue is due to the multiple threads accessing the table, so i reduced it to single thread but the issue still persisted.
I am not knowing where the issue is. I manually inserted into this table using the same above query and that too took a large amount of time. Even simple insert without on duplicate key update was getting timed-out.
Below is the sample data currently residing in the table:
+-------+------------+---------------+-----------+----------+-------+--------+
| id | uniqueId | date | requested | accepted | sent | failed |
+-------+------------+---------------+-----------+----------+-------+--------+
| 10464 | 1241325597 | 2019-12-05 14 | 52698 | 51541 | 42754 | 1164 |
| 10466 | 1227703657 | 2019-12-05 14 | 4094 | 4074 | 3958 | 2130 |
| 10467 | 1241897654 | 2019-12-05 14 | 1404 | 1311 | 1207 | 768 |
| 10468 | 1235335116 | 2019-12-05 14 | 127 | 62 | 46 | 97 |
| 10470 | 1241873029 | 2019-12-05 14 | 1969 | 1903 | 1641 | 66 |
| 10471 | 1240867311 | 2019-12-05 14 | 5169 | 3666 | 3254 | 1502 |
| 10476 | 1240850853 | 2019-12-05 14 | 183 | 181 | 168 | 2 |
| 10477 | 1235492827 | 2019-12-05 14 | 1237 | 1236 | 1140 | 0 |
| 10482 | 1242128197 | 2019-12-05 14 | 157 | 130 | 110 | 27 |
| 10484 | 1241323122 | 2019-12-05 14 | 292 | 251 | 183 | 35 |
+-------+------------+---------------+-----------+----------+-------+--------+
Now i tried to inserted the below query which executed very quickly..
INSERT INTO SummaryTable set userId = 1242128197, date = unix_timestamp("2020-01-05 12:00:00")*1000, requested = 1, accepted = 0, sent = 0, failed = 0;
Could someone help what's causing the insert on duplicate key update to break here