- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是一个随机行为(在重新启动 mysql session 后没有再次发生),当我将 DDL 查询包装在事务 T1 中,同时我使用 select
查询启动另一个事务 T2在同一张表上,DDL 查询卡住等待表元数据锁定,这是预期的,并且任何其他选择查询等待 DDL 查询完成。但是在提交 T2 之后,DDL 应该获得元数据锁定并完成,但它仍然等待表元数据锁定状态。
连接 1 查询:
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER TABLE merchants ADD COLUMN temp6 varchar(255);
连接 2 查询:
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from merchants where account_id=null;
Empty set (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
查询顺序是:
Server version: 8.0.13 MySQL Community Server - GPL
mysql> select @@autocommit;
+--------------+
| @@autocommit |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
mysql> SELECT
OBJECT_TYPE,
OBJECT_SCHEMA,
OBJECT_NAME,
LOCK_TYPE,
LOCK_STATUS,
THREAD_ID,
PROCESSLIST_ID,
PROCESSLIST_INFO
FROM performance_schema.metadata_locks
INNER JOIN performance_schema.threads ON THREAD_ID = OWNER_THREAD_ID
WHERE PROCESSLIST_ID <> CONNECTION_ID();
+-------------+-------------------------+-----------------------------------+---------------------+-------------+-----------+----------------+-----------------------------------------------------+
| OBJECT_TYPE | OBJECT_SCHEMA | OBJECT_NAME | LOCK_TYPE | LOCK_STATUS | THREAD_ID | PROCESSLIST_ID | PROCESSLIST_INFO |
+-------------+-------------------------+-----------------------------------+---------------------+-------------+-----------+----------------+-----------------------------------------------------+
| GLOBAL | NULL | NULL | INTENTION_EXCLUSIVE | GRANTED | 126 | 87 | ALTER TABLE merchants ADD COLUMN temp6 varchar(255) |
| SCHEMA | merchant_onboarding_dev | NULL | INTENTION_EXCLUSIVE | GRANTED | 126 | 87 | ALTER TABLE merchants ADD COLUMN temp6 varchar(255) |
| TABLE | merchant_onboarding_dev | merchants | SHARED_UPGRADABLE | GRANTED | 126 | 87 | ALTER TABLE merchants ADD COLUMN temp6 varchar(255) |
| BACKUP LOCK | NULL | NULL | INTENTION_EXCLUSIVE | GRANTED | 126 | 87 | ALTER TABLE merchants ADD COLUMN temp6 varchar(255) |
| TABLESPACE | NULL | merchant_onboarding_dev/merchants | INTENTION_EXCLUSIVE | GRANTED | 126 | 87 | ALTER TABLE merchants ADD COLUMN temp6 varchar(255) |
| TABLE | merchant_onboarding_dev | #sql-63_57 | EXCLUSIVE | GRANTED | 126 | 87 | ALTER TABLE merchants ADD COLUMN temp6 varchar(255) |
| TABLE | merchant_onboarding_dev | merchants | EXCLUSIVE | PENDING | 126 | 87 | ALTER TABLE merchants ADD COLUMN temp6 varchar(255) |
+-------------+-------------------------+-----------------------------------+---------------------+-------------+-----------+----------------+-----------------------------------------------------+
7 rows in set (0.00 sec)
mysql> show full processlist;
+----+-----------------+-----------------+-------------------------+---------+--------+---------------------------------+-----------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------------+-------------------------+---------+--------+---------------------------------+-----------------------------------------------------+
| 4 | event_scheduler | localhost | NULL | Daemon | 115824 | Waiting on empty queue | NULL |
| 62 | root | localhost:59116 | merchant_onboarding_dev | Sleep | 107 | | NULL |
| 63 | root | localhost:59117 | merchant_onboarding_dev | Sleep | 107 | | NULL |
| 65 | root | localhost:59119 | NULL | Sleep | 1 | | NULL |
| 79 | root | localhost | merchant_onboarding_dev | Query | 0 | starting | show full processlist |
| 81 | root | localhost | merchant_onboarding_dev | Sleep | 838 | | NULL |
| 83 | root | localhost | merchant_onboarding_dev | Sleep | 821 | | NULL |
| 87 | root | localhost | merchant_onboarding_dev | Query | 842 | Waiting for table metadata lock | ALTER TABLE merchants ADD COLUMN temp6 varchar(255) |
+----+-----------------+-----------------+-------------------------+---------+--------+---------------------------------+-----------------------------------------------------+
8 rows in set (0.00 sec)
mysql> SHOW ENGINE INNODB STATUS;
=====================================
2019-04-08 02:30:45 0x700006ef3000 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 20 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 242 srv_active, 0 srv_shutdown, 73418 srv_idle
srv_master_thread log flush and writes: 0
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 87
OS WAIT ARRAY INFO: signal count 516
RW-shared spins 722, rounds 723, OS waits 1
RW-excl spins 1110, rounds 3537, OS waits 28
RW-sx spins 0, rounds 0, OS waits 0
Spin rounds per wait: 1.00 RW-shared, 3.19 RW-excl, 0.00 RW-sx
------------
TRANSACTIONS
------------
Trx id counter 15753
Purge done for trx's n:o < 15749 undo n:o < 0 state: running but idle
History list length 11
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 281479647891952, not started
mysql tables in use 1, locked 1
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 281479647891040, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 281479647893776, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 281479647892864, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 281479647890128, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 281479647889216, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 0
950 OS file reads, 52924 OS file writes, 40911 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
insert 0, delete mark 0, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 34679, node heap has 3 buffer(s)
Hash table size 34679, node heap has 3 buffer(s)
Hash table size 34679, node heap has 7 buffer(s)
Hash table size 34679, node heap has 2 buffer(s)
Hash table size 34679, node heap has 2 buffer(s)
Hash table size 34679, node heap has 1 buffer(s)
Hash table size 34679, node heap has 2 buffer(s)
Hash table size 34679, node heap has 4 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 89894761
Log buffer assigned up to 89894761
Log buffer completed up to 89894761
Log written up to 89894761
Log flushed up to 89894761
Added dirty pages up to 89894761
Pages flushed up to 89894761
Last checkpoint at 89894761
42886 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 137428992
Dictionary memory allocated 1037251
Buffer pool size 8191
Free buffers 3332
Database pages 4835
Old database pages 1764
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 634, not young 1
0.00 youngs/s, 0.00 non-youngs/s
Pages read 911, created 3927, written 8771
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 4835, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Process ID=99, Main thread ID=0x700006689000 , state=sleeping
Number of rows inserted 176434, updated 4249, deleted 10814, read 2383183
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
我无法理解的是为什么 DDL 查询一直在等待表元数据锁,即使持有锁的事务已提交。
最佳答案
在 MySQL 中,大多数 DDL 语句实际上生成一个隐式提交。 ALTER TABLE
语句属于这一类。来自 the documentation :
The statements listed in this section (and any synonyms for them) implicitly end any transaction active in the current session, as if you had done a COMMIT before executing the statement.
Most of these statements also cause an implicit commit after executing.
这internal documentation进入更多细节......事情往往会变得困惑:
In addition, some DDL statements issue interim transaction commits: for example,
ALTER TABLE
issues a commit after data is copied from the original table to the internal temporary table.
因此,当您执行 ALTER TABLE
时,MySQL 会隐式地尝试在语句中提交,但由于另一个事务正在进行中而无法提交。该文档并未准确涵盖此用例,但我怀疑服务器未正确处理这种情况,并生成了您所看到的奇怪行为。文档本身声明这种类型的行为定义不当。
底线:在运行 ALTER TABLE
语句时,您不能依赖事务来处理并发。因此,您不希望在表正忙于处理其他事务时ALTER
:这是不安全的。在运行这样的语句之前,您要确保没有在表上设置锁。是的,在处理类似生产的数据库时,这肯定会带来痛苦的限制,但看起来 MySQL 就是这样做的……
关于包裹在事务中时,Mysql DDL 查询卡在等待表元数据锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55563562/
有没有办法为模式差异生成 DDL,就像 TOAD 中的同步功能一样? 不确定如何使用“updateSQL”选项?我正在使用 diff 选项来比较模式。 最佳答案 您可以通过两个步骤来完成: 在两个模式
我正在向表中添加一些新列,并希望向表 DDL 添加文档以供将来的开发人员使用。如何解决这个问题? 最佳答案 通用语法: COMMENT ON {OBJECT} {OBJECTNAME} AS '{25
我有一个连接到 postgresql 数据库的 spring 项目。连接在此 application.yml 文件中定义: spring: datasource: #TODO replace ap
这个问题与Converting MySQL DDL to SQL Server DDL完全相反 . 我有一个 SQL Server 2008 DDL 文件。我想将其导入MySQL。 有什么工具或建议可
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是
当下拉列表值已从某些外部资源修改时,如何在下拉列表的 JQuery 中调用更改事件? 如果我更改下拉列表中的值,下面的代码可以很好地工作。 Jan Feb $('#month').ch
我们有大量不同的数据源,包括 oracle、db2、mysql。我们还需要在最后添加一些审计列。我遇到了以下 Java 类 org.apache.sqoop.hive.HiveTypes。我计划创建一
我有一个为 MySQL 5 生成的模型,但现在我需要在 SQL Server 安装上创建这些表。 年以来我一直在使用 SQL 服务器,我想确保我可以将此脚本转换为兼容。 我真的不知道要寻找什么 TBQ
我的 View 中有三个 DDL。我在第一个 DDL 的“onchange”上触发一个 JavaScript 函数来填充第二个 DDL,并在第二个 DDL 的“onchange”上触发一个 JavaS
我试图将一个事件绑定(bind)到下拉列表,因为昨天没有任何帮助去 JavaScript 聊天室,没有人能够找到解决我的问题的方法,但后来我以某种方式尝试绑定(bind)我的下拉列表,如下所示 $('
我正在尝试学习如何为给定的 Hive 数据库中的所有表自动创建 DDL。 例如,我有一个名为 abc_db 的数据库。我写了一个Hive脚本,将所有的表写入一个名为abc_db.txt的文件中。文件中
我正在尝试使用jpa-ddl-maven-plugin,如here所述但我使用 Apache Maven 3.0.3 时收到以下错误。有什么想法我哪里出错了吗? 我的 pom.xml:
我是 Spring 和 Hibernate 的新手,我已经尝试了所有我能找到的方法来解决这个问题,但没有成功。任何帮助将不胜感激。我目前正在关注一本书中的一个示例项目,并且收到一条错误消息“Gener
鉴于架构: MACHINE_TYPE { machine_type } 机器{机器,机器类型} SORT_PLAN { sort_plan, machine_type } 日程表 { day_of_w
DML、DDL、DCL区别 . 总体解释: DML(data manipulation language): 它们是SELECT、UPDATE、IN
我需要在 Oracle 数据库上执行一堆(最多约 1000000 条)sql 语句。这些语句最后应该导致引用一致的状态,如果发生错误,所有语句都应该回滚。这些语句不是按引用顺序出现的。因此,如果启用了
在这种情况下,可能具有SQLITE DDL(LineNum为AUTO INCREMENT列是TransDate列的一部分)。如果我在行中插入新的每日交易条目,则将自动使用AUTO INCREMENT
我最近开始学习 hive 。我有一个具有以下值的python列表 list=['hello:struct', 'env:string', 'element:struct','AppId:string'
我正在将表从Hive 1迁移到HDInsight Hive 2,并且在从源到目标的表上重新创建表时,DDL中的以下行未正确解析: FIELDS TERMINATED BY ' ' COLLECTION
我正在尝试在 HSQL 数据库中重新创建一个 oracle 数据库。 这是为了在本地开发人员系统上进行更好的单元测试。 我需要知道的是,是否有任何我可以在 oracle 服务器/客户端中使用的工具/命
我是一名优秀的程序员,十分优秀!