Web design question - XML vs SQL: Which is more efficient?

Started by Nine Toes, Mon 30/05/2011 03:33:43

Previous topic - Next topic

Nine Toes

Would it be more efficient to:
A) Have my PHP script draw information from a database every time a user views the web page, or
B) Set up a cron job to run a PHP script that generates an XML document with the appropriate database information every so often, so that the web page can draw it's information from it instead, or
C) There is no difference.

Both the database and the XML document will be stored on the server, along with the PHP script (where it will also be run, before the actual page is delivered to the user), so I'm almost thinking that there is no difference.  But, I'm also trying to keep in mind security (trying to eliminate SQL injection), and for whatever reason, it just seems like bad practice to have a web page query a database every time a user wants to view it, especially if that information only changes once a week.

Thoughts? Ideas?
Watch, I just killed this topic...

Darth Mandarb

First; since it's only data/info that changes weekly I'd say render a file and let that be the source (rather than multiple hits to the db).  That would be my advice.  Just make sure you have the system in place so that if/when you do update the information it immediately updates the file(s) so it's always current and up-to-date (bad data is bad ;)).

Secondly; it's fine to have the page hit the db server but 'efficiency-wise' it really depends on the type of information, the complexity of the querying being done, and/or the amount of traffic the page(s) will get.  I mean if it's a crazy-complex query that takes some time to run then obviously you're better off with the above advice ... but just small stuff each individual user isn't going to notice the 53ms time-difference one way or another :)

I am working on a site now that renders out pages (over 8500 of them total) and never touches the DB after the initial file creation (well in some places, but not the bulk of the rendered pages).  It works very well and does decrease the over all server-load.

Wyz

Option B will definitely be more efficient, even more because the XML can be cached either locally or by a proxy server and then it will only be requested when it changed. There exist systems that will update the XML when the database get changed. The first person to view the XML will get the script generating it, subsequent users will see the cached version.
If that is too complex and the data does not need to be very up-to-date (like daily updates) the cron-job would definitely work. If you run it every hour, and there are not that many people viewing it it can actually be less efficient, so there are exceptions. :)
Life is like an adventure without the pixel hunts.

Eigen

MySQL databases are extremely optimized and most certainly faster than PHP file I/O (for reading an xml file) + XML parsing. Also, most of the time, queries are cached (unless you use some funky dynamic query strings), so I would go with option A.

Wyz

Oh I assumed the xml would be used client-side, as with an AJAX request. Parsing XML serverside would indeed impose a lot of overhead, and SQL might be faster yes. (all depends on the amount of data, an the data types)
Life is like an adventure without the pixel hunts.

SMF spam blocked by CleanTalk