简体   繁体   English

innodb表级锁

[英]innodb table level lock

We face the table level lock issues, almost every day on our test servers. 我们几乎每天在测试服务器上都面临表级锁定问题。

TRANSACTION 0, not started
mysql tables in use 97, locked 97
MySQL thread id 429, OS thread handle 0x2aff6ff59700, query id 24900 ec2-*-*-*-*.compute-1.amazonaws.com *.*.*.* sminq cleaning up
---TRANSACTION 10631403, not started
MySQL thread id 321, OS thread handle 0x2aff7b359700, query id 24901 115.112.140.139 sminq init
show engine innodb status
---TRANSACTION 10632661, not started
MySQL thread id 13, OS thread handle 0x2aff4e39a700, query id 24817 localhost 127.0.0.1 rdsadmin cleaning up
---TRANSACTION 10632664, not started
MySQL thread id 6, OS thread handle 0x2aff396c5700, query id 24873 ec2-*-*-*-*.ap-southeast-1.compute.amazonaws.com *.*.*.* sminq cleaning up
---TRANSACTION 10632655, not started
MySQL thread id 7, OS thread handle 0x2aff39706700, query id 24783 ec2-*-*-*-*.ap-southeast-1.compute.amazonaws.com *.*.*.* sminq cleaning up
---TRANSACTION 10632652, not started
MySQL thread id 3, OS thread handle 0x2aff37d28700, query id 24745 ec2-*-*-*-*.ap-southeast-1.compute.amazonaws.com *.*.*.* sminq cleaning up
---TRANSACTION 10627075, not started
MySQL thread id 1, OS thread handle 0x2aff37ca6700, query id 0 Waiting for background binlog tasks
---TRANSACTION 10632663, ACTIVE 7 sec
mysql tables in use 1, locked 1
MySQL thread id 431, OS thread handle 0x2aff37daa700, query id 24863  172.31.3.120 sminq Waiting for table level lock
insert into `sminq`.`Queue_token` (`token_queue_id`,    `total_process_time`, `token_user`, `created_on`, `join_date`, `join_time`, `app_type`, `token_user_group`, `uuid`) values (13, 10, 87, '2016-07-21 04:47:04.157000', '2016-07-21 10:17:04', '10:10:00', 1, NULL, 'D<??BY??7?gk?Uo')
Trx #rec lock waits 0 #table lock waits 0
Trx total rec lock wait time 0 SEC
Trx total table lock wait time 0 SEC
---TRANSACTION 10632646, ACTIVE 45 sec

These keep occurring for Inserts only, we have never faced the issue with Updates or deletes. 这些仅发生在插入中,我们从未遇到过更新或删除的问题。

I have an Isolation level READ-COMMITTED, along with innodb_autoinc_lock_mode = 2 我的隔离级别为READ-COMMITTED,以及innodb_autoinc_lock_mode = 2

mysqltuner output for the same server 同一服务器的mysqltuner输出

[--] Up for: 2h 11m 55s (25K q [3.230 qps], 478 conn, TX: 3M, RX: 1M)
[--] Reads / Writes: 82% / 18%
[--] Binary logging is enabled (GTID MODE: OFF)
[--] Total buffers: 1.5G global + 17.0M per thread (100 max threads)
[!!] Maximum reached memory usage: 3.0G (152.35% of installed RAM)
[!!] Maximum possible memory usage: 3.1G (156.50% of installed RAM)
[OK] Slow queries: 0% (0/25K)
[!!] Highest connection usage: 95%  (95/100)
[OK] Aborted connections: 0.00%  (0/478)
[!!] Query cache is disabled
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 1K sorts)
[OK] Temporary tables created on disk: 24% (424 on disk / 1K total)
[OK] Thread cache hit rate: 80% (95 created / 478 connections)
[OK] Table cache hit rate: 129% (291 open / 224 opened)
[OK] Open file limit used: 0% (64/65K)
[OK] Table locks acquired immediately: 99% (6K immediate / 6K locks)
[OK] Binlog cache memory access: 100.00% ( 1618 Memory / 1618 Total)

-------- MyISAM Metrics -----------------------------------------------------
[!!] Key buffer used: 18.5% (1M used / 8M cache)
[OK] Key buffer size / total MyISAM indexes: 8.0M/2.4M
[!!] Read Key buffer hit rate: 82.2% (90 cached / 16 reads)

