Index: src/wp-admin/includes/class-wp-media-list-table.php
===================================================================
--- src/wp-admin/includes/class-wp-media-list-table.php	(revision 26975)
+++ src/wp-admin/includes/class-wp-media-list-table.php	(working copy)
@@ -42,12 +42,22 @@
 	}
 
 	function get_views() {
-		global $wpdb, $post_mime_types, $avail_post_mime_types;
+		global $post_mime_types, $avail_post_mime_types;
 
 		$type_links = array();
 		$_num_posts = (array) wp_count_attachments();
 		$_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
-		$total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
+
+		$args = array(
+			'fields' => 'count',
+			'post_type' => 'attachment',
+			'post_status' => 'any',
+			'post_parent' => 0,
+			'suppress_filters' => false,
+			'query_context' => 'attachment_orphans_count',
+		);
+		$total_orphans = (int) get_posts( $args );
+
 		$matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
 		foreach ( $matches as $type => $reals )
 			foreach ( $reals as $real )
Index: src/wp-admin/includes/media.php
===================================================================
--- src/wp-admin/includes/media.php	(revision 26975)
+++ src/wp-admin/includes/media.php	(working copy)
@@ -40,8 +40,6 @@
  * @return array $tabs with gallery if post has image attachment
  */
 function update_gallery_tab($tabs) {
-	global $wpdb;
-
 	if ( !isset($_REQUEST['post_id']) ) {
 		unset($tabs['gallery']);
 		return $tabs;
@@ -49,8 +47,17 @@
 
 	$post_id = intval($_REQUEST['post_id']);
 
-	if ( $post_id )
-		$attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) );
+	if ( $post_id ) {
+		$args = array(
+			'fields' => 'count',
+			'post_type' => 'attachment',
+			'post_status' => 'any',
+			'post_parent' => $post_id,
+			'suppress_filters' => false,
+			'query_context' => 'gallery_attachment_count',
+		);
+		$attachments = (int) get_posts( $args );
+ 	}
 
 	if ( empty($attachments) ) {
 		unset($tabs['gallery']);
Index: src/wp-includes/query.php
===================================================================
--- src/wp-includes/query.php	(revision 26975)
+++ src/wp-includes/query.php	(working copy)
@@ -1423,6 +1423,7 @@
 			, 'sentence'
 			, 'fields'
 			, 'menu_order'
+			, 'query_context' 
 		);
 
 		foreach ( $keys as $key ) {
@@ -2271,6 +2272,9 @@
 			case 'id=>parent':
 				$fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
 				break;
+			case 'count':
+				$fields = "COUNT(*)";
+				break;
 			default:
 				$fields = "$wpdb->posts.*";
 		}
@@ -2924,6 +2928,11 @@
 			return $r;
 		}
 
+		if ( 'count' == $q['fields'] ) { 
+			$this->posts = $wpdb->get_var( $this->request );
+			return $this->posts;
+		}
+		
 		$split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
 		$split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
 
