My concrete select. Bold parts are same, and I want it somehow to optimalize. Previous example with plus sign in select was missplaced. This real example should be explanatory enough.
(SELECT aaa from cache where bbb = (select bbb from data where id = data_fk)) as aaaColumn,
date + interval '2 hours'
input_number = 51 and
(SELECT aaa from cache where bbb = (select bbb from data where id = data_fk)) like 'BA%' and
data_fk = (select id from data where bbb = (select bbb from cache where aaa = 'BA-123')) and
date >= '2008-09-07 16:00:00'
order by data_fk, date;
PS: view isnt bad idea, but it should be parametric (if it could be done).
OT: does postgre somehow cache a results of views? (if the view selecs is crazy and it takes minute to be done...)
Is that the real statement?
How are the tables data, cache and input related?
Where does the column data_fk come from?
And please put your statements into [ code ] tags so that they easier to read.
does postgre somehow cache a results of views
Postgres does not have materialized views as Oracle does. But if the tables are properly indexed I would only start optimizing this, if you really see a performance problem. How big (=number of records) are those tables and how are they indexed? How does the execution plan look like?