gforge-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gforge-commits] gforge/www/include snippet_caching.php,1.4,1.5


From: tom
Subject: [Gforge-commits] gforge/www/include snippet_caching.php,1.4,1.5
Date: Wed, 12 May 2004 13:26:26 -0500

Update of /cvsroot/gforge/gforge/www/include
In directory db.perdue.net:/tmp/cvs-serv2268/www/include

Modified Files:
        snippet_caching.php 
Log Message:
Modified queries to use GROUP BY vs repeated queries.  This requires only 2 
queries rather than about 20; on my workstation it resulted in about a 20% 
speedup; should be better than that on large databases or setups where the 
application server and database server are on separate machines.

Index: snippet_caching.php
===================================================================
RCS file: /cvsroot/gforge/gforge/www/include/snippet_caching.php,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- snippet_caching.php 14 Mar 2004 16:50:54 -0000      1.4
+++ snippet_caching.php 12 May 2004 18:26:22 -0000      1.5
@@ -10,6 +10,22 @@
  */
 
 /**
+ * create_snippet_hash() - A little utility function to reduce duplicated code 
in snippet_mainpage()
+ * 
+ * @param      sql     String  A SQL query to fetch either snippets or 
categories from the database
+ * @param      field   String  The field name - either 'language' or 'category'
+ * @return An associative array filled with the results of the SQL query
+ */
+function create_snippet_hash($sql, $field) {
+       $res = db_query($sql);
+       $target = array();
+       while ($row = db_fetch_array($res)) {
+               $target[$row[$field]] = $row['count'];
+       }
+       return $target;
+}
+
+/**
  * snippet_mainpage() - Show the main page for the snippet library.
  */
 function snippet_mainpage() {
@@ -22,45 +38,38 @@
        <p>
        <table width="100%" border="0">
        <tr><td>
-
        </td></tr>
-
        <tr><td>
        
<strong>'.$Language->getText('snippet_caching','browse_by_language').':</strong>
        <ul>';
 
-       $count=count($SCRIPT_LANGUAGE);
-       for ($i=1; $i<$count; $i++) {
-               $sql="SELECT count(*) FROM snippet WHERE language=$i";
-               $result = db_query ($sql);
-
-               $return .= '
-               <li><a 
href="/snippet/browse.php?by=lang&lang='.$i.'">'.$SCRIPT_LANGUAGE[$i].'</a> 
('.db_result($result,0,0).')</li>';
+       $existing_snippets = create_snippet_hash("SELECT language, count(*) as 
count from snippet group by language", "language");
+       for ($i=1; $i<count($SCRIPT_LANGUAGE); $i++) {
+               $return .= '<li><a 
href="/snippet/browse.php?by=lang&lang='.$i.'">'.$SCRIPT_LANGUAGE[$i].'</a> (';
+               if ($existing_snippets[$i]) {
+                       $return .= $existing_snippets[$i].')</li>';
+               } else {
+                       $return .= '0)</li>';
+               }
        }
 
        $return .=      
-       '</ul></td>
-       <td>
+       '</ul></td><td>
        
<strong>'.$Language->getText('snippet_caching','browse_by_category').':</strong>
        <ul>';
-
-       $count=count($SCRIPT_CATEGORY);
-       for ($i=1; $i<$count; $i++) {
-               $sql="SELECT count(*) FROM snippet WHERE category=$i";
-               $result = db_query ($sql);
-
-               $return .= '
-               <li><a 
href="/snippet/browse.php?by=cat&cat='.$i.'">'.$SCRIPT_CATEGORY[$i].'</a> 
('.db_result($result,0,0).')</li>';
+       
+       $existing_categories = create_snippet_hash("SELECT category, count(*) 
as count from snippet group by category", "category");
+       for ($i=1; $i<count($SCRIPT_CATEGORY); $i++) {
+               $return .= '<li><a 
href="/snippet/browse.php?by=cat&cat='.$i.'">'.$SCRIPT_CATEGORY[$i].'</a> (';
+               if ($existing_categories[$i]) {
+                       $return .= $existing_categories[$i].')</li>';
+               } else {
+                       $return .= '0)</li>';
+               }
        }
 
-
-       $return .=
-       '</ul></td>
-       </tr>
-       </table></div>';
-
+       $return .= '</ul></td> </tr> </table></div>';
        return $return;
-
 }
 
 ?>





reply via email to

[Prev in Thread] Current Thread [Next in Thread]