I am planning on writing the following query:
INSERT INTO summary (user_id, total_points, count_operations)
SELECT
15 AS user_id,
(SELECT SUM(points) FROM operations WHERE user_id = 15) AS total_points,
(SELECT COUNT(*) FROM operations WHERE user_id = 15) AS count_operations
ON DUPLICATE KEY UPDATE
total_points = VALUES(total_points),
count_operations = VALUES(count_operations);
Is the whole statement atomic? i.e. does MySQL (with MyISAM engine) internally locks the operations table?
Is there a possibility that MySQL would execute the two subsqueries sequentially which could result in some cases (if a new operation is added within that timeframe) that the total_points and count_operations would be inconsistent?
MyISAM still uses table-level locks.
So other processes that want to update the tables you’re using have to wait until your transaction is finished. If it puts a read lock on the table “operations”, all subsequent write locks go into a queue and wait.