I have a table like this:
[mytable] id, min, max, funobj ----------------------------------------- 1 15 23 {some big object} 1 23 41 {another big object} 1 19 27 {next big object}
Now suppose I have a view created like this:
CREATE VIEW functionvalues AS SELECT id, evaluate(funobj) FROM mytable
where evaluation is the function of returning a set evaluating large funobj. The result of a view might be something like the following:
id, evaluate -------------- 1 15 1 16 1 ... 1 23 2 23 2 24 2 ... 2 41 ...
I have no information about the specific values that will be evaluated, but I know that they will always be between the minimum and maximum values specified in mytable (including the bounds)
Finally, I (or better, a third-party application) makes a request in the view:
SELECT * FROM functionvalues WHERE evaluate BETWEEN somevalue AND anothervalue
In this case, Postgres evaluates the function score for each row in the mytable table, whereas depending on the where clause, the function should not be evaluated if it does not exceed min and min between these values. Since evaluation is a rather slow function, it gives me very poor performance.
It would be best to directly query the table using
SELECT * FROM ( SELECT id, evaluate(funobj) FROM mytable WHERE max BETWEEN somevalue AND anothervalue OR min BETWEEN somevalue AND anothervalue OR (min < somevalue AND max > anothervalue) ) AS innerquery WHERE evaluate BETWEEN somevalue AND anothervalue
Is it possible to use postgres to use such a query as stated above (using smart indexes or something like that) without changing the way a third-party application requests a view?
PS: Feel free to suggest a better heading for this question, the one I gave is more likely ... good ... non-specific.