对查询进行缓存
上个月,我们简单地了解了adodb中,如何进行select、insert和update的操作。如果你在adodb上是个新手,我建议先读一下上个月的那篇文章。 adodb 还有很多更高级的特征,这个月,我们就一起来关注其中的部分内容。
数据库时常会成为应用程序效率低下的祸首。尽量减少对数据库的查询,是提高执行效率的方法之一。这,通常可以通过对整页内容进行缓存(有很多种方法来实现。比如,pear->cache),或者,如果你需要做一张动态页面,并且只想让查询指令被缓存,那么,你可以使用adodb,简单地将查询指令缓存起来。在你视图通过缓存来提高你的应用程序的糟糕性能之前,我建议你先试图去优化你的查询指令。有时候,一些简单的索引可以改变一切——有太多的所谓的专业的解决方案,都在使用糟糕的索引。在本文中,你能找到很多这样的实例。现在,让我们来看看adodb是如何使你能够对数据库的查询结果进行缓存的。在这个实例中,adodb把我们的最后的一次查询的结果保存在/var/tmp/adodb_cache这个缓存文件中,并保留10分钟。
| php">include("$adodb_path/db_values.inc.php"); include("$adodb_path/adodb.inc.php"); $db = newadoconnection('$database_type'); $db->connect("$host", "$user", "$password", "employees"); $adodb_cache_dir = "/var/tmp/adodb_cache"; //directory to store cached files $sql = "select surname, age from employees"; $rs = &$db->cacheexecute(600,$sql); // executes, and caches the results for 600 seconds if (!$rs) { print $db->errormsg(); // displays the error message if no results could be returned } else { while (!$rs->eof) { print $rs->fields[0].' '.$rs->fields[1].'<br>'; // fields[0] is surname, fields[1] is age $rs->movenext(); // moves to the next row } // end while } // end else |
| php">$sql = "select surname, age from employees"; $rs = &$db->cacheexecute(600,$sql); // executes, and caches the results for 600 seconds print $rs->recordcount() . " rows returned]"; // display number of rows returned |
| php">$sql = "select surname, age from employees"; $rs = &$db->cacheexecute(600,$sql); // executes, and caches the results for 600 seconds print $rs->fieldcount() . " columns returned]"; // display number of rows returned |
| php">$sql = "select surname, age from employees"; $rs = &$db->selectlimit($sql, 10, 100); // select 10 rows, starting at row 100 if (!$rs) { print $db->errormsg(); // displays the error message if no results could be returned } else { while (!$rs->eof) { print $rs->fields[0].' '.$rs->fields[1].'<br>'; // fields[0] is surname, fields[1] is age $rs->movenext(); // moves to the next row } // end while } // end else |
| php">$sql1 = "update employees set balance=balance-10 where id=15"; $sql2 = "update employees set balance=balance+10 where id=22"; $db->starttrans(); $db->execute($sql); $db->execute($sql2); $db->completetrans(); |
| php">$sql1 = "update employees set balance=balance-10 where id=15"; $sql2 = "update employees set balance=balance+10 where id=22"; $db->starttrans(); $db->execute($sql); $db->execute($sql2); $db->completetrans(); if ($db->hasfailedtrans()) { // something went wrong } |
值得注意的是,你的数据库需要支持这些事务函数。 (大多数的数据库是支持的,不过,mysql innodb表支持,可 mysql myisam 表不支持。)
我希望我所做的一切能让你对数据库库函数能有极大的兴趣。还有大量有趣的函数可以用来从数据库表中自动生成html,并且同一结果有不同的方法来实现。你可以找到完整的手册。
祝你好运!
原文地址:http://www.databasejournal.com/features/php/article.php/2234861
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器