=== modified file 'uc_order/uc_order.install'
--- uc_order/uc_order.install	2009-06-17 15:27:58 +0000
+++ uc_order/uc_order.install	2009-06-17 15:48:48 +0000
@@ -505,6 +505,8 @@
     ),
     'indexes' => array(
       'order_id' => array('order_id'),
+      'nid' => array('nid'),
+      'qty' => array('qty'),
     ),
     'primary key' => array('order_product_id'),
   );
@@ -990,3 +992,15 @@
 
   return $ret;
 }
+
+/**
+ * Add an index so that product and customer reports are usable with large datasets
+ */
+function uc_order_update_6011() {
+  $ret = array();
+
+  db_add_index($ret, 'uc_order_products', 'nid', array('nid'));
+  db_add_index($ret, 'uc_order_products', 'qty', array('qty'));
+
+  return $ret;
+}

=== modified file 'uc_reports/uc_reports.admin.inc'
--- uc_reports/uc_reports.admin.inc	2009-04-18 07:09:30 +0000
+++ uc_reports/uc_reports.admin.inc	2009-06-19 21:10:11 +0000
@@ -47,7 +47,7 @@
 
   $header = array(
     array('data' => t('#')),
-    array('data' => t('Customer'), 'field' => "ou.$last_name"),
+    array('data' => t('Customer'), 'field' => "o.$last_name"),
     array('data' => t('Username'), 'field' => "u.name"),
     array('data' => t('Orders'), 'field' => 'orders'),
     array('data' => t('Products'), 'field' => 'products'),
@@ -56,20 +56,8 @@
   );
   $csv_rows[] = array(t('#'), t('Customer'), t('Username'), t('Orders'), t('Products'), t('Total'), t('Average'));
 
-  $sql = '';
-  $sql_count = '';
-
-  switch ($GLOBALS['db_type']) {
-    case 'mysqli':
-    case 'mysql':
-      $sql = "SELECT u.uid, u.name, ou.$first_name, ou.$last_name, (SELECT COUNT(DISTINCT(order_id)) FROM {uc_orders} as o WHERE o.uid = u.uid AND o.order_status IN $order_statuses) as orders, (SELECT SUM(qty) FROM {uc_order_products} as ps LEFT JOIN {uc_orders} as os ON ps.order_id = os.order_id WHERE os.order_status IN $order_statuses AND os.uid = u.uid) as products, (SELECT SUM(ot.order_total) FROM {uc_orders} as ot WHERE ot.uid = u.uid AND ot.order_status IN $order_statuses) as total, ROUND((SELECT SUM(ot.order_total) FROM {uc_orders} as ot WHERE ot.uid = u.uid AND ot.order_status IN $order_statuses)/(SELECT COUNT(DISTINCT(order_id)) FROM {uc_orders} as o WHERE o.uid = u.uid AND o.order_status IN $order_statuses), 2) as average FROM {users} as u LEFT JOIN {uc_orders} as ou ON u.uid = ou.uid WHERE u.uid > 0 GROUP BY u.uid";
-      $sql_count = "SELECT COUNT(DISTINCT(u.uid)) FROM {users} as u LEFT JOIN {uc_orders} as ou ON u.uid = ou.uid WHERE u.uid > 0";
-      break;
-    case 'pgsql':
-      $sql = "SELECT u.uid, u.name, ou.$first_name, ou.$last_name, (SELECT COUNT(DISTINCT(order_id)) FROM {uc_orders} as o WHERE o.uid = u.uid AND o.order_status IN $order_statuses) as orders, (SELECT SUM(qty) FROM {uc_order_products} as ps LEFT JOIN {uc_orders} as os ON ps.order_id = os.order_id WHERE os.order_status IN $order_statuses AND os.uid = u.uid) as products, (SELECT SUM(ot.order_total) FROM {uc_orders} as ot WHERE ot.uid = u.uid AND ot.order_status IN $order_statuses) as total, ROUND((SELECT SUM(ot.order_total) FROM {uc_orders} as ot WHERE ot.uid = u.uid AND ot.order_status IN $order_statuses)/(SELECT COUNT(DISTINCT(order_id)) FROM {uc_orders} as o WHERE o.uid = u.uid AND o.order_status IN $order_statuses), 2) as average FROM {users} as u LEFT JOIN {uc_orders} as ou ON u.uid = ou.uid WHERE u.uid > 0 GROUP BY u.uid, u.name, ou.$first_name, ou.$last_name";
-      $sql_count = "SELECT COUNT(DISTINCT(u.uid)) FROM {users} as u LEFT JOIN {uc_orders} as ou ON u.uid = ou.uid WHERE u.uid > 0";
-      break;
-  }
+  $sql = "SELECT u.uid, u.name, o.$first_name, o.$last_name, COUNT(DISTINCT o.order_id) AS orders, SUM(op.qty) AS products, SUM(o.order_total) AS total, AVG(o.order_total) AS average FROM {uc_orders} AS o LEFT JOIN {users} AS u ON o.uid = u.uid JOIN (SELECT order_id, SUM(qty) AS qty FROM {uc_order_products} GROUP BY order_id) AS op ON o.order_id = op.order_id WHERE o.order_status IN $order_statuses GROUP BY CONCAT(u.uid, o.$first_name, o.$last_name)";
+  $sql_count = "SELECT COUNT(DISTINCT CONCAT(u.uid, o.$first_name, o.$last_name)) FROM {uc_orders} AS o LEFT JOIN {users} AS u ON o.uid = u.uid JOIN (SELECT order_id, SUM(qty) AS qty FROM {uc_order_products} GROUP BY order_id) AS op ON o.order_id = op.order_id WHERE o.order_status IN $order_statuses";
 
   $context = array(
     'revision' => 'formatted-original',

