【问题标题】:How to optimise this query using table join?如何使用表连接优化此查询?
【发布时间】:2014-04-07 20:04:49
【问题描述】:

谁能帮我优化以下查询...

UPDATE tab1 SET col1 = 'NA' where 
col2 =(Select col1 from tab2 where col2 is null and 
col3 = (Select col1 from tab3 where col2 = 100));

【问题讨论】:

  • 请分享示例数据,我知道这不是完全必需的,但可以加快我的回答速度。
  • 请注意,根据记录的数量,您可以通过确保引用列被索引来进一步优化查询。当然,这一切都取决于您的数据结构和数据量。
  • 您可能会发现语法差异取决于它必须在哪个数据库上运行。

标签: sql optimization join


【解决方案1】:
update t1
SET col1 = 'NA'
from tab1 t1
inner join tab2 t2 on t1.col2=t2.col1 and t2.col2 is null
inner join tab3 t3 on t2.col3=t3.col1 and t3.col2 = 100

【讨论】:

  • 我可能是错的,但是要获得与原始查询相同的结果,不应该将 tab3 连接到 tab2 而不是 tab1 吗?
猜你喜欢
  • 2017-12-14
  • 1970-01-01
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 2022-01-27
  • 1970-01-01
相关资源
最近更新 更多