views:

112

answers:

2

This follows on from this question where I was getting a few answers assuming I was using files for storing my HTML templates.

Before recently I've always saved 'compiled' templates as html files in a directory (above the root). My templates usually have two types of variable - 'static' variables which are not replaced on every usage but are used across the site - basically for ease of maintenanceif I decide to change the site name for example; and dynamic vars that change on every page load.

I always used to save these as files on the server - but my friend pointed out something I'd overlooked: why have 5-10 filesystem calls when you can have one database call?

What I want to know is which is more efficient? Calling several HTML files from the system or calling several rows of templates from the database (in one query/call).

+3  A: 

Don't Store Editable HTML In the database

Seriously, because the maintenance overhead for mere changes becomes exhaustive once you realise you can no longer just pop open a text editor.

I worked on many projects which had HTML content in the database, and it was a constant nightmare of "find that row the content is on" and I really would liked to have shot the person whom made it.

Also, DON'T PREMATURELY OPTIMISE . If you find it a problem thats slowing the project down, then change it. Because making the code exhaustively less maintainable to save a millisecond. But design the code well enough that should you need to change where the content comes from later it should be easy to do.

Surely that can be resolved by having a suitable web interface for editing the templates?

Erm, really not, unless you're only trying to compete with notepad. Syntax highlighting and all the other full host of features you can get in a standard editor just make your developers suicidal when they find themselves editing web pages by hacking at an undersized text area with awful white on black ( not to mention the extra fun you get with having to worry about entity encoding etc, for instance, try editing html with in a text area where the html content contains a text area element! )

On FileIO

While File IO can be a bottleneck, keep in mind that if you have a proper linux install, and plenty of memory, a handy thing known as "disk-cache" takes effect, which in effect, keeps files used in memory, so file IO becomes mere memcpy.

On the contrary, in real stress tests on any of the code I have used, the biggest slowdowns have been in the database!, primarily the nice slow CONNECT string, query parse time, extra php<->mysql interactions. You're not really looking at gaining anything. Filesystem lookup is close to database index lookup, and you don't have any unknowns other than "you need to stream it from a disk", no table locking stuff to worry about!

You should probably try something like a caching library, X-Cache comes highly recommended, thats more likely to give you visible performance gains.

Kent Fredric
Surely that can be resolved by having a suitable web interface for editing the templates? I understand your point though - updating templates in the database from my .html copies is tedious enough.
Ross
Fair enough, I'll take your advice - thank you :)
Ross
It can be resolved with suitable web interfaces for editing the templates, the point is that a suitable web based HTML (or generic code) editor is a project on its own merits and not trivial
Vinko Vrsalovic
A: 

I pretty much agree with the gist of Kent Fredric's answer. But, if you really want to know, which is more efficient/faster, you cannot reasonably expect to get the answer here. If you want that answer, there is only one way to get it: profile the application both ways.

George Jempty