Mysql表更新查询非常缓慢,而且每天都在增加。

huangapple go评论67阅读模式
英文:

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作业更新。并且每分钟都有大量数据进入该表。

在此查看MySQL分析

表详细信息

检查正在运行该表的进程

英文:

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.

Check the MySQL profiling here

Table Details

Check the process which are running in that table

答案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';

huangapple
  • 本文由 发表于 2020年1月6日 21:56:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定