使用exists优化查询

时间:2012-05-16 21:26:05

标签: ruby-on-rails ruby-on-rails-3

如何使用EXISTS而不是IN函数优化以下mysql查询?

Subscriber.first(:conditions => ["email IN(SELECT email from submissions WHERE url = ? AND confirmed != 0)", url], :order => "RANDOM()")

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

基于存在的SQL语法

EXISTS (SELECT email from submissions x WHERE url = ? AND confirmed != 0 AND x.email = email)