[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Comparison: Caching data to an external file or retrieving from MySQL database?

Posted on 2005-04-06
6
Medium Priority
?
193 Views
Last Modified: 2013-12-12
When dealing with moderate amounts of information, such as script settings (roughly 50-75 settings), etc... which is more effecient: writing the data to an external file to be included from there-on-out, or quering the database to retrieve the information? I ask becuase I'd like to get my page query count as low as possible, and I could eliminate one more query if I just included the settiings from a file, but if it's better to query it, I'll do that. Any examples, documentation, anything?
0
Comment
Question by:MaybeItsJeremy
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
matt_mcswain earned 1000 total points
ID: 13724104
If you're just talking about static settings that either don't or rarily change, then it's most likely better to use a flat file; be in php, text, xml, etc. The same goes for page generation. Dynamic caching can produce huge gains in performance. It all depends how often content is updated and how often content is requested. If you're creating a database driven website, and I know you are, it's pretty inefficient for on every page request to have to parse php which then makes expensive database calls and then generates the output....Everytime. Instead, why not just generate the html once and serving that on every request. Unless, of course, your content is highly dynamic, then if might not be worth your time or just impractical. Or if your site doesn't get any hits, then why bother. :)

Your definitely right in trying to limit queries; put those all on the backend. And your in good shape using Smarty, because it uses caching when it can too. Here's a page that discusses all different kinds of optimizing.-->http://phplens.com/lens/php-book/optimizing-debugging-php.php

Sure php and mysql are fast, but if your page or large portions of your page is only changed once a week, or once a day, or even once a hour; it might be time for some serious caching. Your server will thank you.

0
 
LVL 8

Assisted Solution

by:_Marcel_
_Marcel_ earned 1000 total points
ID: 13724905
Normally all settings will go into a file that you can include. Using a database isn't normal and you would do so only if you have little other choice (because of changing it through a remote application or something like that). If you need the settings to be easily changed in a user-friendly way, you'll probably will want to use a ini-file  (user parse_ini_file() for this: http://www.php.net/manual/en/function.parse-ini-file.php Otherwise just use a php-file in which you set your variables or constants. If you really will need fast access and you are also concerned about I/O operations and/or CPU you might want to use shared memory (it's fast, but might be too much work for a small gain): http://www.php.net/manual/en/ref.sem.php

All in all, I'll say just include the file (remember that your operating system is smart: when the file is read often, the system will cache it also, and it will be served from memory!)
0
 

Author Comment

by:MaybeItsJeremy
ID: 13726778
Really sounds like I need to go with writing the data to a file... I didn't think about the server being able to cache the file, that will be perfect. One other question before I split the points here, let's say we have a news site with a HUGE amount of traffic (as in several thousand page views per day) would it be smart to instead of query the database for the content of an article, just retrieve the data from a cached file (in addition to having it stored in the DB for back-up)? I was thinking a method could be set up in the AdminCP clean up to check the time stamp of the file (just putting it in the file name for the cache file, like timestamp.01.xml) and if it's over a week or two old, then delete the cached file, as most people will not be going to it any longer and will be concetrating on recent news. And if someone were to go to the old article, it would just come from the DB.... does that sound like a good plan?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:_Marcel_
ID: 13727508
Line  matt_mcswain already said, serving a static page is often better than serving a dynamic page with mysql requests. The first gain would be that the database-server would be (far) less questioned, and the second gain would be that the webserver could easily cache the page, and if it is requested often, would be able to serve it from memory, again lessening your I/O. Normally you would have yet another advantage: since the page is static it would also be easier for the browser to cache (with dynamic pages it is often difficult to determine if the page has been changed). In your case this would not benefit you much, because I guess most people wouldn't be reading the same news more than once...

So I think your plan would be right (unless you have of course plenty of money and don't care about server capacity).
0
 
LVL 11

Expert Comment

by:matt_mcswain
ID: 13729028
You just have to decide how much time you have to put towards caching pages; how much is worth your time. On a site like that, it would definitely be worth auto-generating the home page at least when an update is made; instead, for instance, pulling the top story, top related stories, and top stories for other categories, etc from the database; however your site works. Is it worth your time to implement the fuctionality for each section of the news? or even each news story itself? Again, I'm not talking about caching in memory(like _Marcel_ said, let your OS worry about that), I'm talking about generating html files. If you wanted to go nuts, you could created a very powerful database driven site, that never accesses the database or php on user request. I wouldn't because changing the layout would be a mess, and I rely on Smarty for that type of thing. But instead, slowly minimize the database calls on the frontend until you're happy with the performance.

Oh, and pulling old articles from the database and just auto-gerenerating fresh ones, sounds very reasonable.
0
 

Author Comment

by:MaybeItsJeremy
ID: 13731377
Thanks guys. The advice is much appreciated. :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question