Deduplicate SELECT statements in relational division
7
votes
1
answer
572
views
I have a query that does a lot of duplicate work:
SELECT visitor_id, '1'::text AS filter
FROM events
WHERE id IN (SELECT event_id FROM params
WHERE key = 'utm_campaign' AND value = 'campaign_one')
AND id IN (SELECT event_id FROM params
WHERE key = 'utm_source' AND value = 'facebook')
GROUP BY visitor_id
UNION ALL
SELECT visitor_id, '2'::text AS filter
FROM events
WHERE id IN (SELECT event_id FROM params
WHERE key = 'utm_campaign' AND value = 'campaign_two')
AND id IN (SELECT event_id FROM params
WHERE key = 'utm_source' AND value = 'facebook')
GROUP BY visitor_id
As you can see, it performs different filtering on the params table 4 times. I'm using Redshift and although it scans this table very quickly, I have quite a few of these statements
UNION
ed together. Is there a way to rewrite the SQL using CASE
/IF
statements?
The example uses key = 'utm_source' AND value = 'facebook'
in both, but this is not necessarily true for all selects.
Asked by Sam
(647 rep)
Aug 17, 2015, 11:30 PM
Last activity: May 10, 2024, 12:44 AM
Last activity: May 10, 2024, 12:44 AM