【发布时间】:2017-08-24 15:19:19
【问题描述】:
我读到在 MySql 查询中使用 IN 会导致性能下降,这与 Oracle 不同,应该使用 JOIN 代替。我能够转换简单的查询,但我正在努力处理包含嵌套 SELECT 的更复杂的查询。例如:
update Records set ActiveStatus=0, TransactionStatus=0, LastUpdated=&
where ActiveStatus!=5 and LastUpdated!=&
and Pk in (select RecordPk from GroupRecordLink
where GroupPk in (select Pk from Groups where ActiveStatus!=5 and
DateTimeStamp>&))
我按照this post 重写了它,但我不确定我的结果是否正确。
update Records r join (select distinct RecordPk from GroupRecordLink grl join
Groups g on grl.Pk = g.Pk where g.ActiveStatus!=5 and g.DateTimeStamp>&) s
using (Pk) set ActiveStatus=0, TransactionStatus=0, LastUpdated=&
where ActiveStatus!=5 and DateTimeStamp>&
谢谢
【问题讨论】:
-
好吧,将第一个查询的输出与您的尝试进行比较,如果结果匹配,那么您做对了。但我认为你的尝试过于复杂了。
标签: mysql sql select join sql-update