Index: includes/path.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/path.inc,v
retrieving revision 1.38
diff -u -r1.38 path.inc
--- includes/path.inc	1 Jun 2009 11:23:27 -0000	1.38
+++ includes/path.inc	1 Jun 2009 22:26:50 -0000
@@ -87,7 +87,7 @@
           // Now fetch the aliases corresponding to these system paths.
           // We order by ASC and overwrite array keys to ensure the correct
           // alias is used when there are multiple aliases per path.
-          $map[$path_language] = db_query("SELECT src, dst FROM {url_alias} WHERE src IN(:system) AND language IN(:language, '') ORDER BY language ASC", array(
+          $map[$path_language] = db_query("SELECT src, dst FROM {url_alias} WHERE src IN(:system) AND language IN(:language, '') ORDER BY language ASC, pid DESC", array(
             ':system' => $system_paths,
             ':language' => $path_language
           ))->fetchAllKeyed();
@@ -108,7 +108,7 @@
       // For system paths which were not cached, query aliases individually.
       else if (!isset($no_aliases[$path_language][$path])) {
         // Get the most fitting result falling back with alias without language
-        $alias = db_query("SELECT dst FROM {url_alias} WHERE src = :src AND language IN(:language, '') ORDER BY language DESC", array(
+        $alias = db_query("SELECT dst FROM {url_alias} WHERE src = :src AND language IN(:language, '') ORDER BY language DESC, pid DESC", array(
           ':src' => $path,
           ':language' => $path_language
         ))->fetchField();
@@ -123,7 +123,7 @@
       $src = '';
       if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) {
         // Get the most fitting result falling back with alias without language
-        if ($src = db_query("SELECT src FROM {url_alias} WHERE dst = :dst AND language IN(:language, '') ORDER BY language DESC", array(
+        if ($src = db_query("SELECT src FROM {url_alias} WHERE dst = :dst AND language IN(:language, '') ORDER BY language DESC, pid DESC", array(
                      ':dst' => $path,
                      ':language' => $path_language))
             ->fetchField()) {
Index: modules/system/system.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.install,v
retrieving revision 1.334
diff -u -r1.334 system.install
--- modules/system/system.install	1 Jun 2009 22:07:10 -0000	1.334
+++ modules/system/system.install	1 Jun 2009 22:25:32 -0000
@@ -1340,11 +1340,11 @@
       ),
     ),
     'unique keys' => array(
-      'dst_language' => array('dst', 'language'),
+      'dst_language_pid' => array('dst', 'language', 'pid'),
     ),
     'primary key' => array('pid'),
     'indexes' => array(
-      'src_language' => array('src', 'language'),
+      'src_language_pid' => array('src', 'language', 'pid'),
     ),
   );
 
@@ -3525,6 +3525,18 @@
   return $ret;
 }
 
+ /**
+  * Improve indexes on the {url_alias} table.
+  */
+function system_update_7025() {
+  $ret = array();
+  db_drop_index($ret, 'url_alias', 'src_language');
+  db_drop_index($ret, 'url_alias', 'dst_language');
+  db_add_index($ret, 'url_alias', 'dst_language_pid', array('dst', 'language', 'pid'));
+  db_add_index($ret, 'url_alias', 'src_language_pid', array('src', 'language', 'pid'));
+  return $ret;
+}
+
 /**
  * @} End of "defgroup updates-6.x-to-7.x"
  * The next series of updates should start at 8000.

