【问题标题】:LEFT OUTER JOIN vs SUBSELECT in MySQLMySQL中的LEFT OUTER JOIN vs SUBSELECT
【发布时间】:2012-06-22 04:46:42
【问题描述】:

我有一张表说table1,它有 3 列 column1, column2 and column3。

column1 和 column2 是 FOREIGN KEY 和另外 2 个表。但是column3 中的数据来自 n 个表。

例如让我们考虑一下 Facebook。为了显示活动,它可能会维护一个可能有user1 photoliked photo1 或user1 statusliked status1 的表。所以在这种情况下,column3 不能是带有特定表的FOREIGN KEY。

现在有两种获取真实数据的方法 -

第一种方式-

SELECT user_id,
       verb_id,
       CASE WHEN verb_id = photoliked THEN
            (SELECT photo_name FROM photos WHERE photo_id = column3) -- getting the desired data from the third column
         WHEN verb_id = statusliked THEN
            (SELECT status FROM statustable WHERE status_id = column3) 
         ELSE '' END AS performedon
FROM table1
     JOIN table2 ON user_id = user_id  -- joining the first column
     JOIN table3 ON verb_id = verb_id  -- joining the second column

第二种方式-

SELECT user_id,
       verb_id,
       CASE WHEN verb_id = photoliked THEN
            p.photo_name
         WHEN verb_id = statusliked THEN
            s.status
         ELSE '' END AS performedon
FROM table1
     JOIN table2 ON user_id = user_id  -- joining the first column
     JOIN table3 ON verb_id = verb_id  -- joining the second column
     LEFT JOIN photos p ON p.photo_id = column3  -- joining the column3 with specific table 
     LEFT JOIN statustable s ON s.status_id = column3

问题

这两种方法中哪一种更好地检索数据? 两个查询中哪一个更便宜?

【问题讨论】:

  • 不,它不是重复的。在那个问题中,WHERE 子句中有一个IN,SUBSELECT 不依赖于主查询的任何列。
  • 第二个查询更好...
  • 可以做一个解释来分析查询。
  • 第二个查询更好、优化、独立且更快。虽然第一个查询是依赖的并且需要更多的执行时间。

标签: mysql sql join query-optimization


【解决方案1】:

第二个会更快,原因是第一个包含所谓的相关子查询。子查询与主查询中的记录相关。因此,对于主查询中的每个匹配记录,子查询都需要运行一次。在您的情况下,它无法运行子查询,直到它确定主查询中的 verb_id 值。需要运行很多查询。

第一个查询中的 EXPLAIN 应表明此问题。当您在 EXPLAIN 中看到它时,这通常是一个危险信号。

【讨论】:

    【解决方案2】:

    我认为JOIN 会更快,因为它对查询执行一次,而且我会尝试在JOIN 中过滤verb_id

    SELECT user_id,
       verb_id,
       COALESCE(p.photo_name, s.status) AS performedon
    FROM table1
        JOIN table2 ON user_id = user_id  -- joining the first column
        JOIN table3 ON verb_id = verb_id  -- joining the second column
        LEFT JOIN photos p ON verb_id = 'photoliked' AND p.photo_id = column3  -- joining the column3 with specific table 
        LEFT JOIN statustable s ON verb_id = 'statusliked' AND s.status_id = column3
    

    【讨论】:

      【解决方案3】:

      您可以使用这种方法:

      SELECT t.user_id,
             t.verb_id,
             p.photo_name  AS performedon
      FROM table1 AS t
           JOIN table2 AS t2  ON t2.user_id = t.user_id  
           JOIN table3 AS t3  ON t3.verb_id = t.verb_id 
           JOIN photos AS p  ON  p.photo_id = t.column3  
                             AND t.verb_id = 'photoliked'
      
      UNION ALL
      
      SELECT t.user_id,
             t.verb_id,
             s.status  
      FROM table1 AS t
           JOIN table2 AS t2  ON t2.user_id = t.user_id
           JOIN table3 AS t3  ON t3.verb_id = t.verb_id
           JOIN statustable AS s  ON  s.status_id = t.column3
                                  AND t.verb_id = 'statusliked' ;
      

      【讨论】:

        猜你喜欢
        • 2016-10-31
        • 1970-01-01
        • 1970-01-01
        • 2010-09-29
        • 2018-02-09
        • 2016-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多