How does MySQL handle DROP TABLE

Summary

MySQL's DROP TABLE command executes a multi-stage process that can cause other DDL and DML operations to hang. The server first removes the table from internal caches and handler hash tables before delegating to the storage engine for file deletion. For InnoDB, this involves acquiring a dictionary mutex to serialize operations, updating dictionary tables, and removing the table from the dictionary cache. This mutex protects critical data dictionary updates and the deletion of tablespace files, explaining the observed blocking behavior. After the engine-specific deletion, the server clears the query cache and writes the operation to the binary log.

A few days ago, when executing DROP TABLE in MySQL, all processes including DDL and DML were hung until DROP TABLE was completed. I am confused about this phenomenon.

I have reviewed the source codes of MySQL to check how MySQL internally handle DROP TABLE.

When user trigger DROP TABLE command:

########################MySQL SERVER drop table########################

/* Sql_table.cc  delete (drop) tables. */
bool mysql_rm_table( )

  /*   Execute the drop of a normal or temporary table */
  int mysql_rm_table_part2 ()
 
   /* Remove matching tables from the HANDLER’s hash table.  */
   void mysql_ha_rm_tables () 

    /* Mark all entries with the table as deleted to force an reopen of the table */
    remove_table_from_cache ()
   
   /* remove engine files */  LINE 2024
       int handler::ha_delete_table(const char *name) 
          file->ha_delete_table(path)   line 1991
             int handler::delete_table(const char *name) //Delete data table
                 /* Here INNODB inherits parent class handler ,
                 it actually calls int ha_innobase::delete_table
                 go to next   #### INNODB handle drop table #### 
                 if it's other engine, directly delete data file, continue*/                
                int mysys::my_delete_with_symlink(const char *name, myf MyFlags)
                  int mysys::my_delete(const char *name, myf MyFlags)     
                 
   /* Delete the table definition file */  LINE 2042 
    int mysys::my_delete()

  /* clear query cache*/ 
      query_cache_invalidate3

    /* writes to BINLOG */
############# INNODB handle drop table ##################    
/* Drops a table from an InnoDB database.  */
int ha_innobase::delete_table( const char* name) ; // Drop table with INNODB
   int row_drop_table_for_mysql()
      /* Serialize data dictionary operations with dictionary mutex:
        row_mysql_lock_data_dictionary(trx);
            
       /* Update dictionary table (MEMDB,information_schema), line 3178 */
        que_eval_sql(info, “PROCEDURE DROP_TABLE_PROC () IS\n”
        /*Here using a PROCEDURE*/
          
        /* update dictionary table(CACHE) */
        void dict_table_remove_from_cache(dict_table_t* table))
          
          /* Delete data file
          /* Deletes a single-table tablespace */
          ibool fil_delete_tablespace()
      
            /* Frees a space object from the tablespace memory cache*/
            ibool fil_space_free ()
      
            /* Deletes a file. The file has to be closed before calling this. */
            ibool os_file_delete ()
              unlink((const char*)name);     
        
     /* release the lock */
        row_mysql_unlock_data_dictionary(trx);


Source : http://www.mysqlops.com/2011/02/18/mysql-drop-table-%e5%a4%84%e7%90%86%e8%bf%87%e7%a8%8b.html

MYSQL DROP TABLE

  RELATED

  COMMENT

1
Avanir
Nov 8, 2012 at 2:23 pm
I was drawn by the hoentsy of what you write