MySQL:使用(select?)表的结果批量更新的最快方法

时间:2015-01-30 08:06:48

标签: mysql

我的桌子上有超过7,000,000本书,包括电子书(约500K)和真实书籍。 我为电子书添加了一个parent_ean列,并希望用真实书籍中的ean编号填充它们,其中作者和标题是相同的。

这是我到目前为止(备选方案A):

UPDATE books as ebooks, books as parents
SET ebooks.parent_ean = parents.ean
WHERE (
  parents.title = ebooks.title AND parents.author = ebooks.author
  AND parents.format NOT LIKE "%Digital%"
  AND ebooks.format LIKE "%Digital%"
  AND ebooks.parent_ean = ""
  );

我不确定这是最快的方法(查询仍在运行: - /)或者它是否按预期工作(在完成之前无法查看)。还有另一种更好的方法吗?

这是一个替代B(上面稍作修改.c =电子书,p =父母):

UPDATE books as c,
  (SELECT ean, title, author FROM books
   WHERE format NOT LIKE "%Digital%") as p
SET c.parent_ean = p.ean
WHERE (
  p.title = c.title AND
  p.author = c.author AND
  c.format LIKE "%Digital%" AND
  c.parent_ean = "");

替代B感觉比A快(但它还在运行)。也许还有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

UPDATE books as a INNER JOIN books as b
ON b.title = a.title AND b.author = a.author
SET a.parent_ean = b.ean
WHERE SUBSTRING(a.format,1,7) = "Digital"
AND SUBSTRING(b.format,1,7) <> "Digital"
AND a.parent_ean = ""