I have a performance issue in MySQL.
I have a relatively simple table that only uses numeric values.
CREATE TABLE `_user_activity` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` bigint NOT NULL,
`jday` mediumint NOT NULL,
`hour` tinyint NOT NULL,
`actions` int NOT NULL,
`seconds` int NOT NULL,
`timestamp` int NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id` (`user_id`,`jday`,`hour`)
) ENGINE=InnoDB AUTO_INCREMENT=880420 DEFAULT CHARSET=latin1 ROW_FORMAT=REDUNDANT
The table records user activity from time to time.
The table uses a unique index to identify whether a user already has activity on a given day at a given hour. If not, insert the number of actions performed and the time spent. If activity already exists, increment the values by the passed values.
INSERT INTO `_user_activity` (user_id, jday, hour, actions, seconds, timestamp)
VALUES (1234567890123, 2460438, 10, 5, 60, 1715085563) "
ON DUPLICATE KEY UPDATE actions = actions + 5, seconds = seconds + IF(1715085563 - timestamp > 300, 60, 1715085563 - timestamp), timestamp = 1715085563;
Everything works fine. But from time to time, this query takes between 0.3-1.0 seconds, which is extremely long. This seemingly random behavior has nothing to do with the number of online users, appearing even when there are almost no users. The server resources are not used, CPU and memory being used somewhere at 1-2%, and cPanel graphics (Resource usage) being close to 0. No errors, no faults, nothing in logs.
Also, other MySQL queries done at the same time in other tables (write or read) appear with reasonable execution times.
When I use this function (as a regular user), the normal query execution time is 0.005 sec.
innodb_buffer_pool_size = 6,442,450,944
max_connections = 250
innodb_thread_concurrency = 0
innodb_thread_sleep_delay = 10,000
innodb_io_capacity = 200
innodb_io_capacity_max = 2,000
innodb_flush_method = fsync
innodb_log_file_size = 50,331,648
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table = ON
MySQL 8.0.37
No. maximum concurrent connections 5
Physical Memory 8 GB
Does anyone have any idea where this high execution time for this table comes from? I recreated/optimized the table and still nothing. I don’t know if it’s just this table that suffers from high write times from time to time, but it’s the most frequently updated and the one I catch most often with such high execution times. It may be something general, but this one stands out. By the way, this table is rarely used for reading.