我打算编写以下查询:
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);
整个语句是原子的吗?即 MySQL(使用 MyISAM 引擎)是否在内部锁定了operations
表?
MySQL 是否有可能顺序执行两个子查询,这可能会导致在某些情况下(如果在该时间范围内添加新操作)total_points
andcount_operations
会不一致?