diff -u -r -N C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments.orig/apachesolr_attachments.admin.inc C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments/apachesolr_attachments.admin.inc
--- C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments.orig/apachesolr_attachments.admin.inc	2010-10-11 11:10:20.000000000 +1300
+++ C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments/apachesolr_attachments.admin.inc	2010-10-11 11:02:22.312500000 +1300
@@ -191,7 +191,8 @@
       t('All file attachments will be re-indexed.');
       break;
     case 'clear-cache':
-      db_query("DELETE FROM {apachesolr_attachments_files} WHERE removed = 0");
+      db_query("DELETE FROM {apachesolr_attachments_nodes} WHERE removed = 0");
+      db_query("DELETE FROM {apachesolr_attachments_files}");
       drupal_set_message(t('The local cache of extracted text has been deleted.'));
       break;
   }
@@ -233,7 +234,7 @@
     // fids that were added before but no longer present on this node.
 
     $fids = array();
-    $result = db_query("SELECT fid FROM {apachesolr_attachments_files} WHERE nid = %d", $node->nid);
+    $result = db_query("SELECT fid FROM {apachesolr_attachments_nodes} WHERE nid = %d", $node->nid);
     while ($row = db_fetch_array($result)) {
       $fids[$row['fid']] = $row['fid'];
     }
@@ -243,12 +244,19 @@
     // Find deleted files.
     $missing_fids = array_diff_key($fids, $files);
     if ($missing_fids) {
-      db_query("UPDATE {apachesolr_attachments_files} SET removed = 1 WHERE fid IN (". db_placeholders($missing_fids) .")", $missing_fids);
+      $db_query_array = $missing_fids;
+      array_unshift($db_query_array, $nid);
+      db_query("UPDATE {apachesolr_attachments_nodes} SET removed = 1 WHERE nid = %d AND fid IN (". db_placeholders($missing_fids) .")", $db_query_array);
     }
     $new_files = array_diff_key($files, $fids);
     // Add new files.
     foreach ($new_files as $file) {
-      db_query("INSERT INTO {apachesolr_attachments_files} (fid, nid, removed, sha1) VALUES (%d, %d, 0, '')", $file->fid, $node->nid);
+      if (!db_result(db_query("SELECT 1 FROM {apachesolr_attachments_files} WHERE fid = %d", $file->fid))) {
+        db_query("INSERT INTO {apachesolr_attachments_files} (fid, sha1) VALUES (%d, '')", $file->fid);
+      }
+      if (!db_result(db_query("SELECT 1 FROM {apachesolr_attachments_nodes} WHERE fid = %d AND nid = %d", $file->fid, $node->nid))) {
+        db_query("INSERT INTO {apachesolr_attachments_nodes} (fid, nid, removed) VALUES (%d, %d, 0)", $file->fid, $node->nid);
+      }
     }
     foreach ($files as $file) {
       $text = apachesolr_attachments_get_attachment_text($file);
diff -u -r -N C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments.orig/apachesolr_attachments.install C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments/apachesolr_attachments.install
--- C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments.orig/apachesolr_attachments.install	2010-10-11 11:10:06.000000000 +1300
+++ C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments/apachesolr_attachments.install	2010-10-11 10:59:45.218750000 +1300
@@ -63,17 +63,6 @@
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE),
-      'nid' => array(
-        'description' => 'The {node}.nid where the file is attached.',
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0),
-      'removed' => array(
-        'description' => 'file is no longer attached.',
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'default' => 0),
       'sha1' => array(
         'description' => 'file sha1 to check for changes.',
         'type' => 'varchar',
@@ -86,12 +75,35 @@
         'not null' => FALSE,
         'size' => 'big'),
      ),
+    'primary key' => array('fid'),
+  );
+
+  $schema['apachesolr_attachments_nodes'] = array(
+    'description' => 'Stores information about files node attachment.',
+    'fields' => array(
+      'fid' => array(
+        'description' => 'Foreign Key: {files}.fid',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE),
+      'nid' => array(
+        'description' => 'The {node}.nid where the file is attached.',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0),
+      'removed' => array(
+        'description' => 'file is no longer attached.',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'default' => 0),
+     ),
     'indexes' => array(
+      'fid' => array('fid'),
       'nid' => array('nid'),
       'removed' => array('removed'),
       ),
-    'primary key' => array('fid'),
-    );
+  );
 
   return $schema;
 }
