views:

476

answers:

1

Related to my previous question: PHP and Databases: Views, Functions and Stored Procedures performance Just to make a more specific question regarding large SELECT queries.

When would it be more convenient to use a View instead of writing the SELECT query in the code and calling it:

 $connector->query($sql)->fetchAll();

What are the factors to take into account when deciding wether its best to use a view, or just leave it as it is. Say, if you join several tables, select certain amount of data, etc.

I'm asking in the context of a big web app (with PHP & Postgres), and looking for performance and optimization.

+1  A: 

One thing to take into account when you are using PHP source code + views (instead of only PHP source code) is that you now have two kind of sources to modify when you update your application :

  • you must put the new PHP sources on the server
  • and you must update the views

And you sometimes must do that exactly at the same time if you don't want your application to crash... Or you have to program thinking that the application must run OK with an outdated / more recent version of the views (for a couple of seconds).

Something else you might have to consider is versionning : versionning PHP scripts is easy : just use SVN and its allright, as it's text files. With views, to get the same kind of versionning, you have to work in text files (commited on the SVN before you update them on the DB production server), and keep those in sync with the DB server -- seems easy, but it's not when you have to push an emergency patch to production ^^

Personnaly, I generally use views / stored procedures when it really makes a diffenrence : for instance, if a calculation would require thousands of SQL queries (and, so, thousands of call from PHP, waiting for the response, and so on) or too many data exchanges between the two servers, using a stored proc can really be great !

(Never used postgre, but the idea is the same with other products)

Pascal MARTIN