PDA

View Full Version : Static vs. Dynamic


Nick Manley
06-11-2006, 01:47 PM
As with any shared hosting company, CPU cycles are valuable. I am working on my school's website and we have a bi-weekly newspaper that is published. The main purpose of the website is to post these stories like an online version of the paper. We use PHP and MySQL to post the stories and have the main page automatically updated.

My question however is should I continue doing it this way or should I make the pages static? Right now, everytime someone visits my website, it uses index.php and that file grabs all of the main stories and images. However, since the site is only updated bi-weekly, should I instead have a php file that "generates" a static html file.

For example, when we update our MySQL database and post the most recent stories I can go in and hit a "re-generate front page" button that will create a static html file with the new stories displayed. This way, the page won't have to query the database on every hit. I asked my friends about this and they basically gave me an answer along the lines of...

"We are not requesting that much data from the database and it would put just as much strain on the server with a static file because it would still have the access it from the hard drive."

What are your thoughts on this? Would a static file help use less resources or is the amount of data I am requestion so insignificant that it wouldn't matter? I think I have a total of 10 queries on this page however they are only grabbing one result each. I have to get the image URLs, story titles, teasers, poll questions, and all the other stuff that is on my front page. Its all very simple and small text data.

ben_brown30
06-12-2006, 12:39 AM
I would personally think that the static page is much more flexible than the dynamic page. In the future you may want to do more updates than you are currently doing.

One thing that worries me slightly is that you say you do about 10 queries, each pulling back one result. Each query has a basic amount of time that it needs to connect to the database, do internal operations etc, that is the same regardless of the amount of rows found. This means that for 10 different queries it will take roughly 10 times longer than a single query. My advice would be that you can reduce the load (and the time it takes to load the page) by reducing the number of queries. As it stands there should be no problems with a single page but this will at least give your viewers the quickest response from the page.

siguie
06-12-2006, 02:36 AM
If you are not having any issues with page displays or cpu use errors I say stay with the dynamic pages. It sounds like your paper is reasonably small and probably isn't much of a resource hog so there doesn't seem like anything to worry about.

You may also want to spice up your online paper by rotating articles or like what I do with Joomla is have unviewed articles rotate to the top and have a an area with the title/links of the most popular articles available. This keeps the paper looking fresh and if nothing else it's better to update once a week if possible so people don't need to wait as long and return more frequently.

Nick Manley
06-12-2006, 12:35 PM
One thing that worries me slightly is that you say you do about 10 queries, each pulling back one result. Each query has a basic amount of time that it needs to connect to the database, do internal operations etc, that is the same regardless of the amount of rows found. This means that for 10 different queries it will take roughly 10 times longer than a single query. My advice would be that you can reduce the load (and the time it takes to load the page) by reducing the number of queries. As it stands there should be no problems with a single page but this will at least give your viewers the quickest response from the page.

Is there a way to combine these queries? The front page is like our portal in a sense. It has a lot of different stuff where as the links have the specific pages. So the front page has to grab data from different tables. Is there anyway to combine multiple select statements... something like...


SELECT * FROM TableOne {{THEN}} SELECT * FROM TableTwo


I just made up that "then" keyword on the spot. But I think you get the idea. I am wanting to select data from different tables in a single query. Would this be faster and more efficient? How would I do it, what is the MySQL syntax?

areidmtm
06-12-2006, 01:39 PM
Look at the MySQL JOIN statement here (http://dev.mysql.com/doc/refman/4.1/en/join.html)

KenJackson
06-19-2006, 06:31 PM
For example, when we update our MySQL database and post the most recent stories I can go in and hit a "re-generate front page" button that will create a static html file with the new stories displayed. This way, the page won't have to query the database on every hit. You know, I never even thought of that. I am using PHP for some relatively simple tasks and I do infrequent updates. It would make a lot of sense for me to change my setup so the PHP generates a static HTML pages after every change instead of for every page hit.

Even if the server has plenty of power and all that, every operation saved makes the server faster, and no one EVER complains about too much web speed.

wirewolf
07-08-2006, 09:54 AM
10 queries? That's nothing unless you have a few thousand users hitting the page at the same time.
But if you want, do your update to your index.php as you normally do. Open the page, view the source code in your browser. Copy the code, paste it in to a new file called index.html. Upload to your domain directory where the index.php file is located (save a copy of the index.php just in case! Always Back up!!!).

Servers default to index files in this order, index.htm or index.html, then index.shtml and the last one they look for is index.php.
So if you have a index.php and a index.html in the same directory and your url is, lets say, "http://blahblah.com/" (notice there is no specific file in the url), it will go right to the html index first.

Here's an example:
Click - http://academyphotos.net/photogallery/ (a directory I'm still setting up, not in production mode yet)
Then try http://academyphotos.net/index.shtml (the server side include, the same page you see above)
Then try http://academyphotos.net/photogallery/index.php (the actual php script that gets the info for the gallery)

John

soboyle
07-14-2006, 06:06 AM
I would be interested in seeing how to generate static html from php pages. Can someone post an example of the code required to do this?
The other advantage not mentioned is that search engines will be able to index the static pages.

wirewolf
07-14-2006, 01:27 PM
Hi soboyle,

Apache servers have a neat little item called Mod Rewrite.

Example: this link, http://shipmodeling.net/vb_forum/showthread.php?t=215 is the actual php script that created the output for this thread, but by adding this instruction for the apache server:Options +FollowSymLinks
RewriteEngine on
RewriteRule ^thread([0-9]+).html$ showthread.php?t=$1 [L]it will show as http://shipmodeling.net/vb_forum/thread215.html

Actually, there is no such page as thread215.html. It's a phantom. It's not stored any where on your site.
php is script that outputs to a template or through the script itself to display in html code. In Mod Rewrite the server is given an instruction in a .htaccess file that you write and put in the root folder of a directory, that if I address the file as showthread.php?t=215, show it as thread215.html. Search engines love, what appears to them any way, static html pages.

See - modrewrite.com (http://www.modrewrite.com/) and Mod rewrite Tutorials (http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php) for more info.

John