Index: content.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/Attic/content.module,v
retrieving revision 1.301.2.109
diff -u -p -r1.301.2.109 content.module
--- content.module	14 Jul 2009 22:17:05 -0000	1.301.2.109
+++ content.module	15 Jul 2009 14:29:01 -0000
@@ -2019,6 +2019,10 @@ function content_table_schema($field = N
   if (!empty($field['columns'])) {
     foreach ($field['columns'] as $column => $attributes) {
       $column_name =  $field['field_name'] .'_'. $column;
+      if (isset($attributes['index']) && $attributes['index']) {
+        $schema['indexes'][$column_name] = array($column_name);
+        unset($attributes['index']);
+      }
       unset($attributes['column']);
       unset($attributes['sortable']);
       $schema['fields'][$column_name] = $attributes;
Index: includes/content.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/includes/Attic/content.admin.inc,v
retrieving revision 1.181.2.73
diff -u -p -r1.181.2.73 content.admin.inc
--- includes/content.admin.inc	14 Jul 2009 22:17:04 -0000	1.181.2.73
+++ includes/content.admin.inc	15 Jul 2009 20:15:32 -0000
@@ -1465,10 +1465,17 @@ function content_alter_db($previous_fiel
     db_create_table($ret, $new_table, $new_schema);
   }
   else {
-    // Or add fields to an existing table.
+    // Or add fields and/or indexes to an existing table.
     foreach ($new_schema['fields'] as $column => $attributes) {
-      if (!in_array($column, array('nid', 'vid', 'delta')) && !db_column_exists($new_table, $column)) {
-        db_add_field($ret, $new_table, $column, $attributes);
+      if (!in_array($column, array('nid', 'vid', 'delta'))) {
+        // Create the column if it does not exist.
+        if (!db_column_exists($new_table, $column)) {
+          db_add_field($ret, $new_table, $column, $attributes);
+        }
+        // Create the index if requested to, and it does not exist.
+        if (isset($new_schema['indexes'][$column]) && !content_db_index_exists($new_table, $column)) {
+          db_add_index($ret, $new_table, $column, $new_schema['indexes'][$column]);
+        }
       }
     }
   }
@@ -1610,6 +1617,30 @@ function content_alter_db($previous_fiel
 }
 
 /**
+ * Checks if an index exists.
+ *
+ * @todo: Remove this funcion when it is implemented in Drupal core itself.
+ * @link http://drupal.org/node/360854
+ *
+ * @param $table
+ *   Name of the table.
+ * @param $name
+ *   Name of the index.
+ * @return
+ *   TRUE if the table exists. Otherwise FALSE.
+ */
+function content_db_index_exists($table, $name) {
+  global $db_type;
+  if ($db_type == 'mysql' || $db_type == 'mysqli') {
+    return (bool)db_result(db_query("SHOW INDEX FROM {". $table ."} WHERE key_name = '$name'"));
+  }
+  elseif ($db_type == 'pgsql') {
+    return (bool)db_result(db_query("SELECT COUNT(indexname) FROM pg_indexes WHERE indexname = '$name'"));
+  }
+  return FALSE;
+}
+
+/**
  * Helper function for handling cleanup operations when schema changes are made.
  */
 function content_alter_db_cleanup() {
Index: modules/nodereference/nodereference.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/modules/nodereference/Attic/nodereference.install,v
retrieving revision 1.25.2.7
diff -u -p -r1.25.2.7 nodereference.install
--- modules/nodereference/nodereference.install	21 Oct 2008 23:13:22 -0000	1.25.2.7
+++ modules/nodereference/nodereference.install	15 Jul 2009 20:38:58 -0000
@@ -102,4 +102,65 @@ function nodereference_update_6000(&$san
   $ret['#finished'] = $sandbox['progress'] / count($sandbox['fields']);
 
   return $ret;
-}
\ No newline at end of file
+}
+
+/**
+ * Create an index by node reference column for all fields.
+ */
+function nodereference_update_6001(&$sandbox) {
+  include_once('./'. drupal_get_path('module', 'content') .'/content.install');
+  drupal_load('module', 'content');
+  include_once('./'. drupal_get_path('module', 'content') .'/includes/content.admin.inc');
+
+  $ret = array();
+
+  if (!isset($sandbox['progress'])) {
+    if ($abort = content_check_update('nodereference')) {
+      return $abort;
+    }
+
+    // Get the latest cache values and schema.
+    content_clear_type_cache(TRUE, TRUE);
+    $types = content_types_install();
+
+    if (empty($types)) {
+      return $ret;
+    }
+
+    $sandbox['fields'] = array();
+    foreach ($types as $type_name => $fields) {
+      foreach ($fields as $field) {
+        if ($field['type'] == 'nodereference') {
+          $sandbox['fields'][] = $field;
+        }
+      }
+    }
+
+    if (empty($sandbox['fields'])) {
+      return $ret;
+    }
+
+    $sandbox['progress'] = 0;
+    $sandbox['visited'] = array();
+  }
+
+  $field = $sandbox['fields'][$sandbox['progress']];
+
+  // We only want to process a field once -- if we hit it a second time,
+  // that means it's its own table and it should have already been updated.
+  if (!in_array($field['field_name'], $sandbox['visited'])) {
+    $db_info = content_database_info($field);
+    $table = $db_info['table'];
+    $attributes = $db_info['columns']['nid'];
+    $column = $attributes['column'];
+    if (!content_db_index_exists($table, $column)) {
+      db_add_index($ret, $table, $column, array($column));
+    }
+    $sandbox['visited'][] = $field['field_name'];
+  }
+
+  $sandbox['progress']++;
+  $ret['#finished'] = $sandbox['progress'] / count($sandbox['fields']);
+
+  return $ret;
+}
Index: modules/nodereference/nodereference.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/modules/nodereference/Attic/nodereference.module,v
retrieving revision 1.138.2.57
diff -u -p -r1.138.2.57 nodereference.module
--- modules/nodereference/nodereference.module	24 Jun 2009 16:07:46 -0000	1.138.2.57
+++ modules/nodereference/nodereference.module	15 Jul 2009 19:56:11 -0000
@@ -144,7 +144,7 @@ function nodereference_field_settings($o
 
     case 'database columns':
       $columns = array(
-        'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE),
+        'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'index' => TRUE),
       );
       return $columns;
 
Index: modules/userreference/userreference.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/modules/userreference/Attic/userreference.install,v
retrieving revision 1.23.2.6
diff -u -p -r1.23.2.6 userreference.install
--- modules/userreference/userreference.install	4 Oct 2008 13:14:23 -0000	1.23.2.6
+++ modules/userreference/userreference.install	15 Jul 2009 20:46:19 -0000
@@ -52,4 +52,65 @@ function userreference_update_6000() {
 
   $ret = array();
   return $ret;
-}
\ No newline at end of file
+}
+
+/**
+ * Create an index by user reference column for all fields.
+ */
+function userreference_update_6001(&$sandbox) {
+  include_once('./'. drupal_get_path('module', 'content') .'/content.install');
+  drupal_load('module', 'content');
+  include_once('./'. drupal_get_path('module', 'content') .'/includes/content.admin.inc');
+
+  $ret = array();
+
+  if (!isset($sandbox['progress'])) {
+    if ($abort = content_check_update('userreference')) {
+      return $abort;
+    }
+
+    // Get the latest cache values and schema.
+    content_clear_type_cache(TRUE, TRUE);
+    $types = content_types_install();
+
+    if (empty($types)) {
+      return $ret;
+    }
+
+    $sandbox['fields'] = array();
+    foreach ($types as $type_name => $fields) {
+      foreach ($fields as $field) {
+        if ($field['type'] == 'userreference') {
+          $sandbox['fields'][] = $field;
+        }
+      }
+    }
+
+    if (empty($sandbox['fields'])) {
+      return $ret;
+    }
+
+    $sandbox['progress'] = 0;
+    $sandbox['visited'] = array();
+  }
+
+  $field = $sandbox['fields'][$sandbox['progress']];
+
+  // We only want to process a field once -- if we hit it a second time,
+  // that means it's its own table and it should have already been updated.
+  if (!in_array($field['field_name'], $sandbox['visited'])) {
+    $db_info = content_database_info($field);
+    $table = $db_info['table'];
+    $attributes = $db_info['columns']['uid'];
+    $column = $attributes['column'];
+    if (!content_db_index_exists($table, $column)) {
+      db_add_index($ret, $table, $column, array($column));
+    }
+    $sandbox['visited'][] = $field['field_name'];
+  }
+
+  $sandbox['progress']++;
+  $ret['#finished'] = $sandbox['progress'] / count($sandbox['fields']);
+
+  return $ret;
+}
Index: modules/userreference/userreference.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/modules/userreference/Attic/userreference.module,v
retrieving revision 1.106.2.43
diff -u -p -r1.106.2.43 userreference.module
--- modules/userreference/userreference.module	2 Jun 2009 12:24:04 -0000	1.106.2.43
+++ modules/userreference/userreference.module	15 Jul 2009 19:56:39 -0000
@@ -141,7 +141,7 @@ function userreference_field_settings($o
 
     case 'database columns':
       $columns = array(
-        'uid' => array('type' => 'int', 'unsigned' => TRUE,  'not null' => FALSE),
+        'uid' => array('type' => 'int', 'unsigned' => TRUE,  'not null' => FALSE, 'index' => TRUE),
       );
       return $columns;
 
