How to efficiently check if taxonomy has children I am checking if a taxonomy term has children using taxonomy_get_children and I wonder if this is the most efficient way of doing it.
Since the function returns all the term objects, I believe it is not very efficient.
As arpitr says taxonomy_get_children() loads the child terms, so you are better of with a fork of this function.

I wanted to know whether a term a) is connected to any nodes and b) has any child terms.

/**
 * Helper function to check whether a term has no child terms and is not
 * connected to any node.
 *
 * @see taxonomy_get_children()
 */
function YOURMODULE_is_term_empty($tid, $vid) {
  $children = &amp;drupal_static(__FUNCTION__, array());

  // Is term connected to node?
  if (!isset($children[$tid])) {
    // Get id's of connected nodes. Make query lighter by using no pager,
    // setting limit to 1 and sending an empty array with fields to order on.
    $node_ids = taxonomy_select_nodes($tid, FALSE, 1, array());

    if (!empty($node_ids)) {
      $children[$tid] = false;
    }
  }

  // Does term have child terms?
  if (!isset($children[$tid])) {
    $query = db_select('taxonomy_term_data', 't');
    $query-&gt;join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
    $query-&gt;addField('t', 'tid');
    $query-&gt;condition('h.parent', $tid);
    $query-&gt;condition('t.vid', $vid);
    $query-&gt;addTag('term_access');
    $query_result = $query-&gt;execute()-&gt;fetchCol();

    $children[$tid] = empty($query_result);
  }

  return $children[$tid];
}


NB: I did not make this work recursively.