根据文档:https://dev.mysql.com/doc/refman/5.7/en/innodb-locks-table.html,
INFORMATION_SCHEMA INNODB_LOCKS表的LOCK_TYPE应该具有2 可能的值:RECORD AND TABLE。
但是,我还没有看到该字段是“ TABLE”。无论我尝试了什么,它始终都是“ RECORD”。有没有人可以为我提供一个使“ TABLE”发生的理由?
谢谢, 叶贤一
答案 0 :(得分:0)
what Mysql version are you using?
Mysql8 INFORMATION SCHEMA does not have the table INNODB_LOCKS
mysql> show tables like '%innodb%';
+-----------------------------------------+
| Tables_in_information_schema (%INNODB%) |
+-----------------------------------------+
| INNODB_BUFFER_PAGE |
| INNODB_BUFFER_PAGE_LRU |
| INNODB_BUFFER_POOL_STATS |
| INNODB_CACHED_INDEXES |
| INNODB_CMP |
| INNODB_CMPMEM |
| INNODB_CMPMEM_RESET |
| INNODB_CMP_PER_INDEX |
| INNODB_CMP_PER_INDEX_RESET |
| INNODB_CMP_RESET |
| INNODB_COLUMNS |
| INNODB_DATAFILES |
| INNODB_FIELDS |
| INNODB_FOREIGN |
| INNODB_FOREIGN_COLS |
| INNODB_FT_BEING_DELETED |
| INNODB_FT_CONFIG |
| INNODB_FT_DEFAULT_STOPWORD |
| INNODB_FT_DELETED |
| INNODB_FT_INDEX_CACHE |
| INNODB_FT_INDEX_TABLE |
| INNODB_INDEXES |
| INNODB_METRICS |
| INNODB_TABLES |
| INNODB_TABLESPACES |
| INNODB_TABLESPACES_BRIEF |
| INNODB_TABLESTATS |
| INNODB_TEMP_TABLE_INFO |
| INNODB_TRX |
| INNODB_VIRTUAL |
+-----------------------------------------+
30 rows in set (0.01 sec)
答案 1 :(得分:0)
您链接到一些MySQL 5.7文档,因此我假设您使用的是5.7。
InnoDB中唯一的表锁是元数据锁,也称为 intent锁,在这里记录:https://dev.mysql.com/doc/refman/5.5/en/innodb-locking.html#innodb-intention-locks
任何要执行DML操作以读取或写入表中记录的事务(即SELECT / INSERT / UPDATE / DELETE)都需要首先获取元数据锁定。多个并发会话可以获取元数据锁来进行DML,并且它们不会相互冲突。
同样,元数据操作(DDL语句ALTER,DROP或TRUNCATE TABLE)需要在开始之前获取元数据锁定。因此,如果有任何事务在运行DML语句,则会阻止DDL语句,反之亦然。
这是在长时间运行的ALTER中阻止DML查询表的方式。
在MySQL 5.7中,您可以为元数据锁定设置一些PERFORMANCE_SCHEMA工具,但这是一项相当大的工作。参见https://www.percona.com/blog/2016/12/28/quickly-troubleshooting-metadata-locks-mysql-5-7/
在MySQL 8.0.1中,默认情况下有一个新的PERFORMANCE_SCHEMA.DATA_LOCKS来监视这些锁。这是一个示例:
mysql> select * from performance_schema.data_locks\G
*************************** 1. row ***************************
ENGINE: INNODB
ENGINE_LOCK_ID: 4874033848:1112:140194859105464
ENGINE_TRANSACTION_ID: 15171
THREAD_ID: 49
EVENT_ID: 12
OBJECT_SCHEMA: test
OBJECT_NAME: mytable
PARTITION_NAME: NULL
SUBPARTITION_NAME: NULL
INDEX_NAME: NULL
OBJECT_INSTANCE_BEGIN: 140194859105464
LOCK_TYPE: TABLE <-- TABLE lock
LOCK_MODE: IX <-- "IX" intention lock mode
LOCK_STATUS: GRANTED
LOCK_DATA: NULL
*************************** 2. row ***************************
ENGINE: INNODB
ENGINE_LOCK_ID: 4874033848:55:4:2:140194863232024
ENGINE_TRANSACTION_ID: 15171
THREAD_ID: 49
EVENT_ID: 12
OBJECT_SCHEMA: test
OBJECT_NAME: mytable
PARTITION_NAME: NULL
SUBPARTITION_NAME: NULL
INDEX_NAME: PRIMARY
OBJECT_INSTANCE_BEGIN: 140194863232024
LOCK_TYPE: RECORD
LOCK_MODE: X
LOCK_STATUS: WAITING
LOCK_DATA: 1
还有一个METADATA_LOCKS表:
mysql> select * from performance_schema.metadata_locks;
+-------------+--------------------+----------------+-------------+-----------------------+---------------------+---------------+-------------+-------------------+-----------------+----------------+
| OBJECT_TYPE | OBJECT_SCHEMA | OBJECT_NAME | COLUMN_NAME | OBJECT_INSTANCE_BEGIN | LOCK_TYPE | LOCK_DURATION | LOCK_STATUS | SOURCE | OWNER_THREAD_ID | OWNER_EVENT_ID |
+-------------+--------------------+----------------+-------------+-----------------------+---------------------+---------------+-------------+-------------------+-----------------+----------------+
| TABLE | test | mytable | NULL | 140194860588144 | SHARED_WRITE | TRANSACTION | GRANTED | sql_parse.cc:5820 | 48 | 26 |
...