Page 1 of 1

DB to XML to Display

Posted: Fri Jun 13, 2008 9:13 am
by psurrena
Is it smart to pull records from a DB (that won't be updated after they are retrieved, like a new article) and store them in an xml file-then have PHP read from the XML file instead of the db? Is this more efficient, resource wise? Because every time someone click back and forth between articles, wouldn't the query run each time? Or does it pull from the cache?

Just wondering, thanks.

Re: DB to XML to Display

Posted: Fri Jun 13, 2008 9:18 am
by Eran
A couple of things:
- parsing XML is more demanding than executing a simple query.
- Databases (usually) have an in memory cache containing the latest queries. If you repeatedly send the same query then likely it will be retrieved from the cache
- There is a third option and it is to cache output into a static file. This is the fastest delivery option if the content doesn't change often

Re: DB to XML to Display

Posted: Fri Jun 13, 2008 9:21 am
by psurrena
Could you explain the third option a bit more?

Re: DB to XML to Display

Posted: Fri Jun 13, 2008 9:28 am
by Eran
You retrieve data from a database, format it using php and html and cache the results into a static file. Next time the static file gets outputted without querying the database or parsing in php.
When the contents get updated, you invalidate the cache and repeat the process.

Check out Zend_Cache for several caching options - http://framework.zend.com/manual/en/zend.cache.html

Re: DB to XML to Display

Posted: Fri Jun 13, 2008 9:31 am
by psurrena
great, thanks.

Re: DB to XML to Display

Posted: Fri Jun 13, 2008 9:31 am
by dbevfat
I wouldn't use XML, because that's at leat one file access and XML parsing, which is most likely slower than the query.

First, you need to stop thinking about optimization until you know you need it. If your website runs fine with that query, leave it as is. When the page starts getting slower, you need to profile your application to discover the slow parts. And only then you should start optimizing.

You could use DB query caching. This is something that actually databases do, and you don't have to explicitly program anything, it's transparent to the programmer. Query caches cache a whole result for a query in RAM, and if that query gets called again, it's retrieved from the cache rather than actually re-executing the query. This is good, but MySQL flushes the whole table cache on every change in the table. This means that the more writes in a certain table you have, more inefficient query cache gets.

One method (often used) is to render the whole page into a static HTML and let the webserver serve it without the overhead of PHP. Static files are served very fast, but this has issues with dynamic checking (if you're using logins and such) and dynamic content (some data changes on each request). In cases like these, you can render and cache partial HTMLs (header, menu, article, footer) and dynamically put them together with your dynamic data, which is still much faster than parsing XML.

Another way of storing cached files is in RAM, which is noticeably faster than file-based. Memcached is often used for this.

But, let me restate again: you don't need to optimize unless you know you have to.

best regards