@@ -144,5 +156,41 @@
     'size' => 'big')
   );
 
+  $apachesolr_attachments_nodes_schema =  array(
+    'description' => 'Stores information about files node attachment.',
+    'fields' => array(
+      'fid' => array(
+        'description' => 'Foreign Key: {files}.fid',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE),
+      'nid' => array(
+        'description' => 'The {node}.nid where the file is attached.',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0),
+      'removed' => array(
+        'description' => 'file is no longer attached.',
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'default' => 0),
+     ),
+    'indexes' => array(
+      'fid' => array('fid'),
+      'nid' => array('nid'),
+      'removed' => array('removed'),
+      ),
+  );
+  
+  db_create_table($ret, 'apachesolr_attachments_nodes', $apachesolr_attachments_nodes_schema);
+  
+  $ret[] = update_sql("INSERT INTO {apachesolr_attachments_nodes} (fid, nid, removed) SELECT fid, nid, removed FROM {apachesolr_attachments_files}");
+  
+  db_drop_index($ret, 'apachesolr_attachments_files', 'nid');
+  db_drop_index($ret, 'apachesolr_attachments_files', 'removed');
+  db_drop_field($ret, 'apachesolr_attachments_files', 'nid');
+  db_drop_field($ret, 'apachesolr_attachments_files', 'removed');
+  
   return $ret;
 }
\ No newline at end of file
diff -u -r -N C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments.orig/apachesolr_attachments.module C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments/apachesolr_attachments.module
--- C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments.orig/apachesolr_attachments.module	2010-10-11 11:10:20.000000000 +1300
+++ C:\wamp\www\drupal6c\sites\all\modules\apachesolr_attachments/apachesolr_attachments.module	2010-10-11 10:59:45.218750000 +1300
@@ -136,7 +136,7 @@
     case 'delete':
       apachesolr_attachments_remove_attachments_from_index($node->nid);
       // Mark attachments for later re-deletion in case the query fails.
-      db_query("UPDATE {apachesolr_attachments_files} SET removed = 1 WHERE nid = %d", $node->nid);
+      db_query("UPDATE {apachesolr_attachments_nodes} SET removed = 1 WHERE nid = %d", $node->nid);
       break;
   }
 }
@@ -149,18 +149,25 @@
 function apachesolr_attachments_cron() {
   try {
     $solr = apachesolr_get_solr();
-    $result = db_query("SELECT fid, nid FROM {apachesolr_attachments_files} WHERE removed = 1");
+    $result = db_query("SELECT fid, nid FROM {apachesolr_attachments_nodes} WHERE removed = 1");
     $ids = array();
     $fids = array();
     while ($file = db_fetch_object($result)) {
       $ids[] = apachesolr_document_id($file->fid .'-'. $file->nid, 'file');
-      $fids[] = $file->fid;
+      $nfids[$file->nid][] = $file->fid;
     }
     if ($ids) {
       $solr->deleteByMultipleIds($ids);
       $solr->commit();
-      // There was no exception, so update the table.
-      db_query("DELETE FROM {apachesolr_attachments_files} WHERE fid IN (". db_placeholders($fids) .")", $fids);
+      // There was no exception, so update the tables.
+      foreach ($nfids as $nid => $fids) {
+        foreach ($fids as $fid) {
+          db_query("DELETE FROM {apachesolr_attachments_nodes} WHERE nid = %d AND fid = %d", $nid, $fid);
+          if (db_result(db_query("SELECT COUNT(*) FROM {apachesolr_attachments_nodes} WHERE fid = %d", $fid)) == 0) { // file is no longer attached to any node
+            db_query("DELETE FROM {apachesolr_attachments_files} WHERE fid = %d", $fid);
+          }
+        }
+      }
     }
   }
   catch (Exception $e) {
