Index: src/wp-includes/post.php
===================================================================
--- src/wp-includes/post.php	(revision 28187)
+++ src/wp-includes/post.php	(working copy)
@@ -2271,14 +2271,37 @@
 function wp_count_attachments( $mime_type = '' ) {
 	global $wpdb;
 
-	$and = wp_post_mime_type_where( $mime_type );
-	$count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A );
+	if ( empty( $mime_type ) ) {
+		$key = 'default';
+	} else {
+		if ( is_array( $mime_type ) ) {
+			$key = implode( ',', $mime_type );
+		} else {
+			$key = $mime_type;
+		}
+	}
 
-	$counts = array();
-	foreach( (array) $count as $row ) {
-		$counts[ $row['post_mime_type'] ] = $row['num_posts'];
+	$counts_cache = wp_cache_get( 'wp_count_attachments' );
+
+	if ( false !== $counts_cache && ! empty( $counts_cache[ $key ] ) ) {
+		$counts = $counts_cache[ $key ];
+	} else {
+		$and = wp_post_mime_type_where( $mime_type );
+		$count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A );
+
+		$counts = array();
+		foreach( (array) $count as $row ) {
+			$counts[ $row['post_mime_type'] ] = $row['num_posts'];
+		}
+		$counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and");
+
+		if ( ! is_array( $counts_cache ) ) {
+			$counts_cache = array();
+		}
+		$counts_cache[ $key ] = $counts;
+
+		wp_cache_set( 'wp_count_attachments', $counts_cache );
 	}
-	$counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and");
 
 	/**
 	 * Modify returned attachment counts by mime type.
@@ -2292,6 +2315,18 @@
 }
 
 /**
+ * Helper function to clear the cache for number of attachments.
+ *
+ * @private
+ */
+function __clear_count_attachments_cache( $new_status, $old_status, $post ) {
+	if ( 'attachment' === $post->post_type ) {
+		wp_cache_delete( 'wp_count_attachments' );
+	}
+}
+add_action( 'transition_post_status', '__clear_count_attachments_cache', 1, 3 );
+
+/**
  * Get default post mime types
  *
  * @since 2.9.0
