What is the difference between UNION and UNION ALL ?
What is the difference between UNION and UNION ALL ?
Both stack the rows of two or more SELECT results. UNION removes duplicate rows; UNION ALL keeps every row.
-- t1.id: 1, 2 t2.id: 2, 3
SELECT id FROM t1 UNION SELECT id FROM t2; -- 1, 2, 3
SELECT id FROM t1 UNION ALL SELECT id FROM t2; -- 1, 2, 2, 3
UNION has to sort or hash the combined rows to find duplicates. On large inputs that costs time and memory.UNION ALL just appends, so it is faster and can be streamed.Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.