Index: wp-includes/rewrite.php
===================================================================
--- wp-includes/rewrite.php	(revision 11300)
+++ wp-includes/rewrite.php	(working copy)
@@ -783,32 +783,58 @@
 	 */
 	function page_uri_index() {
 		global $wpdb;
+		
+		// get_page_uri() calls get_post(), so pre-fill the cache to avoid db calls
+		$pages = $wpdb->get_results("
+			SELECT	pages.*
+			FROM	$wpdb->posts as pages
+			WHERE	pages.post_type = 'page'
+			AND		pages.post_status IN ('publish', 'private')
+			");
+		
+		if ( !$pages )
+			return array(array(), array());
+		
+		update_post_cache($pages);
+		
+		// get pages in order of hierarchy, i.e. children after parents
+		$pages = get_page_hierarchy($pages);
+		
+		// now reverse it, because we need parents after children for rewrite rules to work properly
+		$pages = array_reverse($pages, true);
 
-		//get pages in order of hierarchy, i.e. children after parents
-		$posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'"));
-		//now reverse it, because we need parents after children for rewrite rules to work properly
-		$posts = array_reverse($posts, true);
-
+		// pre-fill the cache for page attachments, since they call get_post() as well
+		$page_attachments = $wpdb->get_results("
+			SELECT	attachments.*
+			FROM	$wpdb->posts as attachments
+			JOIN	$wpdb->posts as pages
+			ON		pages.ID = attachments.post_parent
+			WHERE	attachments.post_type = 'attachment'
+			AND		pages.post_type = 'page'
+			AND		pages.post_status IN ('publish', 'private')
+			");
+		
+		update_post_cache($page_attachments);
+		
+		// extract array of $page_id => array of $attachment_id
+		$attachments = array();
+		foreach ( $page_attachments as $attachment )
+			$attachments[$attachment->post_parent][] = $attachment->ID;
+		
 		$page_uris = array();
 		$page_attachment_uris = array();
-
-		if ( !$posts )
-			return array( array(), array() );
-
-		foreach ($posts as $id => $post) {
+		
+		foreach ( array_keys($pages) as $page_id ) {
 			// 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;
-				}
+			$page_uri = get_page_uri($page_id);
+			$page_uris[$page_uri] = $page_id;
+			
+			foreach ( (array) $attachments[$page_id] as $attachment_id ) {
+				$attachment_uri = get_page_uri($attachment_id);
+				$page_attachment_uris[$attachment_uri] = $attachment_id;
 			}
-
-			$page_uris[$uri] = $id;
 		}
-
+		
 		return array( $page_uris, $page_attachment_uris );
 	}
 
