Index: /trunk/Nebulous-Server/Changes
===================================================================
--- /trunk/Nebulous-Server/Changes	(revision 24499)
+++ /trunk/Nebulous-Server/Changes	(revision 24500)
@@ -23,4 +23,5 @@
       the semantics
     - add nebdiskd mountpoint test retyring
+    - rework delete_instance() to avoid requiring an index on instance.uri
       
 0.16
Index: /trunk/Nebulous-Server/lib/Nebulous/Server.pm
===================================================================
--- /trunk/Nebulous-Server/lib/Nebulous/Server.pm	(revision 24499)
+++ /trunk/Nebulous-Server/lib/Nebulous/Server.pm	(revision 24500)
@@ -1581,26 +1581,32 @@
 TRANS: while (1) {
         eval {
+            my $instances;
             my $so_id;
-            my $instances;
-            # get so_id
+            my $ins_id;
+            # find so_id for key and get count of instances
             {
-                my $query = $db->prepare_cached( $sql->get_object_from_uri );
-                my $rows = $query->execute( $uri );
-
+                my $query = $db->prepare_cached( $sql->get_instance_count_by_ext_id );
+                my $rows = $query->execute($key->path);
                 unless ( $rows > 0 ) {
                     $query->finish;
+                    die( "$key has no associated instances - this should not happen" );
+                }
+
+                my $record = $query->fetchrow_hashref;
+                $so_id      = $record->{ 'so_id' };
+                $instances  = $record->{ 'count(ins_id)' };
+                $query->finish;
+            }
+
+            # find ins_id for uri
+            {
+                my $query = $db->prepare_cached( $sql->get_instance_by_uri );
+                my $rows = $query->execute($so_id, $uri);
+                unless ( $rows > 0) {
+                    $query->finish;
                     die( "no instance is associated with uri" );
                 }
 
-                $so_id = $query->fetchrow_hashref->{ 'so_id' };
-                $query->finish;
-
-            }
-
-            {
-                my $query = $db->prepare_cached( $sql->get_instance_count );
-                $query->execute( $so_id );
-
-                $instances = $query->fetchrow_hashref->{ 'count(ins_id)' };
+                $ins_id = $query->fetchrow_hashref->{ 'ins_id' };
                 $query->finish;
             }
@@ -1608,10 +1614,10 @@
             # remove instance
             {
-                my $query = $db->prepare_cached( $sql->delete_instance );
-                my $rows = $query->execute( $uri );
+                my $query = $db->prepare_cached( $sql->delete_instance_by_ins_id );
+                my $rows = $query->execute( $ins_id );
                 $query->finish;
                 
-                # if we affected something other then two rows something very bad
-                # has happened
+                # if we affected something other then one row something very
+                # bad has happened
                 unless ( $rows == 1 ) {
                     die( "affected row count is $rows instead of 1" );
Index: /trunk/Nebulous-Server/lib/Nebulous/Server/SQL.pm
===================================================================
--- /trunk/Nebulous-Server/lib/Nebulous/Server/SQL.pm	(revision 24499)
+++ /trunk/Nebulous-Server/lib/Nebulous/Server/SQL.pm	(revision 24500)
@@ -175,7 +175,7 @@
         WHERE ext_id = ?
     },
-    delete_instance     => qq{
+    delete_instance_by_ins_id => qq{
         DELETE FROM instance
-        WHERE uri = ?
+        WHERE ins_id = ?
     },
     get_object_from_uri   => qq{
@@ -184,8 +184,24 @@
         WHERE uri = ?
     },
+    get_instance_by_uri   => qq{
+        SELECT ins_id
+        FROM instance
+        WHERE
+            so_id = ?
+            AND uri = ?
+    },
     get_instance_count   => qq{
         SELECT count(ins_id)
         FROM instance
         WHERE so_id = ?
+    },
+    get_instance_count_by_ext_id   => qq{
+        SELECT count(ins_id), so_id
+        FROM instance
+        JOIN storage_object
+            USING(so_id)
+        WHERE
+            ext_id = ?
+        GROUP BY so_id
     },
     get_object_instances    => qq{
