Index: wp-admin/includes/class-wp-media-list-table.php
===================================================================
--- wp-admin/includes/class-wp-media-list-table.php	(revision 17614)
+++ wp-admin/includes/class-wp-media-list-table.php	(working copy)
@@ -46,8 +46,19 @@
 		$type_links = array();
 		$_num_posts = (array) wp_count_attachments();
 		$_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
-		if ( !isset( $total_orphans ) )
-				$total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
+		if ( !isset( $total_orphans ) ) {
+			$args = array( 
+				'fields' => 'count',
+				'post_type' => 'attachment', 
+				'post_status' => 'any', 
+				'post_parent' => 0,
+				'suppress_filters' => false,
+				'query_context' => 'attachment_orphans_count',
+			);
+			
+			$total_orphans = intval( 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: wp-admin/includes/media.php
===================================================================
--- wp-admin/includes/media.php	(revision 17614)
+++ wp-admin/includes/media.php	(working copy)
@@ -42,9 +42,19 @@
 
 	$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 = intval( get_posts( $args ) );
+	}
+		
 	if ( empty($attachments) ) {
 		unset($tabs['gallery']);
 		return $tabs;
Index: wp-includes/query.php
===================================================================
--- wp-includes/query.php	(revision 17614)
+++ wp-includes/query.php	(working copy)
@@ -1369,6 +1378,7 @@
 			, 's'
 			, 'sentence'
 			, 'fields'
+			, 'query_context'
 		);
 
 		foreach ( $keys as $key ) {
@@ -2001,6 +2013,9 @@
 			case 'id=>parent':
 				$fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
 				break;
+			case 'count':
+				$fields = "COUNT(*)";
+				break;
 			default:
 				$fields = "$wpdb->posts.*";
 		}
@@ -2610,7 +2625,13 @@
 
 			return $r;
 		}
+		
+		if ( 'count' == $q['fields'] ) {
+			$this->posts = $wpdb->get_var($this->request);
 
+			return $this->posts;
+		}
+
 		$this->posts = $wpdb->get_results($this->request);
 
 		// Raw results filter.  Prior to status checks.
