2017-07-18 87 views
-1

任何人都可以请帮助我下面的查询,其中我使用IN子句导致性能问题。我想为它使用JOIN,但不知道如何做这样的查询。mysql如何使用JOIN而不是IN与WHERE子句

select * 
from user_followings 
where followed_id = 'xyz' AND owner_id IN (
    select DISTINCT owner_id 
    from feed_events 
    where DTYPE = 'PLAYLIST' AND last_updated_timestamp > '20-04-2017' AND (feed_type = 'PLAYED_PLAYLIST' OR feed_type = 'STARTED_LISTENING') 
    order by last_updated_timestamp DESC)"; 
+0

请问[手册](https://dev.mysql.com/doc/refman/5.7/en/join.html)中的例子没有帮助吗? –

+0

你有尝试过自己吗?你面临的挑战是什么? – money

+0

@money是的,我试过,但在这里我主要关心的不是获得查询我也想知道如何选择子句的具体要求,什么是获得更快的响应形式查询的最佳选择。 – user2423768

回答

1

我重写使用连接查询:

SELECT * 
    FROM user_followings 
    INNER JOIN feed_events ON user_followings.owner_id = feed_events.owner_id 
    WHERE followed_id = 'xyz' 
     AND DTYPE = 'PLAYLIST' 
     AND feed_events.last_updated_timestamp > '20-04-2017' 
     AND (
      feed_type = 'PLAYED_PLAYLIST' 
      OR feed_type = 'STARTED_LISTENING' 
      ) 
    ORDER BY last_updated_timestamp DESC 
+0

感谢Dave进行查询,但它给出了一个错误“Column'last_updated_timestamp'where where clause is ambiguous”。 – user2423768

+0

你好,我已经更新了我的答案。 – Dave94

1

一个join可能不是最好的办法。使用exists

select uf.* 
from user_followings uf 
where uf.followed_id = 'xyz' and 
     exists (select 1 
       from feed_events fe 
       where uf.owner_id = fe.owner_id and 
        fe.DTYPE = 'PLAYLIST' and 
        fe.last_updated_timestamp > '2017-04-20' and 
        fe.feed_type in ('PLAYED_PLAYLIST', 'STARTED_LISTENING') 
      ); 

你想在feed_events(owner_id, dtype, last_updated_timestamp, feed_type)user_followings(followed_id, owner_id)的索引。

其他说明:

  • ORDER BY在这样的子查询是没有用的。
  • 对于常量日期使用标准日期格式(YYYY-MM-DD)。
  • 使用IN而不是一堆OR s。在大多数情况下,阅读和优化会更容易。
+0

谢谢戈登,解释。我必须创建更复杂的查询这个知识帮助我:) – user2423768