Index: modules/taxonomy/taxonomy.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.module,v
retrieving revision 1.442
diff -u -p -r1.442 taxonomy.module
--- modules/taxonomy/taxonomy.module	13 Nov 2008 08:13:56 -0000	1.442
+++ modules/taxonomy/taxonomy.module	20 Nov 2008 16:46:08 -0000
@@ -928,49 +928,26 @@ 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
-    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'));
-    }
-    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;
+  if (!isset($count[$type][$tid])) {
+    $term = taxonomy_term_load($tid);
+    if ($term) {
+      $tree = taxonomy_get_tree($term->vid, $tid);
+      $tids = array($tid);
+      foreach ($tree as $descendent) {
+        $tids[] =  $descendent->tid;
+      }
+      $query = db_select('term_node', 't');
+      $query->addExpression('COUNT(DISTINCT(t.nid))', 'nid_count');
+      $query->join('node', 'n', 't.vid = n.vid');
+      $query->condition('t.tid', $tids, 'IN');
+      $query->condition('n.status', 1);
+      if (!is_numeric($type)) {
+        $query->condition('n.type', $type);
+      }
+      $count[$type][$tid] = $query->execute()->fetchField();
     }
   }
-  return isset($children[$tid]) ? $children[$tid] : array();
+  return isset($count[$type][$tid]) ? (int) $count[$type][$tid] : FALSE;
 }
 
 /**
Index: modules/taxonomy/taxonomy.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.test,v
retrieving revision 1.13
diff -u -p -r1.13 taxonomy.test
--- modules/taxonomy/taxonomy.test	16 Nov 2008 14:59:33 -0000	1.13
+++ modules/taxonomy/taxonomy.test	20 Nov 2008 16:46:10 -0000
@@ -279,6 +279,10 @@ class TaxonomyTermTestCase extends Drupa
     $this->drupalGet('node/' . $node->nid);
     $this->assertText($term1->name, t('Term is displayed when viewing the node.'));
 
+    // Check that the term is found in the term_node table.
+    $count = taxonomy_term_count_nodes($term1->tid);
+    $this->assertTrue($count == 1, t('Term attached to @count published nodes'), array('@count' => $count));
+
     // Edit the node with a different term.
     $edit['taxonomy[' . $this->vocabulary->vid . ']'] = $term2->tid;
     $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
@@ -286,13 +290,20 @@ class TaxonomyTermTestCase extends Drupa
     $this->drupalGet('node/' . $node->nid);
     $this->assertText($term2->name, t('Term is displayed when viewing the node.'));
 
+    // Check that the term is found in the term_node table.
+    $count = taxonomy_term_count_nodes($term2->tid);
+    $this->assertTrue($count == 1, t('Term attached to @count published nodes.'), array('@count' => $count));
+
     // Delete node through browser.
     $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
     $this->drupalGet('node/' . $node->nid);
-    $this->assertNoText($term2->name, t('Checking if node exists'));
-    // Checking database fields.
-    $result = db_query('SELECT * FROM {term_node} WHERE nid = :nid', array(':nid' => $node->nid))->fetch();
-    $this->assertTrue(empty($result), t('Term/node relationships are no longer in the database table.'));
+    $this->assertNoText($term2->name, t('Checking if node exists.'));
+
+    // Confirm that term_node records have been removed.
+    $count = taxonomy_term_count_nodes($term1->tid);
+    $this->assertTrue($count == 0, t('Term associated with @count nodes.', array('@count' => $count)));
+    $count = taxonomy_term_count_nodes($term2->tid);
+    $this->assertTrue($count == 0, t('Term associated with @count nodes.', array('@count' => $count)));
   }
 
   /**
