Search Marketing

   
   

Increasing Web Server Speed with a Database

Tue Feb 02, 2010 11:10 am
<<     >>
Comments: 0 Views: 254

One of the biggest problems with database driven websites is writing code that is efficient. If you have hundreds, thousands, or hundreds of thousands of pageviews a day your server needs to be quick enough to deliver your content so that your visitors do not go elsewhere. Given that such sites are database driven your code most likely goes back to the database tens or hundreds of times just to draw a page. The goal here is to reduce that to as few as possible.

Ajax and other javascript related technologies have improved this problem but you may not need to use it, or you have decided that is it not helpful for your project. Unless you have a large news site, using Ajax might not be necessary, and if you need to deliver relatively static content you might not need Ajax at all.

Static Content:

What defines static content? About pages or company direction pages? These are generally considered static and most people would assume this is what would be in a static html file. Pages that are generated by a script, going to a database to retrieve information, checking for site permissions or allowing user-generated comments are not considered to be static and are usually not held in literal files on a server.

If you have a complex web program that has accounts and user logins, allows comments and file uploads then there are numerous requests to the database to authenticate users and set permissions. If you allow visitors to your program then they might be able to bypass the logins and file upload areas but you still have to deal with access permissions to specific files on your server, generated or not. So you still have to deal with multiple database read requests that increase the load on your server.

There are numerous ways to improve server response time. Get a faster server, set cache times, improve the efficiency of your code to reduce the number of times you need to access the database, or create static versions of your pages.

It is creating static versions of html pages that we are trying to work with here.

There are basically two ways to do this, one is to read a webpage using the file_get_contents() php function.

file_get_contents() literally reads the html of a page

$x = file_get_content("http://www.example.com");

Then insert this html into a file you create with php.

$filename = // you can name the file by reading the html title tag from above and add .html or .php
$createfile = fopen($filename, 'w');
$info = $x; // this is the webpage you opened above
fwrite($createfile, $info);
fclose($createfile);

If you had many hundreds or thousand of entires in your database each page could be created as a literal file in a directory on your server.

This has its advantages and disadvantages.

You might create thousands of files you don't want. But these could be much faster because your database does not get read at all, your web server just feeds up the requested file.

Another big disadvantage would be the url, but this might not be a problem for some people. It depends on the program.

Lets assume you have user accounts in your url:
http://www.example.com/johndoe/test
and you now want to turn that url into a literal file:
http://www.example.com/johndoe/test.html

This does not have to be a problem but if you do not actually have a directory off the root web server called johndoe, which you probably do not then you either need to create the directory at the time of the first conversion to literal files or use apache rewrite rules, or php to 301 redirect the visitor to the proper file location on the server.

Either way this is a much quicker way to server up files from a server. And even if you have user-generated comments or page edits all you must do is set it up so that anytime a comment is added or a edit is made that you run the script to edit or overwrite the literal file.

Using the database to create web pages

You can do that same thing if you want to bypass creating literal files and just want the database to feed out all the html in one shot.

You still read the file (url) into php, then instead of creating a file, you enter the information into the database.

The one big disadvantage with this method is that assuming you have hundreds or thousands of pages your database might get a little large. You save a lot of server time by only going to the database once to deliver the page, but you sacrifice a small, efficient database, since you would need to use BLOB table characteristics.

In any case you must remember to set your permissions properly so that any file just can't be sent up to the client.



Comments
Name:
*
URL: http: (ex. cnn.com)
Comment:
*
Number:
Math (13 + 4)
* required
SubCategories
Regular Expressions

© 2019 Christonium LLC

Christonium.com
|
Terms of Use
|
Privacy
ccc