? mw.patch
Index: includes/query.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/includes/query.inc,v
retrieving revision 1.24
diff -u -F^f -r1.24 query.inc
--- includes/query.inc	14 Aug 2008 19:32:20 -0000	1.24
+++ includes/query.inc	16 Aug 2008 02:19:25 -0000
@@ -834,12 +834,33 @@
    *   Provide a countquery if this is true, otherwise provide a normal query.
    */
   function query($get_count = FALSE) {
+    
     // Check query distinct value.
     if (empty($this->no_distinct) && $this->distinct && !empty($this->fields)) {
       if (!empty($this->fields[$this->base_field])) {
         $this->fields[$this->base_field]['distinct'] = TRUE;
       }
     }
+    
+    /**
+     * An optimized count query includes just the base field instead of all the fields.
+     * Determine of this query qualifies by checking for a groupby or distinct.
+     */
+    $fields_array = $this->fields;
+    if ($get_count && !$this->groupby) {
+      foreach ($fields_array as $field) {
+        if (isset($field['distinct'])) {
+          $get_count_optimized = FALSE;
+          break;
+        }
+      }
+    }
+    else {
+      $get_count_optimized = FALSE;
+    }
+    if (!isset($get_count_optimized)) {
+      $get_count_optimized = TRUE;
+    }
 
     $joins = $fields = $where = $having = $orderby = $groupby = '';
     // Add all the tables to the query via joins. We assume all LEFT joins.
@@ -849,8 +870,7 @@
       }
     }
 
-    // If it's not a count query, add our fields
-    $fields_array = $this->fields;
+
     foreach ($fields_array as $field) {
       if ($fields) {
         $fields .= ",\n   ";
@@ -870,6 +890,11 @@
         $string .= " AS $field[alias]";
       }
       $fields .= $string;
+      
+      if ($get_count_optimized) {
+        // We only want the first field in this case.
+        break;
+      }
     }
 
     if ($this->groupby) {
@@ -879,7 +904,7 @@
       }
     }
 
-    if (!$get_count) {
+    if (!$get_count_optimized) {
       // we only add the groupby if we're not counting.
       if ($this->orderby) {
         $orderby = "ORDER BY " . implode(', ', $this->orderby) . "\n";
Index: includes/view.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/includes/view.inc,v
retrieving revision 1.122
diff -u -F^f -r1.122 view.inc
--- includes/view.inc	13 Aug 2008 17:08:27 -0000	1.122
+++ includes/view.inc	16 Aug 2008 02:19:25 -0000
@@ -578,6 +578,7 @@
     }
 
     $this->build_info['query'] = $this->query->query();
+    $this->build_info['count_query'] = $this->query->query(TRUE);
     $this->build_info['query_args'] = $this->query->get_where_args();
     $this->built = TRUE;
     $this->build_time = microtime() - $start;
@@ -620,6 +621,7 @@
     }
 
     $query = db_rewrite_sql($this->build_info['query'], $this->base_table, $this->base_field, array('view' => &$this));
+    $count_query = db_rewrite_sql($this->build_info['count_query'], $this->base_table, $this->base_field, array('view' => &$this));
 
     $args = $this->build_info['query_args'];
 
@@ -642,9 +644,6 @@
         $external = TRUE;
       }
 
-      // make count from subselect now
-      $count_query = "SELECT COUNT(*) FROM ($query) AS count_alias";
-
       $start = microtime();
       if (!empty($this->pager['items_per_page'])) {
         // We no longer use pager_query() here because pager_query() does not
