### Eclipse Workspace Patch 1.0
#P wordpress-dev
Index: wp-includes/rewrite.php
===================================================================
--- wp-includes/rewrite.php	(revision 11259)
+++ wp-includes/rewrite.php	(working copy)
@@ -794,15 +794,21 @@
 
 		if ( !$posts )
 			return array( array(), array() );
-
+		
+		// get the set of posts with an attachment to reduce the number of queries inside the loop
+		$all_attachments = $wpdb->get_results( "SELECT post_parent FROM $wpdb->posts WHERE post_type = 'attachment'" );
+		
 		foreach ($posts as $id => $post) {
+				
 			// URL => page name
 			$uri = get_page_uri($id);
-			$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
-			if ( $attachments ) {
-				foreach ( $attachments as $attachment ) {
-					$attach_uri = get_page_uri($attachment->ID);
-					$page_attachment_uris[$attach_uri] = $attachment->ID;
+			if (in_array( $id, $all_attachments )) {
+				$attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
+				if ( $attachments ) {
+					foreach ( $attachments as $attachment ) {
+						$attach_uri = get_page_uri($attachment->ID);
+						$page_attachment_uris[$attach_uri] = $attachment->ID;
+					}
 				}
 			}
 

