--- modules/taxonomy/taxonomy.module	2010-09-07 17:33:17.000000000 +0200
+++ modules/taxonomy/taxonomyNew.module	2010-09-07 17:35:20.000000000 +0200
@@ -912,48 +912,23 @@ function taxonomy_get_synonym_root($syno
 function taxonomy_term_count_nodes($tid, $type = 0) {
   static $count;
 
-  if (!isset($count[$type])) {
-    // $type == 0 always evaluates TRUE if $type is a string
+  $term = taxonomy_get_term($tid);
+  $tree = taxonomy_get_tree($term->vid, $tid);
+
+  $tids = array($tid);
+  foreach ($tree as $descendant) {
+    $tids[] = $descendant->tid;
+  }
+
+  if (!isset($count[$type][$tid])) {
     if (is_numeric($type)) {
-      $result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 GROUP BY t.tid'));
+       $count[$type][$tid] = db_result(db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 AND t.tid IN (%s)"), implode(',', $tids)));
     }
     else {
-      $result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
-    }
-    $count[$type] = array();
-    while ($term = db_fetch_object($result)) {
-      $count[$type][$term->tid] = $term->c;
-    }
-  }
-  $children_count = 0;
-  foreach (_taxonomy_term_children($tid) as $c) {
-    $children_count += taxonomy_term_count_nodes($c, $type);
-  }
-  return $children_count + (isset($count[$type][$tid]) ? $count[$type][$tid] : 0);
-}
-
-/**
- * Helper for taxonomy_term_count_nodes(). Used to find out
- * which terms are children of a parent term.
- *
- * @param $tid
- *   The parent term's ID
- *
- * @return array
- *   An array of term IDs representing the children of $tid.
- *   Results are statically cached.
- *
- */
-function _taxonomy_term_children($tid) {
-  static $children;
-
-  if (!isset($children)) {
-    $result = db_query('SELECT tid, parent FROM {term_hierarchy}');
-    while ($term = db_fetch_object($result)) {
-      $children[$term->parent][] = $term->tid;
+       $count[$type][$tid] = db_result(db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 AND n.type = '%s' AND t.tid IN (%s)"), $type, implode(',', $tids)));
     }
   }
-  return isset($children[$tid]) ? $children[$tid] : array();
+  return $count[$type][$tid];
 }
 
 /**
