Index: modules/field/modules/field_sql_storage/field_sql_storage.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/field/modules/field_sql_storage/field_sql_storage.module,v
retrieving revision 1.19
diff -u -r1.19 field_sql_storage.module
--- modules/field/modules/field_sql_storage/field_sql_storage.module	22 Aug 2009 00:58:53 -0000	1.19
+++ modules/field/modules/field_sql_storage/field_sql_storage.module	29 Aug 2009 00:46:22 -0000
@@ -138,12 +138,6 @@
         'not null' => FALSE,
         'description' => 'The entity revision id this data is attached to, or NULL if the entity type is not versioned',
       ),
-      'delta' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'description' => 'The sequence number for this data item, used for multi-value fields',
-      ),
       // @todo Consider storing language as integer.
       'language' => array(
         'type' => 'varchar',
@@ -152,9 +146,24 @@
         'default' => '',
         'description' => 'The language for this data item.',
       ),
+      'delta' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'description' => 'The sequence number for this data item, used for multi-value fields',
+      ),
     ),
+    // @todo Is the primary key needed at all ?
     'primary key' => array('etid', 'entity_id', 'deleted', 'delta', 'language'),
-    // TODO : index on 'bundle'
+    'indexes' => array(
+      'etid' => array('etid'),
+      'bundle' => array('bundle'),
+      'deleted' => array('deleted'),
+      'entity_id' => array('entity_id'),
+      'entity_id' => array('entity_id'),
+      'revision_id' => array('revision_id'),
+      'language' => array('language'),
+    ),
   );
 
   // Add field columns.
@@ -428,8 +437,7 @@
   $query->join('field_config_entity_type', 'e', 't.etid = e.etid');
 
   $query
-    ->fields('t', array('bundle', 'entity_id', 'revision_id'))
-    ->fields('e', array('type'))
+    ->fields('t', array('bundle', 'entity_id', 'revision_id', 'etid'))
     // We need to ensure objects arrive in a consistent order for the
     // range() operation to work.
     ->orderBy('t.etid')
@@ -461,8 +469,22 @@
     if (in_array($column, $field_columns)) {
       $column = _field_sql_storage_columnname($field_name, $column);
     }
+    // Translate entity types into numeric ids, to avoid adding conditions on
+    // the external field_config_entity_type table.
+    if ($column == 'type') {
+      $column = 'etid';
+      if (is_array($value)) {
+        foreach (array_keys($value) as $key) {
+          $value[$key] = _field_sql_storage_etid($value[$key]);
+        }
+      }
+      else {
+        $value = _field_sql_storage_etid($value);
+      }
+    }
     $query->condition($column, $value, $operator);
 
+
     if ($column == 'deleted') {
       $deleted = $value;
     }
@@ -493,6 +515,7 @@
       $cursor++;
       // If querying all revisions and the entity type has revisions, we need
       // to key the results by revision_ids.
+      $type =
       $entity_type = field_info_fieldable_types($row->type);
       $id = ($load_current || empty($entity_type['object keys']['revision'])) ? $row->entity_id : $row->revision_id;
 
