I have three custom functions that do very similar things: they pull different data from the same, rather complicated set of joins — generating the joined table is what takes the time — and are usually all called in the same select. This is obviously inefficient and I would like to improve performance, but what is the best way to go about it?
- Create a materialised view of the complex join, covering all parameters, and just refer to this in each of the functions (or just omit the functions, altogether).
- Roll the three functions together and return all values at once in a custom type.
- Something else?
The first option seems, to a rookie like me, probably the best solution; but it obviously has the drawback of creating a pretty large materialised view, which would need maintenance (so it’s refreshed as required); although this MV might be useful elsewhere… The second option would be a bit of a hack; but is there anything else that I haven’t considered?
I would choose option 2 to be honest, unless you really want a materialised view. You can cast the function as a table so you can select from it as normal.