[英]Slow update of one table when comparing multiple fields across two tables
The following query is timing out after 600 seconds. 以下查询在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;
The structure is as follows: 结构如下:
'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;
The PLACED
table has 65,000 records, the RESULTS
table has 9,000 records. PLACED
表具有65,000条记录, RESULTS
表具有9,000条记录。
I am assuming the solution involves a JOIN
statement of some descript, and I have tried taking several suggestions from this site, but am simply not finding the answer I am looking for. 我以为解决方案包含一些描述的JOIN
语句,并且我尝试从该站点获取一些建议,但是根本找不到我想要的答案。 Simply put, I would be grateful for suggestions on this. 简单地说,我会为对这个建议表示感谢。 I can put up example tables / create table code if requried. 如果需要,我可以放置示例表/创建表代码。
First of all, you'd better send us your full tables description, using 首先,您最好使用以下方式将完整的表格说明发送给我们:
show create table
Second, you'd better use join syntax : 其次,最好使用join语法:
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;
Hope this will help. 希望这会有所帮助。
The index cannot be used efficiently to perform the join because of your REPLACE
operation. 由于执行REPLACE
操作,因此无法有效地使用索引来执行REPLACE
。 I'd suggest creating an index with the columns in the following slightly different order: 我建议建立在以下顺序稍有不同的列的索引:
(date, competitor, time, position)
It may also help to add this index on both tables. 在两个表上添加此索引也可能会有所帮助。
It would be even better if you could modify the data in the database so that the data in the time
column was stored in the same format in both tables. 如果可以修改数据库中的数据,以使time
列中的数据以相同的格式存储在两个表中,那将更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.