Index: wp-includes/rewrite.php
===================================================================
--- wp-includes/rewrite.php	(revision 17191)
+++ wp-includes/rewrite.php	(working copy)
@@ -766,8 +766,11 @@
 	function page_uri_index() {
 		global $wpdb;
 
+		$pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'", OBJECT_K);
+		$ids = implode( ',', array_keys( $pages ) );
+		$attachments = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type= 'attachment' AND post_parent IN({$ids})");
 		//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'"));
+		$posts = get_page_hierarchy( $pages );
 
 		// If we have no pages get out quick
 		if ( !$posts )
@@ -780,18 +783,31 @@
 		$page_attachment_uris = array();
 
 		foreach ( $posts as $id => $post ) {
+			$curr_page = $pages[$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 ( !empty($attachments) ) {
-				foreach ( $attachments as $attachment ) {
-					$attach_uri = get_page_uri($attachment->ID);
-					$page_attachment_uris[$attach_uri] = $attachment->ID;
+			$uri = $curr_page->post_name;
+			
+			if( $curr_page->post_parent != $curr_page->ID ) {
+				while( $curr_page->post_parent != 0 ) {
+					$curr_page = $pages[$curr_page->post_parent];
+					$uri = $curr_page->post_name. "/" . $uri;
 				}
 			}
-
+			
 			$page_uris[$uri] = $id;
 		}
+		
+		if( !empty( $attachments ) ){
+			foreach ( $attachments as $attachment ) {								
+				$parent = $attachment;
+				$attach_uri = $attachment->post_name;
+				while( $parent->post_parent != 0 ){					
+					$parent = $pages[$parent->post_parent];
+					$attach_uri = $parent->post_name. "/" . $attach_uri;
+				}
+				$page_attachment_uris[$attach_uri] = $attachment->ID;
+			}
+		}
 
 		return array( $page_uris, $page_attachment_uris );
 	}
