Many left outer joins with limit performance

From: Gerhard Wiesinger <lists(at)wiesinger(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Many left outer joins with limit performance
Date: 2009-05-01 13:55:49
Message-ID: alpine.LFD.2.00.0905011552460.20718@bbs.intern
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hello,

I want to use postgresql for data entries (every minute) from a central heating
system where the timestamp is logged in a table log. For flexibility in the
future for future values and for implementing several high level types I've
modelled the values in a separate key/value table called log_details.

A Query for the last valid entry for today looks like (also defined as a view),
sometimes used without the limit:
SELECT
l.id AS id,
l.datetime AS datetime,
l.tdate AS tdate,
l.ttime AS ttime,
d1.value AS Raumsolltemperatur,
d2.value AS Raumtemperatur,
-- a lot more here, stripped for readibility, see link
FROM
log l
-- Order is relevant here
LEFT OUTER JOIN key_description k1 ON k1.description = 'Raumsolltemperatur'
LEFT OUTER JOIN log_details d1 ON l.id = d1.fk_id AND d1.fk_keyid =
k1.keyid
-- Order is relevant here
LEFT OUTER JOIN key_description k2 ON k2.description = 'Raumtemperatur'
LEFT OUTER JOIN log_details d2 ON l.id = d2.fk_id AND d2.fk_keyid =
k2.keyid
-- a lot more here, stripped for readibility, see link
WHERE
-- 86400 entries in that timeframe
datetime >= '1970-01-01 00:00:00+02'
AND datetime < '1970-01-02 00:00:00+02'
ORDER BY
datetime DESC
LIMIT 1;

For me a perfect query plan would look like:
1.) Fetch the one and only id from table log (or fetch even all necessary id
entries when no limit is specifie)
2.) Make the left outer joins

Details (machine details, table definition, query plans, etc.)
can be found to due size limitations at:
http://www.wiesinger.com/tmp/pg_perf.txt

Any ideas how to improve the performance on left outer joins only and how to
improve the planner to get better results?

For this special case a better solution exists but I thing the planner has to
do the work.
-- ...
WHERE
-- Also slow: id IN
-- OK: id =
id = (
SELECT
id
FROM
log
WHERE
datetime >= '1970-01-01 00:00:00+02'
AND datetime < '1970-01-02 00:00:00+02'
ORDER BY
datetime DESC
LIMIT 1
)
ORDER BY
datetime DESC LIMIT 1;

Any ideas?

Thnx.

Ciao,
Gerhard

--
http://www.wiesinger.com/

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message henk de wit 2009-05-01 14:32:19 Transparent table partitioning in future version of PG?
Previous Message Grzegorz Jaƛkiewicz 2009-05-01 11:12:19 Re: bad plan and LIMIT