{"id":588,"date":"2014-04-09T19:56:09","date_gmt":"2014-04-09T11:56:09","guid":{"rendered":"http:\/\/www.gpfeng.com\/?p=588"},"modified":"2014-04-30T20:09:53","modified_gmt":"2014-04-30T12:09:53","slug":"mysql-metadata-lock%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"http:\/\/www.gpfeng.com\/?p=588","title":{"rendered":"MySQL metadata lock\u6e90\u7801\u5206\u6790"},"content":{"rendered":"<p>\u82b1\u4e86\u4e9b\u65f6\u95f4\u7406\u4e86\u4e0bMySQL Meatadata Lock(\u5143\u6570\u636e\u9501\uff0c\u6709\u4eba\u5c06\u5176\u7ffb\u8bd1\u4e3a\u6570\u636e\u5b57\u5178\u9501)\u7684\u5b9e\u73b0\uff0c\u6587\u7ae0\u5185\u5bb9\u6bd4\u8f83\u504f\u6e90\u7801\u5c42\u9762\uff0c\u5728\u5982\u4f55\u4f7f\u7528\u65b9\u9762\u6709\u4e9b\u6b20\u7f3a\uff0c\u9700\u8981\u5bf9\u6570\u636e\u5e93\u7cfb\u7edf\u539f\u7406\u6216\u8005MySQL\u5b9e\u73b0\u6709\u4e00\u5b9a\u57fa\u7840\u624d\u4e0d\u4f1a\u89c9\u5f97\u67af\u71e5\uff0c\u5982\u679c\u8fdeS\/X\/IS\/IX\u53ca\u4ed6\u4eec\u4e4b\u95f4\u7684\u5173\u7cfb\u90fd\u4e0d\u6e05\u695a\uff0c\u5efa\u8bae\u5148\u5b66\u4e60\u8fd9\u90e8\u5206\u77e5\u8bc6\uff01<\/p>\n<p><strong>\u6e90\u7801\u7248\u672c\uff1a<\/strong><br \/>\nOracle MySQL community server 5.6.16<\/p>\n<h3>\u5173\u952e\u6570\u636e\u7ed3\u6784\uff1a<\/h3>\n<p><strong>MDL_map<\/strong><br \/>\n\u7ba1\u7406\u7cfb\u7edf\u4e2d\u6240\u6709\u7684mdl(MDL_lock)\uff0c\u4e3a\u4e86\u51cf\u5c0f\u9501\u7ade\u4e89\uff0c\u88ab\u5212\u5206\u4e3amdl_locks_hash_partitions(\u9ed8\u8ba4\u4e3a8)\u90e8\u5206\uff0c\u8be5\u7c7b\u5168\u5c40\u53ea\u6709\u4e00\u4e2a\u5bf9\u8c61\uff1amdl_locks<\/p>\n<p><strong>MDL_map_partition<\/strong><br \/>\nMDL_map\u4e2d\u7684\u4e00\u4e2apartition\uff0c\u4f7f\u7528hash\u5b58\u50a8\u90e8\u5206mdl(MDL_lock)<br \/>\n<!--more--><br \/>\n<strong>MDL_context<\/strong><br \/>\n\u7ba1\u7406\u4e00\u4e2asession\u4e2d\u7684\u6240\u6709mdl(MDL_ticket)\uff0c\u5305\u62ec\u4e0d\u540cduration\u7c7b\u578b(MDL_STATEMENT\/MDL_TRANSACTION\/MDL_EXPLICIT)\u7684\u5df2\u83b7\u5f97\u7684mdl(MDL_ticket)\u94fe\u8868\u548c\u5f53\u524d\u7b49\u5f85\u7684mdl(MDL_wait_for_subgraph)\uff0c\u662fsession\u52a0\/\u89e3\u9501\u8bf7\u6c42\u7684\u5165\u53e3\uff0c\u4e3b\u8981\u63a5\u53e3\u51fd\u6570\uff1a<strong>acquire_lock\/release_lock<\/strong><\/p>\n<p><strong>MDL_key<\/strong><br \/>\nmdl\u7684key\u90e8\u5206\uff0c\u4ee3\u8868\u4e00\u4e2a\u5bf9\u8c61\u6216\u8005\u4e00\u4e2a\u8303\u56f4\uff0c\u5305\u542bmdl_namespace\uff0cdb_name\uff0cobject_name<\/p>\n<pre lang=\"c\">\r\nenum enum_mdl_namespace { GLOBAL=0,\r\n                          SCHEMA,\r\n                          TABLE,\r\n                          FUNCTION,\r\n                          PROCEDURE,\r\n                          TRIGGER,\r\n                          EVENT,\r\n                          COMMIT,\r\n                          \/* This should be the last ! *\/\r\n                          NAMESPACE_END };\r\n<\/pre>\n<p>\u5bf9\u4e8eGLOBAL\/COMMIT\u4e24\u79cdmdl\uff0cdb_name\u548cobject_name\u4e3a\u7a7a<\/p>\n<p><strong>MDL_request<\/strong><br \/>\n\u4e00\u4e2amdl\u8bf7\u6c42\uff0c\u5305\u62ecmdl_type\uff0cmdl_duration\uff0ckey(MDL_key\u7c7b\u578b)\uff0c\u4ee5\u53camdl\u8bf7\u6c42\u6210\u529f\u540e\u7684MDL_ticket<\/p>\n<p><strong>mdl_type\uff1a<\/strong><\/p>\n<pre lang=\"c\">\r\nenum enum_mdl_type {\r\n  \/*\r\n    An intention exclusive metadata lock. Used only for scoped locks.\r\n    Owner of this type of lock can acquire upgradable exclusive locks on\r\n    individual objects.\r\n    Compatible with other IX locks, but is incompatible with scoped S and\r\n    X locks. *\/\r\n  MDL_INTENTION_EXCLUSIVE= 0,\r\n\r\n  \/*\r\n    A shared metadata lock.\r\n    To be used in cases when we are interested in object metadata only\r\n    and there is no intention to access object data (e.g. for stored\r\n    routines or during preparing prepared statements).\r\n    We also mis-use this type of lock for open HANDLERs, since lock\r\n    acquired by this statement has to be compatible with lock acquired\r\n    by LOCK TABLES ... WRITE statement, i.e. SNRW (We can't get by by\r\n    acquiring S lock at HANDLER ... OPEN time and upgrading it to SR\r\n    lock for HANDLER ... READ as it doesn't solve problem with need\r\n    to abort DML statements which wait on table level lock while having\r\n    open HANDLER in the same connection).\r\n    To avoid deadlock which may occur when SNRW lock is being upgraded to\r\n    X lock for table on which there is an active S lock which is owned by\r\n    thread which waits in its turn for table-level lock owned by thread\r\n    performing upgrade we have to use thr_abort_locks_for_thread()\r\n    facility in such situation.\r\n    This problem does not arise for locks on stored routines as we don't\r\n    use SNRW locks for them. It also does not arise when S locks are used\r\n    during PREPARE calls as table-level locks are not acquired in this\r\n    case. *\/\r\n  MDL_SHARED,\r\n\r\n  \/*\r\n    A high priority shared metadata lock.\r\n    Used for cases when there is no intention to access object data (i.e.\r\n    data in the table).\r\n    \"High priority\" means that, unlike other shared locks, it is granted\r\n    ignoring pending requests for exclusive locks. Intended for use in\r\n    cases when we only need to access metadata and not data, e.g. when\r\n    filling an INFORMATION_SCHEMA table.\r\n    Since SH lock is compatible with SNRW lock, the connection that\r\n    holds SH lock lock should not try to acquire any kind of table-level\r\n    or row-level lock, as this can lead to a deadlock. Moreover, after\r\n    acquiring SH lock, the connection should not wait for any other\r\n    resource, as it might cause starvation for X locks and a potential\r\n    deadlock during upgrade of SNW or SNRW to X lock (e.g. if the\r\n    upgrading connection holds the resource that is being waited for).\r\n  *\/\r\n  MDL_SHARED_HIGH_PRIO,\r\n\r\n  \/*\r\n    A shared metadata lock for cases when there is an intention to read data\r\n    from table.\r\n    A connection holding this kind of lock can read table metadata and read\r\n    table data (after acquiring appropriate table and row-level locks).\r\n    This means that one can only acquire TL_READ, TL_READ_NO_INSERT, and\r\n    similar table-level locks on table if one holds SR MDL lock on it.\r\n    To be used for tables in SELECTs, subqueries, and LOCK TABLE ...  READ\r\n    statements. *\/\r\n  MDL_SHARED_READ,\r\n\r\n  \/*\r\n    A shared metadata lock for cases when there is an intention to modify\r\n    (and not just read) data in the table.\r\n    A connection holding SW lock can read table metadata and modify or read\r\n    table data (after acquiring appropriate table and row-level locks).\r\n    To be used for tables to be modified by INSERT, UPDATE, DELETE\r\n    statements, but not LOCK TABLE ... WRITE or DDL). Also taken by\r\n    SELECT ... FOR UPDATE. *\/\r\n  MDL_SHARED_WRITE,\r\n\r\n  \/*\r\n    An upgradable shared metadata lock for cases when there is an intention\r\n    to modify (and not just read) data in the table.\r\n    Can be upgraded to MDL_SHARED_NO_WRITE and MDL_EXCLUSIVE.\r\n    A connection holding SU lock can read table metadata and modify or read\r\n    table data (after acquiring appropriate table and row-level locks).\r\n    To be used for the first phase of ALTER TABLE. *\/\r\n  MDL_SHARED_UPGRADABLE,\r\n\r\n  \/*\r\n    An upgradable shared metadata lock which blocks all attempts to update\r\n    table data, allowing reads.\r\n    A connection holding this kind of lock can read table metadata and read\r\n    table data.\r\n    Can be upgraded to X metadata lock.\r\n    Note, that since this type of lock is not compatible with SNRW or SW\r\n    lock types, acquiring appropriate engine-level locks for reading\r\n    (TL_READ* for MyISAM, shared row locks in InnoDB) should be\r\n    contention-free.\r\n    To be used for the first phase of ALTER TABLE, when copying data between\r\n    tables, to allow concurrent SELECTs from the table, but not UPDATEs. *\/\r\n  MDL_SHARED_NO_WRITE,\r\n\r\n  \/*\r\n    An upgradable shared metadata lock which allows other connections\r\n    to access table metadata, but not data.\r\n    It blocks all attempts to read or update table data, while allowing\r\n    INFORMATION_SCHEMA and SHOW queries.\r\n    A connection holding this kind of lock can read table metadata modify and\r\n    read table data.\r\n    Can be upgraded to X metadata lock.\r\n    To be used for LOCK TABLES WRITE statement.\r\n    Not compatible with any other lock type except S and SH. *\/\r\n  MDL_SHARED_NO_READ_WRITE,\r\n\r\n  \/*\r\n    An exclusive metadata lock.\r\n    A connection holding this lock can modify both table's metadata and data.\r\n    No other type of metadata lock can be granted while this lock is held.\r\n    To be used for CREATE\/DROP\/RENAME TABLE statements and for execution of\r\n    certain phases of other DDL statements. *\/\r\n  MDL_EXCLUSIVE,\r\n\r\n  \/* This should be the last !!! *\/\r\n  MDL_TYPE_END};\r\n<\/pre>\n<p>\u4e0d\u540cmdl_type\u4e4b\u95f4\u517c\u5bb9\u77e9\u9635\u5c06\u5728\u540e\u9762MDL_scoped_lock\/MDL_object_lock\u4e2d\u4ea4\u4ee3<\/p>\n<p><strong>mdl_duration\uff1a<\/strong><\/p>\n<pre lang=\"c\">\r\nenum enum_mdl_duration {\r\n  \/**\r\n    Locks with statement duration are automatically released at the end\r\n    of statement or transaction. *\/\r\n  MDL_STATEMENT= 0,\r\n\r\n  \/**\r\n    Locks with transaction duration are automatically released at the end\r\n    of transaction. *\/\r\n  MDL_TRANSACTION,\r\n\r\n  \/**\r\n    Locks with explicit duration survive the end of statement and transaction.\r\n    They have to be released explicitly by calling MDL_context::release_lock(). *\/\r\n  MDL_EXPLICIT,\r\n\r\n  \/* This should be the last ! *\/\r\n  MDL_DURATION_END };\r\n<\/pre>\n<p><strong>MDL_ticket<\/strong><br \/>\n\u5355\u4e2agranted\/waiting mdl\uff0c\u6700\u5c0f\u52a0\u9501\u5355\u4f4d\uff0c\u4fdd\u5b58\u4e86mdl_type\u53ca\u5176\u4ece\u5c5e\u7684MDL_context\u53caMDL_lock\uff0c\u901a\u8fc7\u5176\u4ece\u5c5e\u7684MDL_lock\uff0c\u53ef\u4ee5\u83b7\u5f97\u5bf9\u5e94\u7684mdl_namespace\uff0cMDL_ticket\u7ee7\u627fMDL_wait_for_subgraph\u7c7b\uff0c\u5728\u6b7b\u9501\u68c0\u6d4b\u65f6\u88ab\u7528\u5230<\/p>\n<p><strong>MDL_lock<\/strong><br \/>\n\u7cfb\u7edf\u4e2d\u4e00\u4e2aMDL_key\u4e0a\u7684\u6240\u6709mdl\u52a0\u9501\u4fe1\u606f\uff0c\u4e3b\u8981\u5305\u542b\u5bf9\u5e94\u7684key\u53ca\u4e24\u4e2alist: granted\/waiting list(MDL_ticket)<\/p>\n<p>\u6839\u636emdl_namespace\u4e0d\u540c\uff0cMDL_lock\u6709\u4e24\u4e2a\u6d3e\u751f\u7c7b\uff1a<br \/>\nGLOBAL\/SCHEMA\/COMMIT: MDL_scoped_lock<br \/>\nTABLE\/FUNCTION\/PROCEDUER\/TRIGGER\/EVENT: MDL_object_lock<\/p>\n<p><strong>MDL_scoped_lock<\/strong><br \/>\n\u8303\u56f4\u9501\uff0cmdl_namespace\u4e3aGLOBAL\/SCHEMA\/COMMIT<br \/>\nrequest\u4e0egranted\u517c\u5bb9\u77e9\u9635\uff1a<\/p>\n<pre lang=\"c\">\r\n             | Type of active   |\r\n     Request |   scoped lock    |\r\n      type   | IS     IX   S  X |\r\n    ---------+------------------+\r\n    IS       |  +      +   +  + |\r\n    IX       |  +      +   -  - |\r\n    S        |  +      -   +  - |\r\n    X        |  +      -   -  - |\r\n<\/pre>\n<p>request\u4e0ewaiting\u517c\u5bb9\u77e9\u9635\uff1a<\/p>\n<pre lang=\"c\">\r\n             |    Pending      |\r\n     Request |  scoped lock    |\r\n      type   | IS     IX  S  X |\r\n    ---------+-----------------+\r\n    IS       |  +      +  +  + |\r\n    IX       |  +      +  -  - |\r\n    S        |  +      +  +  - |\r\n    X        |  +      +  +  + |\r\n<\/pre>\n<p><strong>MDL_object_lock<\/strong><br \/>\n\u5bf9\u8c61\u9501\uff0cmdl_namespace\u4e3aTABLE\/FUNCTION\/PROCEDUER\/TRIGGER\/EVENT<br \/>\nrequest\u4e0egranted\u517c\u5bb9\u77e9\u9635\uff1a(0\u4e3a\u4e0d\u53ef\u80fd\u60c5\u51b5)<\/p>\n<pre lang=\"c\">\r\n     Request  |  Granted requests for lock       |\r\n      type    | S  SH  SR  SW  SU  SNW  SNRW  X  |\r\n    ----------+----------------------------------+\r\n    S         | +   +   +   +   +   +    +    -  |\r\n    SH        | +   +   +   +   +   +    +    -  |\r\n    SR        | +   +   +   +   +   +    -    -  |\r\n    SW        | +   +   +   +   +   -    -    -  |\r\n    SU        | +   +   +   +   -   -    -    -  |\r\n    SNW       | +   +   +   -   -   -    -    -  |\r\n    SNRW      | +   +   -   -   -   -    -    -  |\r\n    X         | -   -   -   -   -   -    -    -  |\r\n    SU -> X   | -   -   -   -   0   0    0    0  |\r\n    SNW -> X  | -   -   -   0   0   0    0    0  |\r\n    SNRW -> X | -   -   0   0   0   0    0    0  |\r\n<\/pre>\n<p>request\u4e0ewaiting\u517c\u5bb9\u77e9\u9635\uff1a<\/p>\n<pre lang=\"c\">\r\n     Request  |  Pending requests for lock      |\r\n      type    | S  SH  SR  SW  SU  SNW  SNRW  X |\r\n    ----------+---------------------------------+\r\n    S         | +   +   +   +   +   +     +   - |\r\n    SH        | +   +   +   +   +   +     +   + |\r\n    SR        | +   +   +   +   +   +     -   - |\r\n    SW        | +   +   +   +   +   -     -   - |\r\n    SU        | +   +   +   +   +   +     +   - |\r\n    SNW       | +   +   +   +   +   +     +   - |\r\n    SNRW      | +   +   +   +   +   +     +   - |\r\n    X         | +   +   +   +   +   +     +   + |\r\n    SU -> X   | +   +   +   +   +   +     +   + |\r\n    SNW -> X  | +   +   +   +   +   +     +   + |\r\n    SNRW -> X | +   +   +   +   +   +     +   + |\r\n<\/pre>\n<p><strong>MDL_wait<\/strong><br \/>\n\u7533\u8bf7mdl(MDL_request&#8211;>MDL_ticket)\u7531\u4e8e\u51b2\u7a81\u4e0d\u80fd\u6ee1\u8db3\u65f6\uff0csession\u8fdb\u5165\u7b49\u5f85\u72b6\u6001\uff0c\u5728\u5176\u4ed6session\u91ca\u653e\u76f8\u5e94\u7684mdl\u65f6\u88ab\u5524\u9192\uff0cMDL_wait\u53ea\u662f\u5c01\u88c5\u4e86\u7528\u4e8econd-wait\/signal\u7684lock\u548ccondition\u53d8\u91cf\u53ca\u4e00\u4e2astatus<\/p>\n<h3>\u6b7b\u9501\u68c0\u6d4b<\/h3>\n<p>Deadlock_detection_visitor\u7ee7\u627f\u81eaMDL_wait_for_graph_visitor\uff0c\u88ab\u7528\u4e8ewait-for-graph\u6b7b\u9501\u68c0\u6d4b\uff0c\u5f53\u4eceMDL_context\u4e2d\u7533\u8bf7mdl\u5931\u8d25\u65f6\uff0cMDL_context\u4f1a\u7b49\u5f85\u4e00\u4e2aMDL_ticket\uff0c\u901a\u8fc7MDL_ticket\u53ef\u4ee5\u627e\u5230\u5176\u5bf9\u5e94MDL_lock\u7684granted\/waiting list\uff0clist\u4e2d\u4fdd\u5b58MDL_ticket\u5bf9\u8c61\uff0c\u53ef\u4ee5\u627e\u5230\u5176\u4ece\u5c5e\u7684MDL_context\uff0c\u56e0\u6b64\u53ef\u4ee5\u627e\u5230\u4e00\u4e2await-for-graph\u7684\u5b50\u56fe\uff0c\u6cbf\u7740\u8d77\u59cb\u7684MDL_context\u6839\u636e\u52a0\u9501\u51b2\u7a81\u4f9d\u8d56\u8fdb\u884c\u6df1\u5ea6\u641c\u7d22\uff0c\u5982\u679c\u627e\u5230\u4e86\u4e00\u6761\u73af\u8def\u6216\u8005\u641c\u7d22\u6df1\u5ea6\u8fbe\u5230MAX_SEARCH_DEPTH(32)\uff0c\u5c31\u8ba4\u4e3a\u53d1\u751f\u4e86\u6b7b\u9501\uff0cMySQL MDL\u6b7b\u9501\u68c0\u6d4b\u505a\u4e86\u4e00\u4e2a\u4f18\u5316\uff0c\u5728\u8fdb\u5165\u5230wait-for-graph\u4e2d\u7684\u4e00\u4e2a\u8282\u70b9\u505a\u6df1\u5ea6\u641c\u7d22\u4e4b\u524d\uff0c\u5148\u505a\u4e00\u6b21\u5e7f\u5ea6\u641c\u7d22<\/p>\n<h3>\u52a0\u9501\u51fd\u6570\u8c03\u7528\uff1a(select\u8bed\u53e5)<\/h3>\n<pre lang=\"c\">\r\nmysql_execute_command --> execute_sqlcom_select --> open_normal_and_derived_tables\r\n--> open_tables --> open_and_process_table --> open_table--> MDL_context::acquire_lock\r\n<\/pre>\n<p><strong>MDL_context::acquire_lock(MDL_request *mdl_request, ulong lock_wait_timeout)<\/strong><br \/>\n1. \u68c0\u67e5MDL_context\u4e4b\u524d\u662f\u5426\u5df2\u7ecf\u83b7\u5f97\u8fc7\u6ee1\u8db3\u8981\u6c42\u7684mdl(\u5b58\u5728\u5df2granted\u9501\u4e14\u7c7b\u578b\u5f3a\u4e8e\u7533\u8bf7\u9501\u7c7b\u578b)<br \/>\n2. \u5982\u679c\u6ca1\u6709\u627e\u5230\u6216\u8005\u7c7b\u578b\u4e0d\u6ee1\u8db3\uff0c\u6839\u636ehash\u7b97\u6cd5\u5728\u5bf9\u5e94MDL_map_partition\u4e2d\u627e\u5230MDL_lock(\u4e0d\u5b58\u5728\u5219\u63d2\u5165\uff0cMDL_map::find_or_insert)<br \/>\n3. \u6839\u636emdl_request\u751f\u6210ticket(MDL_ticket)\uff0c\u901a\u8fc7\u517c\u5bb9\u77e9\u9635\u8ba1\u7b97\u52a0\u9501\u80fd\u5426\u6ee1\u8db3\uff0c\u5982\u679c\u6ee1\u8db3\uff0c\u5c06ticket\u5176\u52a0\u5165\u5230MDL_lock\u4e2d\u7684granted list\u4e2d\uff0c\u51fd\u6570\u6210\u529f\u8fd4\u56de<br \/>\n3. \u52a0\u9501\u8bf7\u6c42\u65e0\u6cd5\u7acb\u5373\u6ee1\u8db3\uff0c\u5c06ticket\u52a0\u5165\u5230MDL_lock\u4e2d\u7684waiting list\uff0c\u5e76\u8bbe\u7f6eMDL_context\u5f53\u524d\u7b49\u5f85mdl(MDL_wait_for_subgraph)\u4e3aticket<br \/>\n4. \u8fdb\u884c\u6b7b\u9501\u68c0\u6d4b\uff0c\u5982\u679c\u53d1\u73b0\u6b7b\u9501\u5e76\u4e14\u4e3avictim\uff0c\u52a0\u9501\u5931\u8d25\uff0c\u51fd\u6570\u8fd4\u56de<br \/>\n5. \u7b49\u5f85\u5176\u5b83session\u8c03\u7528MDL_context::release_lock\u5524\u9192<br \/>\n6. \u9501\u7b49\u5f85\u88ab\u5524\u9192\/\u8d85\u65f6\u540e\uff0c\u68c0\u67e5\u72b6\u6001\uff0c\u5982\u679c\u4e0d\u4e3agranted\u5219\u62a5\u9519\uff0c\u5426\u5219\uff0c\u51fd\u6570\u52a0\u9501\u6210\u529f\u8fd4\u56de<\/p>\n<h3>\u89e3\u9501\u51fd\u6570\u8c03\u7528\uff1a(select\u8bed\u53e5)<\/h3>\n<pre lang=\"c\">\r\nmysql_execute_command --> MDL_context::release_transactional_locks \r\n--> MDL_context::release_locks_stored_before --> MDL_context::release_lock\r\n<\/pre>\n<p><strong>MDL_context::release_lock(enum_mdl_duration duration, MDL_ticket *ticket)<\/strong><br \/>\n1. \u901a\u8fc7ticket\u627e\u5230\u5bf9\u5e94\u7684MDL_lock\uff0c\u5c06ticket\u4eceMDL_lock\u4e2d\u7684granted list\u4e2d\u79fb\u9664<br \/>\n2. \u5224\u65ad\u79fb\u9664ticket\u540e\uff0c\u5982\u679cMDL_lock\u4e2dgranted\/waiting list\u540c\u65f6\u4e3a\u7a7a\uff0c\u5c06MDL_lock\u4ece\u5168\u5c40mdl_lock\u4e2d\u79fb\u9664\uff0c\u51fd\u6570\u8fd4\u56de<br \/>\n3. \u5982\u679c\u975e\u7a7a\uff0c\u904d\u5386MDL_lock\u4e2dwaiting list\uff0c\u901a\u8fc7\u517c\u5bb9\u77e9\u9635\u8ba1\u7b97\u52a0\u9501\u8bf7\u6c42\u662f\u5426\u53ef\u4ee5grant\uff0c\u5982\u679c\u53ef\u4ee5\uff0c\u5c06\u5176\u4ecewaiting list\u4e2d\u79fb\u9664\uff0c\u540c\u65f6\u52a0\u5165\u5230granted list\uff0c\u5e76\u5524\u9192\u5bf9\u5e94\u7684session<\/p>\n<h3>\u5173\u4e8eMDL_key::GLOBAL\u548cMDL_key::COMMIT<\/h3>\n<p>\u8fd9\u4e24\u79cdnamespace\u7684mdl\u4e3ascope lock\uff0c\u5176\u4e2dMDL_key::GLOBAL\u7c7b\u578bmdl\u5728dml\/ddl(mdl_type: MDL_INTENTION_EXCLUSIVE)\/set read_only\/flush tables with read lock(mdl_type: MDL_SHARED)\u65f6\u4f1a\u7533\u8bf7\uff0cMDL_key::COMMIT\u7c7b\u578bmdl\u5728\u4e8b\u52a1\u63d0\u4ea4(mdl_type: MDL_INTENTION_EXCLUSIVE)\u4ee5\u53caset read_only\/flush tables with read lock(mdl_type: MDL_SHARED)\u65f6\u7533\u8bf7\uff0c\u672c\u8d28\u4e0aset read_only\/flush tables with read lock\u505a\u7684\u4e8b\u60c5\u5dee\u4e0d\u591a\uff0c\u7531\u4e8e\u8fd9\u4e24\u79cdmdl\u7684\u5bf9\u5e94\u7684MDL_key\u662f\u5e38\u91cf\uff0c\u56e0\u6b64\u5728\u5b9e\u73b0\u65f6\u662f\u72ec\u7acb\u51fa\u6765\u7684(\u6ca1\u6709\u4fdd\u5b58\u5728MDL_map_partition\u4e2d)\uff1a<\/p>\n<pre lang=\"c\">\r\nclass MDL_map\r\n{\r\npublic:\r\n  void init();\r\n  void destroy();\r\n  MDL_lock *find_or_insert(const MDL_key *key);\r\n  void remove(MDL_lock *lock);\r\nprivate:\r\n  \/** Array of partitions where the locks are actually stored. *\/\r\n  Dynamic_array<MDL_map_partition *> m_partitions;\r\n  \/** Pre-allocated MDL_lock object for GLOBAL namespace. *\/\r\n  MDL_lock *m_global_lock;\r\n  \/** Pre-allocated MDL_lock object for COMMIT namespace. *\/\r\n  MDL_lock *m_commit_lock;\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u82b1\u4e86\u4e9b\u65f6\u95f4\u7406\u4e86\u4e0bMySQL Meatadata Lock(\u5143\u6570\u636e\u9501\uff0c\u6709\u4eba\u5c06\u5176\u7ffb\u8bd1\u4e3a\u6570\u636e\u5b57\u5178\u9501)\u7684\u5b9e\u73b0\uff0c\u6587\u7ae0\u5185\u5bb9 &hellip; <a href=\"http:\/\/www.gpfeng.com\/?p=588\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">MySQL metadata lock\u6e90\u7801\u5206\u6790<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3tPZp-9u","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/588"}],"collection":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=588"}],"version-history":[{"count":15,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/588\/revisions"}],"predecessor-version":[{"id":646,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/588\/revisions\/646"}],"wp:attachment":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=588"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}