Index: wp-admin/includes/schema.php
===================================================================
--- wp-admin/includes/schema.php	(revision 13185)
+++ wp-admin/includes/schema.php	(working copy)
@@ -145,7 +145,8 @@
   PRIMARY KEY  (ID),
   KEY post_name (post_name),
   KEY type_status_date (post_type,post_status,post_date,ID),
-  KEY post_parent (post_parent)
+  KEY post_parent (post_parent),
+  KEY post_author (post_author)
 ) $charset_collate;
 CREATE TABLE $wpdb->users (
   ID bigint(20) unsigned NOT NULL auto_increment,
Index: wp-admin/includes/template.php
===================================================================
--- wp-admin/includes/template.php	(revision 13185)
+++ wp-admin/includes/template.php	(working copy)
@@ -1789,16 +1789,17 @@
 }
 
 /**
- * {@internal Missing Short Description}}
+ * Generate HTML for a single row on the users.php admin panel.
  *
  * @since unknown
  *
- * @param unknown_type $user_object
- * @param unknown_type $style
- * @param unknown_type $role
- * @return unknown
+ * @param object $user_object
+ * @param string $style Optional. Attributes added to the TR element.  Must be sanitized.
+ * @param string $role Key for the $wp_roles array.
+ * @param int $numposts Optional. Post count to display for this user.  Defaults to zero, as in, a new user has made zero posts.
+ * @return string
  */
