Forum Moderators: open

Message Too Old, No Replies

Database vs File read

Attempting to lighten the load time on my home page

         

frakilk

7:10 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



I am attempting to optimize the load time on my home page a bit and reduce the number of database accesses to build the page. I had the idea that for some of the more demanding DB queries I could use a script called by a cron job to execute the query every minute or so and write the results to a file with surrounding HTML. I would then use a PHP file read to add the file contents to the page at load time.

Would this be a good optimization or will I be actually slowing things down with file reads?

Any feedback appreciated, thanks.

Demaestro

7:33 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What you are suggesting could indeed speed up your time.... however I wonder what maintaining those files will do to your sanity.

I would suggest that you try loading pieces of that page using an Ajax implementation. If you aren't familiar with it do some research. I think you will find that more and more sites are using Ajax for faster page loads.... even though it may take the same amount of time for your "widgets" to grab the data and display it to the users... What it will do is present the user with a page... which then loads the smaller widgets after the page has loaded... using little loading graphics in the unloaded widgets will let the user know that there is more data to come but they are now at least looking at your site while they wait for database hits to happen.

I would look into this before switching to flat files.

If you need any Ajax help I can point you to some helpful guides... there are a few good ones on this site as well. Depending on your backend scripting language this should be a breeze and there is a lot of pre-written things to get you going.

Post back so we know how you are making out.

frakilk

7:45 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



Thanks Demaestro but I think AJAX would be overkill for my situation plus I want the page to be indexed fully by the search engines. Maintaining the files would not be a big deal as I don't intend to use the practice extensively throughout the site.

On another note do you think the memory footprint/process creation aspect of loading a file would outweigh the advantages of fast access to the database via an already open database connection?

Demaestro

8:47 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would venture a guess on your question but there are so many factors...like your connection object type... your DB type... your server OS... too many variables to guess.

Under ideal circumstances I think once you have opened a connection for a DB hit... if you can continue to use the same connection for all the hits before closing it.... then the DB might be faster. To get it to a place where the files are faster you may have to look at caching them somehow, this might improve performance over a DB.

The best way to find the answer is to set up a duplicate site... move it over to reading files and then do some load texts on both. Once you have hard numbers to crunch you will have the answer.

To be honest I think the improvement if there was one would be negligible unless under high load times. Since I don't know how busy your site is I am not sure if high load times are an issue.

Can't hurt to set up the test though. if you have time I would love to hear what you do and what the results are.

[edited by: Demaestro at 8:47 pm (utc) on June 13, 2007]

frakilk

9:11 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



Great post Demaestro.

Yes I think a few tests may be in order as they are a lot of variable elements in the equation. It's not the busiest site in the world but it is growing. I just don't want it to become an issue down the line.

I've just had an idea right this moment, maybe instead of writing the cron job HTML to a file I could write it to a record in a special DB table instead and just do the most basic of SELECTs on the already open connection. Could work :)

Demaestro

9:21 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That is a good thought.... another thing to look into and I don't know why I didn't think of this before is query caching.

Do a search in your SQL documentation for it.... basically it will run a query once and get the output as a recordSet.... then when the same query is called it grabs the results from the last time it was run rather then rerunning it...... there are ways of dealing with "stale" results and that sort of thing... could be worth a try as well.

Demaestro

9:22 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



According to some MySql documentation

The query cache does not return stale data. When tables are modified, any relevant entries in the query cache are flushed.

inbound

9:38 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One word: RamDisk

Uses a defined amount of system memory to mimick a disk drive but works SUPERFAST. If you are working in Linux then you simply need to mount it in the most appropriate place and make sure the permissions are correct (then just write and read as you would normally). I think Windows treats them as different Drive letters but it's equally simple.

You are restricted to pre-defined RamDisk sizes with a default Linux installation if I remember correctly, I think you need to build linux with a custom config to change this default. For your use I think you need not worry.

frakilk

11:09 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



I just found a great article on query caching for MYSQL and it definitely looks to be the solution to my problem, and what's more it is an elegant solution. Thanks all, I will try to remember to post back my experiences with query caching.