diff --git a/myisam/ft_boolean_search.c b/myisam/ft_boolean_search.c
index f0865932ca01df801b3bd3afc0127c8439e644f0..8f7f8ab5d73b78753f7ae5aa13810764d97d8758 100644
--- a/myisam/ft_boolean_search.c
+++ b/myisam/ft_boolean_search.c
@@ -198,7 +198,7 @@ FT_DOCLIST *ft_boolean_search(MI_INFO *info, uint keynr, byte *query,
   aio.end=query+query_len;
   aio.total_yes=aio.total_no=0;
 
-  init_tree(&aio.dtree,0,sizeof(FT_SUPERDOC),(qsort_cmp)&FT_SUPERDOC_cmp,0,
+  init_tree(&aio.dtree,0,sizeof(FT_SUPERDOC),(qsort_cmp2)&FT_SUPERDOC_cmp,0,
             NULL);
 
   if (do_boolean(&aio,0,0,0,0))
diff --git a/myisam/mi_extra.c b/myisam/mi_extra.c
index cf075512ac4cdb10ac81205ee0633cf11c4d4710..e1e4fe75a0777fbe8642ed066613cfc3487682f6 100644
--- a/myisam/mi_extra.c
+++ b/myisam/mi_extra.c
@@ -219,9 +219,17 @@ int mi_extra(MI_INFO *info, enum ha_extra_function function)
     }
     if (share->state.key_map)
     {
-      share->state.key_map=0;
-      info->state->key_file_length=share->state.state.key_file_length=
-	share->base.keystart;
+      MI_KEYDEF *key=share->keyinfo;
+      uint i;
+      for (i=0 ; i < share->base.keys ; i++,key++)
+      {
+        if (!(key->flag & HA_NOSAME) && info->s->base.auto_key != i+1)
+        {
+          share->state.key_map&= ~ ((ulonglong) 1 << i);
+          info->update|= HA_STATE_CHANGED;
+        }
+      }
+
       if (!share->changed)
       {
 	share->state.changed|= STATE_CHANGED | STATE_NOT_ANALYZED;
diff --git a/myisam/myisamlog.c b/myisam/myisamlog.c
index ca2c4f27a743c3e6c752c40913345330710e3e9e..8ff9bb0dc1a59ea058c7f6e97b471a7aa4f4ad22 100644
--- a/myisam/myisamlog.c
+++ b/myisam/myisamlog.c
@@ -331,7 +331,7 @@ static int examine_log(my_string file_name, char **table_names)
 
   init_io_cache(&cache,file,0,READ_CACHE,start_offset,0,MYF(0));
   bzero((gptr) com_count,sizeof(com_count));
-  init_tree(&tree,0,sizeof(file_info),(qsort_cmp) file_info_compare,1,
+  init_tree(&tree,0,sizeof(file_info),(qsort_cmp2) file_info_compare,1,
 	    (void(*)(void*)) file_info_free);
   VOID(init_key_cache(KEY_CACHE_SIZE,(uint) (10*4*(IO_SIZE+MALLOC_OVERHEAD))));
 
diff --git a/sql/ha_myisam.cc b/sql/ha_myisam.cc
index 6409ec5d01918e537398605904859d3024439242..bec465a5c5c9bddcba6b3d41cefc60c00be28903 100644
--- a/sql/ha_myisam.cc
+++ b/sql/ha_myisam.cc
@@ -637,8 +637,18 @@ int ha_myisam::repair(THD *thd, MI_CHECK &param, bool optimize)
 
 void ha_myisam::deactivate_non_unique_index(ha_rows rows)
 {
-  if (!(specialflag & SPECIAL_SAFE_MODE))
-    mi_disable_non_unique_index(file,rows);
+  MYISAM_SHARE* share = file->s;
+  if (share->state.key_map == ((ulonglong) 1L << share->base.keys)-1)
+  {
+    if (!(specialflag & SPECIAL_SAFE_MODE))
+      if (rows==HA_POS_ERROR)
+        mi_extra(file, HA_EXTRA_NO_KEYS);
+      else
+        mi_disable_non_unique_index(file,rows);
+    enable_activate_all_index=1;
+  }
+  else
+    enable_activate_all_index=0;
 }
 
 
@@ -648,7 +658,8 @@ bool ha_myisam::activate_all_index(THD *thd)
   MI_CHECK param;
   MYISAM_SHARE* share = file->s;
   DBUG_ENTER("activate_all_index");
-  if (share->state.key_map != ((ulonglong) 1L << share->base.keys)-1)
+  if (enable_activate_all_index &&
+      share->state.key_map != ((ulonglong) 1L << share->base.keys)-1)
   {
     const char *save_proc_info=thd->proc_info;
     thd->proc_info="Creating index";
@@ -663,6 +674,8 @@ bool ha_myisam::activate_all_index(THD *thd)
     error=repair(thd,param,0) != HA_ADMIN_OK;
     thd->proc_info=save_proc_info;
   }
+  else
+    enable_activate_all_index=1;
   DBUG_RETURN(error);
 }
 
diff --git a/sql/ha_myisam.h b/sql/ha_myisam.h
index 6451e2b80ee5c7a2a1f3980f018cf95e830646b0..21b7e5bbd39e33ef606d33c0a5e95576cab91644 100644
--- a/sql/ha_myisam.h
+++ b/sql/ha_myisam.h
@@ -37,11 +37,11 @@ extern ulong myisam_recover_options;
 class ha_myisam: public handler
 {
   MI_INFO *file;
-  uint    int_option_flag;
+  uint    int_option_flag,enable_activate_all_index;
   int repair(THD *thd, MI_CHECK &param, bool optimize);
 
  public:
-  ha_myisam(TABLE *table): handler(table), file(0),
+  ha_myisam(TABLE *table): handler(table), file(0),enable_activate_all_index(1),
     int_option_flag(HA_READ_NEXT | HA_READ_PREV | HA_READ_RND_SAME |
 		    HA_KEYPOS_TO_RNDPOS | HA_READ_ORDER |  HA_LASTKEY_ORDER |
 		    HA_HAVE_KEY_READ_ONLY | HA_READ_NOT_EXACT_KEY |
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index 89699655ab931254ecf15ce58404f7aadddf983d..95f5d4da24d306818aaa0dd0cb54a5634691df24 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -1189,9 +1189,11 @@ int mysql_alter_table(THD *thd,char *new_db, char *new_name,
       switch (keys_onoff)
       {
         case LEAVE_AS_IS: break;
-        case ENABLE: error=table->file->activate_all_index(thd); break;
+        case ENABLE:
+          error=table->file->activate_all_index(thd);
+          break;
         case DISABLE:
-          table->file->deactivate_non_unique_index(table->file->records);
+          table->file->deactivate_non_unique_index(HA_POS_ERROR);
           break;
       }
     }