Home arrow PHP arrow Page 4 - PHP Application Development With ADODB (part 2)

Cache Cow - PHP

In this concluding article, find out about ADODB's advancedfunctions, with examples that demonstrate how ADODB can be used tooptimize multiple-run queries, commit and roll back transactions,improve performance by caching query results, and automatically writeHTML (ortext) files.

TABLE OF CONTENTS:
  1. PHP Application Development With ADODB (part 2)
  2. Rapid Execution
  3. A Fear Of Commitment
  4. Cache Cow
  5. What's On The Menu?
  6. A Rose By Any Other Name...
  7. The Final Countdown
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 8
July 31, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
One of the coolest things about ADODB has to be its support for cached queries. Why? Because caching your queries can result in a fairly significant performance improvement, especially if you're executing the same tired old SELECT every time.

In order to illustrate the difference, let's take a look at how this normally works:

<?php // uncomment this to see plaintext output in your browser // header("Content-Type: text/plain"); // include the ADODB library include("adodb.inc.php"); // create an object instance // configure it for a MySQL connection $db = NewADOConnection("mysql"); // open connection to database $db->Connect("localhost", "john", "doe", "db278") or die("Unable to connect!"); // execute query $query = "SELECT * FROM library"; $result = $db->Execute($query) or die("Error in query: $query. " . $db->ErrorMsg()); // iterate through resultset // print column data in format TITLE - AUTHOR while (!$result->EOF) { echo $result->fields[1] . " - " . $result->fields[2] . "\n"; $result->MoveNext(); } // get and print number of rows in resultset echo "\n[" . $result->RecordCount() . " rows returned]\n"; // close database connection $db->Close(); ?>
This should be familiar to you by now - it's a very basic SQL SELECT operation with ADODB. If this was your personal Web site, and you were getting 5000 hits a minute, you'd be running the query above 30,000 times an hour. As you might imagine, this will have your database server scurrying around like a hamster on cocaine - not to mention affecting the performance of your Web site.

ADODB offers a better option - caching the results of the first SELECT query, and using this cached resultset in each subsequent run of the query. This reduces the load on the database server, and can also provide you with an incremental performance benefit.

Here's what the revised script looks like:

<?php // uncomment this to see plaintext output in your browser // header("Content-Type: text/plain"); // include the ADODB library include("adodb.inc.php"); // set cache location $ADODB_CACHE_DIR = '.'; // create an object instance // configure it for a MySQL connection $db = NewADOConnection("mysql"); // open connection to database $db->Connect("localhost", "john", "doe", "db278") or die("Unable to connect!"); // execute query $query = "SELECT * FROM library"; $result = $db->CacheExecute(300,$query) or die("Error in query: $query. " . $db->ErrorMsg()); // iterate through resultset // print column data in format TITLE - AUTHOR while (!$result->EOF) { echo $result->fields[1] . " - " . $result->fields[2] . "\n"; $result->MoveNext(); } // get and print number of rows in resultset echo "\n[" . $result->RecordCount() . " rows returned]\n"; // close database connection $db->Close(); ?>
The first argument to CacheExecute() is the number of seconds to cache the query results; the second is, obviously, the query string itself. The remainder of the script remains unchanged - a cached resultset is processed in exactly the same manner as a non-cached one.

You can also use the CacheFlush() method to flush all queries from the cache.

 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap

Dev Shed Tutorial Topics: