Sometimes for better performance you might want to do multiple sums as it will be less data stored in memory for the final sum. I have had pretty good success with this in the past. Such as:
SELECT A, SUM(B) FROM
(
SELECT A, sum(B)
FROM X
GROUP BY A
UNION ALL
SELECT A,sum(B)
FROM Y
GROUP BY A) C
GROUP BY A ;