sqlite 重写联接-查询为子查询/更快的变体

neskvpey  于 2022-11-15  发布在  SQLite
关注(0)|答案(1)|浏览(148)

我有两张table,一张叫“Shots”,有9M行,另一张叫“holes”,有50k行。我的数据库管理系统是SQLite。
目标是用从holes返回的值替换shotxyz的零值。我有以下一整天都在运行的查询:

UPDATE shots
SET x = h.hole_x,
    y = h.hole_y,
    z = h.hole_z
FROM holes h
LEFT OUTER JOIN shots s
ON h.tournament = s.tournament
   AND h.course = s.course
   AND h.year = s.year
   AND h.round = s.round
   AND h.hole = s.hole
WHERE s.end = 'hole'
   AND s.x = '0.0'
   AND s.y ='0.0'
   AND s.z = '0.0'
   AND h.hole_x != '0.0'
   AND h.hole_y != '0.0'
   AND h.hole_z != '0.0'

我读到了hereSubquery可能比JOIN快260倍。我如何重写我的查询以使其变得更快?

mmvthczy

mmvthczy1#

这里的主要问题是,您正在对holes进行不必要的联接,您必须将其删除。
以下是SQLite中类似联接的UPDATE语句的正确语法:

UPDATE shots AS s
SET x = h.hole_x,
    y = h.hole_y,
    z = h.hole_z
FROM holes AS h
WHERE h.tournament = s.tournament AND h.course = s.course 
  AND h.year = s.year AND h.round = s.round AND h.hole = s.hole
  AND s.end = 'hole' AND s.x = '0.0' AND s.y = '0.0' AND s.z = '0.0' 
  AND h.hole_x <> '0.0' AND h.hole_y <> '0.0' AND h.hole_z <> '0.0';

另外,为什么要将其与'0.0'进行比较?如果列hole_?是数字,则应该将它们与0.0或仅与0进行比较。

相关问题