英文:
Mysql Table Update Query is very slow and its increasing day by day
问题
我们有一张表,仅用于更新查询,它需要几乎200-300秒,总数据量大约为3,357,196行。我们已经附加了键,并且选择查询非常快。只有更新查询需要很长时间。
Mysql
数据库引擎:InnoDB
CREATE TABLE `t_strava_push` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`strava_user` int(20) DEFAULT NULL,
`strava_workout` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`type` varchar(45) COLLATE utf8_bin DEFAULT NULL,
`aspect` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`event_time` int(20) DEFAULT NULL,
`body` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`processed` int(1) NOT NULL DEFAULT '0',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `strava_user` (`strava_user`),
KEY `strava_workout` (`strava_workout`)
) ENGINE=InnoDB AUTO_INCREMENT=3377747 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
我们试图更新的查询:
UPDATE t_strava_push SET processed = 3 WHERE strava_workout = 2677850786
该表正在不断地由一个每2分钟运行一次的cron作业更新。并且每分钟都有大量数据进入该表。
英文:
We have a table which just for the update query it takes almost 200-300 secs, the total data size is almost 3,357,196 rows. We have the keys attached to it and the select query is very fast. Only the update query is taking a lot of time.
Mysql
Db Engine: InnoDB
CREATE TABLE `t_strava_push` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`strava_user` int(20) DEFAULT NULL,
`strava_workout` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`type` varchar(45) COLLATE utf8_bin DEFAULT NULL,
`aspect` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`event_time` int(20) DEFAULT NULL,
`body` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`processed` int(1) NOT NULL DEFAULT '0',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `strava_user` (`strava_user`),
KEY `strava_workout` (`strava_workout`)
) ENGINE=InnoDB AUTO_INCREMENT=3377747 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
The query we are trying to update :
UPDATE t_strava_push SET processed = 3 WHERE strava_workout = 2677850786
The table is being constantly updated by a cron job which runs every 2 minutes. And a lot of data is coming to the table every minute.
答案1
得分: 5
strava_workout
是一个字符串。但您将其作为数字进行比较,所以索引未被使用。
保持类型一致!
UPDATE t_strava_push
SET processed = 3
WHERE strava_workout = '2677850786';
英文:
strava_workout
is a string. But you are comparing it as a number, so the index is not being used.
Keep the types consistent!
UPDATE t_strava_push
SET processed = 3
WHERE strava_workout = '2677850786';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论