Alternative to Intersect in MySQL

前端 未结 8 1708
暖寄归人
暖寄归人 2020-11-22 11:53

I need to implement the following query in MySQL.

(select * from emovis_reporting where (id=3 and cut_name= \'全プロセス\' and cut_name=\'恐慌\') ) 
intersect
( sel         


        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 12:16

    There is a more effective way of generating an intersect, by using UNION ALL and GROUP BY. Performances are twice better according to my tests on large datasets.

    Example:

    SELECT t1.value from (
      (SELECT DISTINCT value FROM table_a)
      UNION ALL 
      (SELECT DISTINCT value FROM table_b)
    ) AS t1 GROUP BY value HAVING count(*) >= 2;
    

    It is more effective, because with the INNER JOIN solution, MySQL will look up for the results of the first query, then for each row, look up for the result in the second query. With the UNION ALL-GROUP BY solution, it will query results of the first query, results of the second query, then group the results all together at once.

提交回复
热议问题