-function user_row( $user_object, $style = '', $role = '' ) {
+function user_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
 	global $wp_roles;
 
 	$current_user = wp_get_current_user();
@@ -1814,7 +1815,6 @@
 		$short_url = substr( $short_url, 0, -1 );
 	if ( strlen( $short_url ) > 35 )
 		$short_url = substr( $short_url, 0, 32 ).'...';
-	$numposts = get_usernumposts( $user_object->ID );
 	$checkbox = '';
 	// Check if the user for this row is editable
 	if ( current_user_can( 'edit_user', $user_object->ID ) ) {
Index: wp-admin/users.php
===================================================================
--- wp-admin/users.php	(revision 13185)
+++ wp-admin/users.php	(working copy)
@@ -208,9 +208,15 @@
 	$userspage = isset($_GET['userspage']) ? $_GET['userspage'] : null;
 	$role = isset($_GET['role']) ? $_GET['role'] : null;
 
-	// Query the users
+	// Query the user IDs for this page
 	$wp_user_search = new WP_User_Search($usersearch, $userspage, $role);
 
+	// Query the post counts for this page
+	$post_counts = get_users_numposts($wp_user_search->get_results());
+	
+	// Query the users for this page
+	cache_users($wp_user_search->get_results());
+
 	$messages = array();
 	if ( isset($_GET['update']) ) :
 		switch($_GET['update']) {
@@ -263,22 +269,14 @@
 <form id="list-filter" action="" method="get">
 <ul class="subsubsub">
 <?php
-$role_links = array();
-$avail_roles = array();
-$users_of_blog = get_users_of_blog();
-$total_users = count( $users_of_blog );
-foreach ( (array) $users_of_blog as $b_user ) {
-	$b_roles = unserialize($b_user->meta_value);
-	foreach ( (array) $b_roles as $b_role => $val ) {
-		if ( !isset($avail_roles[$b_role]) )
-			$avail_roles[$b_role] = 0;
-		$avail_roles[$b_role]++;
-	}
-}
+$users_of_blog = count_blog_users_by_role_intime();
+$total_users = $users_of_blog['total_users'];
+$avail_roles =& $users_of_blog['avail_roles'];
 unset($users_of_blog);
 
 $current_role = false;
 $class = empty($role) ? ' class="current"' : '';
+$role_links = array();
 $role_links[] = "<li><a href='users.php'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
 foreach ( $wp_roles->get_names() as $this_role => $name ) {
 	if ( !isset($avail_roles[$this_role]) )
@@ -372,7 +370,7 @@
 	$role = array_shift($roles);
 
 	$style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
-	echo "\n\t" . user_row($user_object, $style, $role);
+	echo "\n\t", user_row($user_object, $style, $role, $post_counts[(string)$userid]);
 }
 ?>
 </tbody>
Index: wp-includes/pluggable.php
===================================================================
--- wp-includes/pluggable.php	(revision 13185)
+++ wp-includes/pluggable.php	(working copy)
@@ -139,6 +139,40 @@
 }
 endif;
 
+if ( !function_exists('cache_users') ) :
+/**
+ * Retrieve info for user lists to prevent multiple queries by get_userdata()
+ *
+ * @since 3.0.0
+ *
+ * @param array $users User ID numbers list
+ */
+function cache_users( $users ) {
+	global $wpdb;
+
+	$clean = array();
+	foreach($users as $id) {
+		$id = (int) $id;
+		if (wp_cache_get($id, 'users')) {
+			// seems to be cached already
+		} else {
+			$clean[] = $id;
+		}
+	}
+
+	if ( 0 == count($clean) )
+		return;
+
+	$list = implode(',', $clean);
+
+	$result = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE ID IN ($list)", OBJECT_K);
+
+	foreach($result as $user_object) {
+		wp_cache_add($user_object->ID, $user_object, 'users');
+	}
+}
+endif;
+
 if ( !function_exists('get_user_by') ) :
 /**
  * Retrieve user info by a given field
Index: wp-includes/post.php
===================================================================
--- wp-includes/post.php	(revision 13185)
+++ wp-includes/post.php	(working copy)
@@ -3634,9 +3634,23 @@
  * @return string SQL code that can be added to a where clause.
  */
 function get_private_posts_cap_sql($post_type) {
-	global $user_ID;
-	$cap = '';
+	return get_posts_by_author_sql($post_type, FALSE);
+}
 
+/**
+ * Retrieve the post SQL based on capability, author, and type.
+ *
+ * See above for full description.
+ *
+ * @since 3.0.0
+ * @param string $post_type currently only supports 'post' or 'page'.
+ * @param bool $full Optional.  Returns a full WHERE statement instead of just an 'andalso' term.
+ * @param int $post_author Optional.  Query posts having a single author ID.
+ * @return string SQL WHERE code that can be added to a query.
+ */
+function get_posts_by_author_sql($post_type, $full = TRUE, $post_author = NULL) {
+	global $user_ID, $wpdb;
+
 	// Private posts
 	if ($post_type == 'post') {
 		$cap = 'read_private_posts';
@@ -3645,24 +3659,40 @@
 		$cap = 'read_private_pages';
 	// Dunno what it is, maybe plugins have their own post type?
 	} else {
+		$cap = '';
 		$cap = apply_filters('pub_priv_sql_capability', $cap);
 
 		if (empty($cap)) {
 			// We don't know what it is, filters don't change anything,
 			// so set the SQL up to return nothing.
-			return '1 = 0';
+			return ' 1 = 0 ';
 		}
 	}
 
-	$sql = '(post_status = \'publish\'';
+	if ($full) {
+		if (is_null($post_author)) {
+			$sql = $wpdb->prepare('WHERE post_type = %s AND ', $post_type);
+		} else {
+			$sql = $wpdb->prepare('WHERE post_author = %d AND post_type = %s AND ', $post_author, $post_type);
+		}
+	} else {
+		$sql = '';
+	}
 
+	$sql .= "(post_status = 'publish'";
+
 	if (current_user_can($cap)) {
 		// Does the user have the capability to view private posts? Guess so.
-		$sql .= ' OR post_status = \'private\'';
+		$sql .= " OR post_status = 'private'";
 	} elseif (is_user_logged_in()) {
 		// Users can view their own private posts.
-		$sql .= ' OR post_status = \'private\' AND post_author = \'' . $user_ID . '\'';
-	}
+		$id = (int) $user_ID;
+		if (is_null($post_author) || !$full) {
+			$sql .= " OR post_status = 'private' AND post_author = $id";
+		} elseif ($id == (int)$post_author) {
+			$sql .= " OR post_status = 'private'";
+		} // else none
+	} // else none
 
 	$sql .= ')';
 
@@ -4454,4 +4484,4 @@
 
 		add_filter('the_preview', '_set_preview');
 	}
-}
\ No newline at end of file
+}
Index: wp-includes/user.php
===================================================================
--- wp-includes/user.php	(revision 13185)
+++ wp-includes/user.php	(working copy)
@@ -178,12 +178,47 @@
  */
 function get_usernumposts($userid) {
 	global $wpdb;
-	$userid = (int) $userid;
-	$count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = %d AND post_type = 'post' AND ", $userid) . get_private_posts_cap_sql('post'));
+
+	$where = get_posts_by_author_sql('post', TRUE, $userid);
+
+	$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
+
 	return apply_filters('get_usernumposts', $count, $userid);
 }
 
 /**
+ * Number of posts written by a list of users.
+ *
+ * @since 3.0.0
+ * @param array $userid User ID number list.
+ * @return array Amount of posts each user has written.
+ */
+function get_users_numposts($users) {
+	global $wpdb;
+	
+	if (0 == count($users))
+		return array();
+	    
+	$userlist = implode(',', $users);
+	$where = get_posts_by_author_sql('post');
+
+	$result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
+
+	$count = array();
+	foreach($result as $row) {
+		$count[$row[0]] = $row[1];
+	}
+
+	foreach($users as $id) {
+		$id = (string) $id;
+		if (!isset($count[$id]))
+			$count[$id] = 0;
+	}
+
+	return $count;
+}
+
+/**
  * Check that the user login name and password is correct.
  *
  * @since 0.71
@@ -440,6 +475,93 @@
 	return true;
 }
 
+/**
+ * Count number of users who have each of the user roles.
+ *
+ * Assumes there are neither duplicated nor orphaned capabilities meta_values.
+ * Assumes the serialized array values are meaningless, only array keys are used.
+ * Intended scale for this version is 10^5 users.  It is not quite capable of that yet due to WP Bug #12257
+ * Memory exhaustion is expected at higher orders.
+ *
+ * @since 3.0.0
+ * @return array Includes a grand total and an array of counts indexed by role strings.
+ */
+function count_blog_users_by_role_inmem() {
+	global $wpdb, $blog_id;
+
+	$id = (int) $blog_id;
+	$blog_prefix = $wpdb->get_blog_prefix($id);
+	$avail_roles = array();
+
+	$users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" );
+
+	foreach ( $users_of_blog as $caps_meta ) {
+		$b_roles = unserialize($caps_meta);
+		if ( is_array($b_roles) ) {
+			foreach ( $b_roles as $b_role => $val ) {
+				if ( isset($avail_roles[$b_role]) )
+					$avail_roles[$b_role]++;
+				else
+					$avail_roles[$b_role] = 1;
+			}
+		}
+	}
+
+	$result = array();
+	$result['total_users'] = count( $users_of_blog );
+	$result['avail_roles'] =& $avail_roles;
+	
+	return $result;
+}
+
+/**
+ * Count number of users who have each of the user roles.
+ *
+ * Assumes there are neither duplicated nor orphaned capabilities meta_values.
+ * Assumes role names are unique phrases.  Same assumption made by WP_User_Search::prepare_query()
+ * Intended scale for this version is 10^7 users.
+ * CPU exhaustion is expected at higher orders.
+ *
+ * @since 3.0.0
+ * @return array Includes a grand total and an array of counts indexed by role strings.
+ */
+function count_blog_users_by_role_intime() {
+	global $wpdb, $blog_id, $wp_roles;
+
+	// Initialize
+	$id = (int) $blog_id;
+	$blog_prefix = $wpdb->get_blog_prefix($id);
+	$avail_roles = $wp_roles->get_names();
+
+	// Build a CPU-intensive query that will return concise information.
+	$select_count = array();
+	foreach ( $avail_roles as $this_role => $name ) {
+		$select_count[] = "COUNT(NULLIF(`meta_value` LIKE '%" . like_escape($this_role) . "%', FALSE))";
+	}
+	$select_count = implode(', ', $select_count);
+
+	// Add the meta_value index to the selection list, then run the query.
+	$row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N );
+
+	// Run the previous loop again to associate results with role names.
+	$col = 0;
+	$role_counts = array();
+	foreach ( $avail_roles as $this_role => $name ) {
+		$count = (int) $row[$col++];
+		if ($count > 0)
+			$role_counts[$this_role] = $count;
+	}
+
+	// Get the meta_value index from the end of the result set.
+	$total_users = (int) $row[$col];
+
+	$result = array();
+	$result['total_users'] = $total_users;
+	$result['avail_roles'] =& $role_counts;
+
+	return $result;
+}
+
 //
 // Private helper functions
 //
