Tom's Query:
SELECT * FROM media m
WHERE m.mediatype = (SELECT objectid FROM mediatype WHERE
medianame='Audio') 
AND (   EXISTS(SELECT 1
               FROM media 
               WHERE activity='347667' 
               AND objectid = m.objectid)
     OR EXISTS(SELECT 1
               FROM intsetmedia ism, set s 
               WHERE ism.set = s.objectid 
               AND s.activity='347667'
               AND ism.media = m.objectid))
ORDER BY medianame ASC, status DESC 


Tom's QUERY PLAN 
 Sort (cost=553641.43..553648.80 rows=2945 width=218) (actual time=126087.77..126087.80 rows=27 loops=1) 
  Sort Key: medianame, status 
  InitPlan 
  -> Seq Scan on mediatype (cost=0.00..1.29 rows=1 width=8) (actual time=0.12..0.14 rows=1 loops=1) 
  Filter: (medianame = 'Audio'::character varying) 
  -> Index Scan using media_mtype_index on media m (cost=0.00..553471.74 rows=2945 width=218) (actual time=4870.65..126087.06 rows=27 loops=1) 
  Index Cond: (mediatype = $0) 
  Filter: ((subplan) OR (subplan)) 
  SubPlan 
  -> Index Scan using media_pkey on media (cost=0.00..5.11 rows=1 width=0) (actual time=0.04..0.04 rows=0 loops=44876) 
  Index Cond: (objectid = $1) 
  Filter: (activity = 347667::bigint) 
  -> Hash Join (cost=24.25..133.80 rows=1 width=16) (actual time=2.71..2.71 rows=0 loops=44850) 
  Hash Cond: ("outer"."set" = "inner".objectid) 
  -> Index Scan using intsetmedia_media_index on intsetmedia ism (cost=0.00..109.26 rows=38 width=8) (actual time=0.04..0.04 rows=1 loops=44850) 
  Index Cond: (media = $1) 
  -> Hash (cost=24.24..24.24 rows=6 width=8) (actual time=0.14..0.14 rows=0 loops=44850) 
  -> Index Scan using set_act_index on "set" s (cost=0.00..24.24 rows=6 width=8) (actual time=0.11..0.13 rows=2 loops=44850) 
  Index Cond: (activity = 347667::bigint) 
 Total runtime: 126088.33 msec 