-------- InnoDB Metrics -----------------------------------------------------
[--] InnoDB is enabled.
[OK] InnoDB buffer pool / data size: 1.3G/29.0M
[!!] InnoDB buffer pool instances: 8
[!!] InnoDB Used buffer: 1.32% (1139 used/ 86584 total)
[OK] InnoDB Read buffer efficiency: 99.86% (713109 hits/ 714137 total)
[!!] InnoDB Write buffer efficiency: 0.00% (0 hits/ 1 total)
[OK] InnoDB log waits: 0.00% (0 waits / 4915 writes)

Since this is a test server we are running the tests on a t2.small 由于这是测试服务器,因此我们在t2.small上运行测试

1.3G buffer_pool in 2GB of RAM? 1.3GB buffer_pool是否在2GB RAM中? This probably leads to lots of swapping, which is really bad for performance. 这可能导致大量交换,这确实对性能不利。

For 2GB of RAM and only 29M of data, let's set innodb_buffer_pool_size = 100M . 对于2GB的RAM和仅29M的数据,让我们设置innodb_buffer_pool_size = 100M That should enough for now, and safe for later (as the data grows). 现在就足够了,以后再用(随着数据的增长)就可以了。

(The recommendation of 70% or 80% only applies for machines with at least 4GB of RAM.) (建议70%或80%仅适用于至少具有4GB RAM的计算机。)

Fix that. 解决这个。 If the problem continues, then update the question with newer values, plus SHOW CREATE TABLE for the table(s) involved. 如果问题仍然存在,请使用较新的值以及有关表的SHOW CREATE TABLE更新问题。

An AUTO-INC lock is a special table-level lock taken by transactions inserting into tables with AUTO_INCREMENT columns. AUTO-INC锁是一种特殊的表级锁,由插入到具有AUTO_INCREMENT列的表中的事务获取。 In the simplest case, if one transaction is inserting values into the table, any other transactions must wait to do their own inserts into that table, so that rows inserted by the first transaction receive consecutive primary key values. 在最简单的情况下,如果一个事务正在向表中插入值,那么任何其他事务都必须等待自己在该表中进行插入,以便第一个事务插入的行接收连续的主键值。

The innodb_autoinc_lock_mode configuration option controls the algorithm used for auto-increment locking. innodb_autoinc_lock_mode配置选项控制用于自动增量锁定的算法。 It allows you to choose how to trade off between predictable sequences of auto-increment values and maximum concurrency for insert operations. 它使您可以选择如何在可预测的自动增量值序列与插入操作的最大并发性之间进行权衡。

The permissible values are 0, 1, or 2, for “traditional”, “consecutive”, or “interleaved” lock mode 对于“传统”,“连续”或“交错”锁定模式,允许的值为0、1或2

innodb_autoinc_lock_mode = 2 (“interleaved” lock mode) innodb_autoinc_lock_mode = 2(“交错”锁定模式)

In this lock mode, no “INSERT-like” statements use the table-level AUTO-INC lock, and multiple statements can execute at the same time. 在这种锁定模式下,没有“类似于INSERT的”语句使用表级的AUTO-INC锁定,并且可以同时执行多个语句。 This is the fastest and most scalable lock mode, but it is not safe when using statement-based replication or recovery scenarios when SQL statements are replayed from the binary log. 这是最快,最具扩展性的锁定模式,但是当使用基于语句的复制或恢复方案从二进制日志中重放SQL语句时,这是不安全的。

In this lock mode, auto-increment values are guaranteed to be unique and monotonically increasing across all concurrently executing “INSERT-like” statements. 在这种锁定模式下,可以确保自动增量值是唯一的,并且在所有同时执行的“类似INSERT”语句中单调递增。 However, because multiple statements can be generating numbers at the same time (that is, allocation of numbers is interleaved across statements), the values generated for the rows inserted by any given statement may not be consecutive. 但是,由于多个语句可以同时生成数字(也就是说,在语句之间交错分配数字),因此为任何给定语句插入的行生成的值可能不是连续的。

If the only statements executing are “simple inserts” where the number of rows to be inserted is known ahead of time, there will be no gaps in the numbers generated for a single statement, except for “mixed-mode inserts”. 如果唯一执行的语句是“简单插入”,其中要提前知道要插入的行数,则为“除混合模式插入”外,为单个语句生成的数字将没有间隙。 However, when “bulk inserts” are executed, there may be gaps in the auto-increment values assigned by any given statement. 但是,执行“批量插入”时,任何给定语句分配的自动增量值可能存在间隙。

Refer the source Locking and modify the configuration based on your requirement. 请参阅源锁定,然后根据需要修改配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM