Combining SELECT DISTINCT with UNION DISTINCT in MySQL - any effect?

≯℡__Kan透↙ 提交于 2019-12-11 05:23:48

问题


The following two SQL statements are functionally identical:

SELECT DISTINCT a,b,c FROM table1
UNION DISTINCT
SELECT DISTINCT a,b,c FROM table2

and

SELECT a,b,c FROM table1
UNION DISTINCT
SELECT a,b,c FROM table2

...because "DISTINCT" is applied to the union as a whole, and so is redundant within the individual SELECT's.

(NOTE: UNION DISTINCT is identical to just UNION by itself, but I included the DISTINCT keyword for clarity)

My question here is, is there a performance difference, or execution-plan difference between the two in MySQL? Or are the SELECT DISTINCTs turned into regular SELECT's by the optimizer?


回答1:


You need to check the execution plans. However, I would expect that the execution plans are different -- or at least they should be in some circumstances.

The first query:

SELECT DISTINCT a, b, c FROM table1
UNION DISTINCT
SELECT DISTINCT a, b, c FROM table2

can readily take advantage of indexes on table1(a, b, c) and table2(a, b, c) before doing the final UNION. This should speed the final union by reducing the size of the data. The second query doesn't have this advantage.

In fact, the most efficient way to write this query would probably be to have the two indexes and use:

SELECT DISTINCT a, b, c FROM table1 t1
UNION ALL
SELECT DISTINCT a, b, c
FROM table2 t2
WHERE NOT EXISTS (SELECT 1 FROM table1 t1 WHERE t2.a = t1.a and t2.b = t1.b and t2.c = t1.c)

This is almost identical, although it might handle NULL values in the second table a bit differently.



来源:https://stackoverflow.com/questions/30142553/combining-select-distinct-with-union-distinct-in-mysql-any-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!