=?UTF-8?B?QmrDuHJu?= T Johansen <btj@xxxxxxxxxx> writes: > But when I try to run the 3 queries separately, then they are very quick, 2 barely measurable and the third takes about 1,5 seconds. The union query > takes a little over 9 seconds, so I guess the union part is the bottleneck? No; it's clear from your EXPLAIN output that the first UNION arm is taking the bulk of the runtime. It's odd that it wouldn't be exactly the same when run standalone. What does EXPLAIN ANALYZE show if you run that first arm by itself? I concur with depesz's observation that the MAX() subselects seem to be pretty expensive. If you don't want to rewrite the query in a wholesale fashion like he suggests, you might be able to make the MAX's cheaper by providing an index on sed_uttak(avlsnr, dato); but I'm not sure how much that will help. regards, tom lane -- Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general