Index: src/wp-includes/media.php
===================================================================
--- src/wp-includes/media.php	(revision 32485)
+++ src/wp-includes/media.php	(working copy)
@@ -2918,26 +2918,39 @@
 		}
 	}
 
-	$has_audio = $wpdb->get_var( "
-		SELECT ID
-		FROM $wpdb->posts
-		WHERE post_type = 'attachment'
-		AND post_mime_type LIKE 'audio%'
-		LIMIT 1
-	" );
-	$has_video = $wpdb->get_var( "
-		SELECT ID
-		FROM $wpdb->posts
-		WHERE post_type = 'attachment'
-		AND post_mime_type LIKE 'video%'
-		LIMIT 1
-	" );
-	$months = $wpdb->get_results( $wpdb->prepare( "
-		SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
-		FROM $wpdb->posts
-		WHERE post_type = %s
-		ORDER BY post_date DESC
-	", 'attachment' ) );
+	// Cache these expensive queries
+	if ( false === ( $has_audio = get_transient( 'has_audio' ) ) ) {
+		$has_audio = $wpdb->get_var( "
+			SELECT ID
+			FROM $wpdb->posts
+			WHERE post_type = 'attachment'
+			AND post_mime_type LIKE 'audio%'
+			LIMIT 1
+		" );
+		$has_audio = $has_audio ? 1 : 0;
+		set_transient( 'has_audio', $has_audio );
+	}
+	if ( false === ( $has_video = get_transient( 'has_video' ) ) ) {
+		$has_video = $wpdb->get_var( "
+			SELECT ID
+			FROM $wpdb->posts
+			WHERE post_type = 'attachment'
+			AND post_mime_type LIKE 'video%'
+			LIMIT 1
+		" );
+		$has_video = $has_video ? 1 : 0;
+		set_transient( 'has_video', $has_video );
+	}
+	if ( false === ( $months = get_transient( 'media_months' ) ) ) {
+		$months = $wpdb->get_results( $wpdb->prepare( "
+			SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
+			FROM $wpdb->posts
+			WHERE post_type = %s
+			ORDER BY post_date DESC
+		", 'attachment' ) );
+		set_transient( 'media_months', $months );
+	}
+
 	foreach ( $months as $month_year ) {
 		$month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year );
 	}
@@ -2956,8 +2969,8 @@
 		),
 		'defaultProps' => $props,
 		'attachmentCounts' => array(
-			'audio' => ( $has_audio ) ? 1 : 0,
-			'video' => ( $has_video ) ? 1 : 0
+			'audio' => intval( $has_audio ),
+			'video' => intval( $has_video )
 		),
 		'embedExts'    => $exts,
 		'embedMimes'   => $ext_mimes,
Index: src/wp-includes/post.php
===================================================================
--- src/wp-includes/post.php	(revision 32485)
+++ src/wp-includes/post.php	(working copy)
@@ -3477,7 +3477,16 @@
 			 */
 			do_action( 'edit_attachment', $post_ID );
 		} else {
+			// Delete persistent cache transients set in wp_enqueue_media
+			$mime_type_prefix = substr($postarr['post_mime_type'], 0, 5);
+			if ( 'video' === $mime_type_prefix ) {
+				delete_transient( 'has_video' );
+			} else if ( 'audio' === $mime_type_prefix ) {
+				delete_transient( 'has_audio' );
+			}
 
+			delete_transient( 'media_months' );
+
 			/**
 			 * Fires once an attachment has been added.
 			 *
