我需要根据另一个表中的列更新一个表中的几个列。首先,我只是更新其中之一。我已经尝试了 2 种方法来做到这一点,这两种方法都有效,但是它们使用 mySQL 命令需要大约 4 分钟,而在 php 中运行时需要超过 20 分钟。两个表的长度都约为 20,000 行。
我的问题是,有没有更好或更有效的方法来做到这一点?
方法一:
UPDATE table_a,table_b
SET table_a.price = table_b.price
WHERE table_a.product_code=table_b.product_code
Run Code Online (Sandbox Code Playgroud)
方法二:
UPDATE table_a INNER JOIN table_b
ON table_a.product_code = table_b.product_code
SET table_a.price=table_b.price
Run Code Online (Sandbox Code Playgroud)
我想这些基本上以相同的方式工作,但我认为加入会更有效率。product_code 列是随机文本,尽管是唯一的,并且每一行都与另一个表中的一行相匹配。
还有什么我可以尝试的吗?
谢谢
更新:这是通过创建索引解决的,例如
CREATE UNIQUE INDEX index_code on table_a (product_code)
CREATE UNIQUE INDEX index_code on table_b (product_code)
Run Code Online (Sandbox Code Playgroud)