Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在两个表之间比较多个字段时,一个表的更新速度较慢_Mysql - Fatal编程技术网

Mysql 在两个表之间比较多个字段时,一个表的更新速度较慢

Mysql 在两个表之间比较多个字段时,一个表的更新速度较慢,mysql,Mysql,以下查询在600秒后超时 update placed p ,Results r set p.position = r.position where p.competitor = r.competitor AND p.date = r.date AND REPLACE(p.time,":","") = r.time; 结构如下: 'CREATE TABLE `placed` ( `idplaced` varchar(50) DEFAULT NULL,

以下查询在600秒后超时

update placed p
      ,Results r
  set p.position = r.position
  where p.competitor = r.competitor
    AND p.date = r.date 
    AND REPLACE(p.time,":","") = r.time; 
结构如下:

'CREATE TABLE `placed` (
  `idplaced` varchar(50) DEFAULT NULL,
  `date` decimal(8,0) DEFAULT NULL,
  `time` varchar(45) DEFAULT NULL,
  `field1` varchar(45) DEFAULT NULL,
  `competitor` varchar(45) DEFAULT NULL,
  `field2` int(2) DEFAULT NULL,
  `field3` varchar(45) DEFAULT NULL,
  `field4` varchar(45) DEFAULT NULL,
  `field5` decimal(6,2) DEFAULT NULL,
  `field6` decimal(10,2) DEFAULT NULL,
  `field7` decimal(6,2) DEFAULT NULL,
  `field8` char(1) DEFAULT NULL,
  `field9` varchar(45) DEFAULT NULL,
  `position` char(4) DEFAULT NULL,
  `field10` decimal(6,2) DEFAULT NULL,
  `field11` char(1) DEFAULT NULL,
  `field12` char(1) DEFAULT NULL,
  `field13` decimal(6,2) DEFAULT NULL,
  `field14` decimal(6,2) DEFAULT NULL,
  `field15` decimal(6,2) DEFAULT NULL,
  `field16` decimal(6,2) DEFAULT NULL,
  `field17` decimal(6,2) DEFAULT NULL,
  `field18` char(1) DEFAULT NULL,
  `field19` char(20) DEFAULT NULL,
  `field20` char(1) DEFAULT NULL,
  `field21` char(5) DEFAULT NULL,
  `field22` char(5) DEFAULT NULL,
  `field23` int(11) DEFAULT NULL
   PRIMARY KEY (`idplaced`),
  UNIQUE KEY `date_time_competitor_field18_combo` (`date`,`time`,`competitor`,`field18`)
) ENGINE=InnoDB AUTO_INCREMENT=100688607 DEFAULT CHARSET=latin1;

CREATE TABLE `results` (
  `idresults` int(11) NOT NULL AUTO_INCREMENT,
  `date` char(8) DEFAULT NULL,
  `time` char(4) DEFAULT NULL,
  `field1` varchar(45) DEFAULT NULL,
  `competitor` varchar(45) DEFAULT NULL,
  `position` char(4) DEFAULT NULL,
  `field2` varchar(45) DEFAULT NULL,
  `field3` decimal(2,0) DEFAULT NULL,
  PRIMARY KEY (`idresults`)
) ENGINE=InnoDB AUTO_INCREMENT=6644 DEFAULT CHARSET=latin1;
放置的
表有65000条记录,
结果
表有9000条记录


我假设这个解决方案包含一个
JOIN
语句,其中包含一些描述,我尝试从这个网站上获取一些建议,但我没有找到我想要的答案。简单地说,如果你能就此提出建议,我将不胜感激。如果需要,我可以提供示例表/创建表代码。

首先,您最好使用

show create table
其次,最好使用连接语法:

update placed p
  join Results r on r.competitor = p.competitor
   set p.position = r.position
 where p.date = r.date 
   AND REPLACE(p.time,":","") = r.time;

希望这会有所帮助。

由于您的
REPLACE
操作,索引无法有效地用于执行联接。 我建议创建一个索引,其中列的顺序稍有不同:

(date, competitor, time, position)
在两个表上添加此索引也可能有所帮助


如果您可以修改数据库中的数据,使
time
列中的数据以相同的格式存储在两个表中,那就更好了。

您可以为每个表发布
SHOW CREATE TABLE…
的结果吗?@MarkByers-是的,对不起,应该从偏移量开始,不幸的是没有。我试过这个,但600秒后它也超时了。
REPLACE
确实是杀手。作为记录,
POSITION
从未出现在索引中,但我确实尝试了重新排序(在删除
REPLACE
之前),但它没有改变任何内容。由于table
RESULT
没有
索引中的所有字段,我也无法将相同的索引应用于两个表。非常感谢。