我有TABLE1
如下
+----------+----------+----------+----------+----------+
+ date + time + course + runner + position +
+----------+----------+----------+----------+----------+
+ 20120701 + 1200 + london + aaa + 1st +
+ 20120701 + 1200 + london + bbb + 2nd +
+ 20120701 + 1200 + london + ccc + 3rd +
+ 20120701 + 1300 + london + eee + 1st +
+ 20120701 + 1300 + london + fff + 2nd +
+ 20120701 + 1400 + new york + ggg + 1st +
+ 20120701 + 1400 + new york + hhh + 2nd +
+ 20120702 + 2000 + london + iii + 1st +
+ 20120702 + 2000 + london + aaa + 2nd +
+ 20120702 + 2100 + new york + iii + 1st +
+ 20120702 + 2100 + new york + bbb + 2nd +
+----------+----------+----------+----------+----------+
和第二张桌子
+------+----------+------+--------+--------+-----+------+-------+
+idtbl2+ date + time + course + runner + pos + link + total +
+------+----------+------+--------+--------+-----+------+-------+
+ 1 + 20120701 + 1200 + london + aaa + 1st + WWW + +
+ 1 + 20120701 + 1200 + london + aaa + 1st + XXX + +
+ 1 + 20120701 + 1200 + london + aaa + 1st + YYY + +
+ 1 + 20120701 + 1200 + london + aaa + 1st + XXX + +
+------+----------+------+--------+--------+-----+------+-------+
基本上我需要RUNNER
从一个事件中计算竞争对手的数量( )TABLE1
并将其更新到TOTAL
第二个表的字段中,TABLE2
.
但是,TABLE1
大小为 8500 条记录,大小TABLE2
为 65,000 条记录。运行以下查询时,它会超时并丢失 MySQL 连接。
update table2 b, table1 a set b.total = (select count(a.runner) from table1 where a.date = b.date AND a.time = b.time AND a.course = b.course AND a.position != 'DQ' );
我对 JOIN 的有限理解使我相信 JOIN 不会提高效率,所以我很困惑。有什么想法吗?
@MarkByers -
JOIN 创建一个临时表,两个表的串联,是吗?我的想法是,无论如何,我仍然必须将一张表中的四个字段(日期、时间、课程、跑步者)与另一张表进行比较。
我试过这个,但它也超时: