views:

70

answers:

2

I am designing a database in PostgreSQL on a dedicated server. I want certain complex queries to be run often, and the results to be accessible with ODBC/JDBC clients such as MS-Access or reporting software.

As I see it, I have the option to either define a VIEW within PostgreSQL and import it using ODBC, or to define the query within the ODBC client and import the underlying tables.

Which is best? Are there any other options that I am missing?

+1  A: 

I'm not sure what you mean by "import". A view is essentially a predefined query which you can select data from as you would from a table. When you execute a SELECT query, whether you're accessing the data tables directly or through a view, only the results of the query are sent back to you.

If you have to execute the same query from various different clients it might be a good idea to define a view for that query.

Share and enjoy.

Bob Jarvis
Thanks for clearing that up.. there is indeed no extra 'import' in the ODBC client case. Do you think there might be a difference in SQL optimization between the two cases?
littlegreen
No, in PostgreSQL the view is just replacing the query, so it will be optimized the same way as the individual statement. If the query is long enough that the parsing overhead is measurable, that may have an effect - but that will not be the case if the query is *complex*. In general, expect no measurable performance difference as long as the query is the same.
Magnus Hagander
+1  A: 

You say you have complex queries that create an output that you want to be used for reporting purposes.

Rollups, aggregations, etc are performed and stored in the data warehouse in pretty much the same way as you describe.

If this is what you want, you should consider having a table that stores the output of the query. Use a scheduling program to run your ComplexQuery at appropriate times and store the output in a table.

You have to decide why you want this. The complexity of the query is not a factor as much as the amount of resources that required database server to run the query. If this is small enough and it does not negatively affect performance, and you need your data real time, use a View. If you can live with periodically refreshed data, that is quickly available and does not put a high load on the server, go for an aggregate table.

Raj More
Thanks, I figured a concept like 'aggregate tables' would exist, but I didn't know it did.
littlegreen
It is more of a Data Warehousing concept than a PostgreSQL concept.
Raj More
Isnt it possible to make a Data Warehouse in PostgreSQL?
littlegreen
Yes it is absolutely possible to make a warehouse in PostgreSQL.
Raj More