189 8069 5689

mysql学习13:第八章:锁

1.   锁

InnoDB支持行锁,有时升级为表锁。

创新互联自2013年起,是专业互联网技术服务公司,拥有项目成都做网站、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元珙县做网站,已为上家服务,为珙县各地企业和个人服务,联系电话:18982081108

MyISAM只支持表锁。

表锁:开小小,加锁快,不会出现死锁;锁粒度大,锁冲突概率高,并发度低。

行锁:开销大,加锁慢,会出现死锁,锁粒度小,锁冲突概率低,并发高。

1.1.   InnoDB锁类型

主要分为:读锁(共享锁),写锁(排他锁),意向锁,和MDL锁。

1.1.1.   读锁

读锁,S锁,一个事物在读取一个数据行时,其他事务也可以读,但不能对该数据行增删改的操作。两种select方式的应用。

l   自动提交模式下的select查询语句,不需加任何锁返回结果,是一致性非锁定读。

l   通过select....lock in share mode在被读取的行记录或行记录的范围上加一个读锁,让其他事务可读不可申请加写锁。

1.1.2.   写锁

写锁简称X锁,一个事务获取一行的写锁,其他事务就不能获取该行其它锁,优先级最高。

select for update,会对读取的行记录上加一个写锁,其他任何事务就不能加任何锁。

1.1.3.   MDL锁

MySQL5.5引入meta data lock,简称MDL锁,用于保护表中元数据的信息。即一个事务查询表将自动给表加MDL锁,其他事务不能做任何DDL操作。

1.1.4.   意向锁

InnoDB引擎中,意向锁是表级锁,作用和MDL类似,防止事务进行过程中,执行DDL语句的操作而导致数据不一致。有两种意向锁类型:

l   意向共享锁(IS):数据行加共享锁前必须先取得该表的IS锁。

l   意向排他锁(IX):数据行加排他锁前必须先取得该表的IX锁。

1.2.   InnoDB行锁种类

InnoDB默认事务隔离级别为RR,且参数innodb_locks_unsafe_for_binlog=0的模式下,行锁有三种。

l   单个行记录的锁(record lock),主键和唯一索引都是。

l   间隙锁(GAP lock)

l   记录锁和间隙锁的组合叫next-key lock。普通索引默认。

1.2.1.   单个行记录的锁

InnoDB上的行锁就是加在索引上。有索引,更新只锁指定行,无索引,更新锁所有行。

1.2.2.   间隙锁(GAP lock)

RR隔离级别,为了避免幻读,引入Gap lock,只锁定行记录数据的范围,不包含记录本身,即不允许在此范围内插入任何数据。

RC隔离级别允许出现幻读现象。

1.2.3.   Next-Key Locks

Next-key lock是记录锁(Record Lock)与间隔锁(Gap Lock)的组合,当InnoDB扫描索引记录时,会先对选中的索引记录加上记录锁(Record lock),再对索引记录两边的间隙加上间隙锁(Gap lock)。

1.3.   锁等待和死锁

锁等待,是一个事务产生锁,其他事务等待上个事务释放它的锁。锁等待超时阈值innodb_lok_wait_timeout控制,单位秒。

死锁,多个事务争夺资源相互等待的现象,即锁资源请求产生了回路,就是死循环。

避免死锁的方法:

l   如不同的程序会并发存取多个表,或涉及多汗记录,尽量约定以相同的顺序访问表。

l   业务中尽量采用小事务,避免大事务,及时提交或回滚。

l   在同一个事务中,尽可能做到一次锁定所需的所有资源。

l   对容易产生死锁的业务,可以尝试使用升级锁粒度,通过表锁定减少锁产生的概率。

 

通过show engine innodb sttus查看死锁展示信息

[(none)]>show engine innodb status;

 

| Type   | Name | Status

 

| InnoDB |      |

=====================================

2018-11-07 22:49:40 0x7f1320202700 INNODB MONITOR OUTPUT

=====================================

Per second averages calculated from the last 11 seconds

-----------------

BACKGROUND THREAD

-----------------

srv_master_thread loops: 98 srv_active, 0 srv_shutdown, 49465 srv_idle

srv_master_thread log flush and writes: 49563

----------

SEMAPHORES

----------

OS WAIT ARRAY INFO: reservation count 20

OS WAIT ARRAY INFO: signal count 20

RW-shared spins 0, rounds 34, OS waits 16

RW-excl spins 0, rounds 200, OS waits 2

RW-sx spins 0, rounds 0, OS waits 0

Spin rounds per wait: 34.00 RW-shared, 200.00 RW-excl, 0.00 RW-sx

------------

TRANSACTIONS

------------

Trx id counter 65440

Purge done for trx's n:o < 65438 undo n:o < 0 state: running but idle

History list length 12

LIST OF TRANSACTIONS FOR EACH SESSION:

---TRANSACTION 421197684710112, not started

0 lock struct(s), heap size 1136, 0 row lock(s)

---TRANSACTION 421197684709200, not started

0 lock struct(s), heap size 1136, 0 row lock(s)

--------

FILE I/O

--------

I/O thread 0 state: waiting for completed aio requests (insert buffer thread)

I/O thread 1 state: waiting for completed aio requests (log thread)

I/O thread 2 state: waiting for completed aio requests (read thread)

I/O thread 3 state: waiting for completed aio requests (read thread)

I/O thread 4 state: waiting for completed aio requests (read thread)

I/O thread 5 state: waiting for completed aio requests (read thread)

I/O thread 6 state: waiting for completed aio requests (read thread)

I/O thread 7 state: waiting for completed aio requests (read thread)

I/O thread 8 state: waiting for completed aio requests (read thread)

I/O thread 9 state: waiting for completed aio requests (read thread)

I/O thread 10 state: waiting for completed aio requests (write thread)

I/O thread 11 state: waiting for completed aio requests (write thread)

I/O thread 12 state: waiting for completed aio requests (write thread)

I/O thread 13 state: waiting for completed aio requests (write thread)

I/O thread 14 state: waiting for completed aio requests (write thread)

I/O thread 15 state: waiting for completed aio requests (write thread)

I/O thread 16 state: waiting for completed aio requests (write thread)

I/O thread 17 state: waiting for completed aio requests (write thread)

Pending normal aio reads: [0, 0, 0, 0, 0, 0, 0, 0] , aio writes: [0, 0, 0, 0, 0, 0, 0, 0] ,

 ibuf aio reads:, log i/o's:, sync i/o's:

Pending flushes (fsync) log: 0; buffer pool: 0

271 OS file reads, 61118 OS file writes, 60451 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 553193, node heap has 0 buffer(s)

Hash table size 553193, node heap has 0 buffer(s)

Hash table size 553193, node heap has 0 buffer(s)

Hash table size 553193, node heap has 0 buffer(s)

Hash table size 553193, node heap has 0 buffer(s)

Hash table size 553193, node heap has 1 buffer(s)

Hash table size 553193, node heap has 0 buffer(s)

Hash table size 553193, node heap has 0 buffer(s)

0.00 hash searches/s, 0.00 non-hash searches/s

---

LOG

---

Log sequence number 20357616

Log flushed up to   20357616

Pages flushed up to 20357616

Last checkpoint at  20357607

0 pending log flushes, 0 pending chkp writes

60048 log i/o's done, 0.00 log i/o's/second

----------------------

BUFFER POOL AND MEMORY

----------------------

Total large memory allocated 2198863872

Dictionary memory allocated 156387

Buffer pool size   131056

Free buffers       130465

Database pages     590

Old database pages 0

Modified db pages  0

Pending reads      0

Pending writes: LRU 0, flush list 0, single page 0

Pages made young 0, not young 0

0.00 youngs/s, 0.00 non-youngs/s

Pages read 238, created 352, written 805

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: 590, unzip_LRU len: 0

I/O sum[0]:cur[0], unzip sum[0]:cur[0]

----------------------

INDIVIDUAL BUFFER POOL INFO

----------------------

---BUFFER POOL 0

Buffer pool size   16382

Free buffers       16279

Database pages     102

Old database pages 0

Modified db pages  0

Pending reads      0

Pending writes: LRU 0, flush list 0, single page 0

Pages made young 0, not young 0

0.00 youngs/s, 0.00 non-youngs/s

Pages read 38, created 64, written 95

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: 102, unzip_LRU len: 0

I/O sum[0]:cur[0], unzip sum[0]:cur[0]

---BUFFER POOL 1

Buffer pool size   16382

Free buffers       16312

Database pages     70

Old database pages 0

Modified db pages  0

Pending reads      0

Pending writes: LRU 0, flush list 0, single page 0

Pages made young 0, not young 0

0.00 youngs/s, 0.00 non-youngs/s

Pages read 6, created 64, written 64

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: 70, unzip_LRU len: 0

I/O sum[0]:cur[0], unzip sum[0]:cur[0]

---BUFFER POOL 2

Buffer pool size   16382

Free buffers       16319

Database pages     63

Old database pages 0

Modified db pages  0

Pending reads      0

Pending writes: LRU 0, flush list 0, single page 0

Pages made young 0, not young 0

0.00 youngs/s, 0.00 non-youngs/s

Pages read 8, created 55, written 56

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: 63, unzip_LRU len: 0

I/O sum[0]:cur[0], unzip sum[0]:cur[0]

---BUFFER POOL 3

Buffer pool size   16382

Free buffers       16303

Database pages     79

Old database pages 0

Modified db pages  0

Pending reads      0

Pending writes: LRU 0, flush list 0, single page 0

Pages made young 0, not young 0

0.00 youngs/s, 0.00 non-youngs/s

Pages read 73, created 6, written 59

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: 79, unzip_LRU len: 0

I/O sum[0]:cur[0], unzip sum[0]:cur[0]

---BUFFER POOL 4

Buffer pool size   16382

Free buffers       16265

Database pages     117

Old database pages 0

Modified db pages  0

Pending reads      0

Pending writes: LRU 0, flush list 0, single page 0

Pages made young 0, not young 0

0.00 youngs/s, 0.00 non-youngs/s

Pages read 76, created 41, written 120

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: 117, unzip_LRU len: 0

I/O sum[0]:cur[0], unzip sum[0]:cur[0]

---BUFFER POOL 5

Buffer pool size   16382

Free buffers       16307

Database pages     75

Old database pages 0

Modified db pages  0

Pending reads      0

Pending writes: LRU 0, flush list 0, single page 0

Pages made young 0, not young 0

0.00 youngs/s, 0.00 non-youngs/s

Pages read 11, created 64, written 91

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: 75, unzip_LRU len: 0

I/O sum[0]:cur[0], unzip sum[0]:cur[0]

---BUFFER POOL 6

Buffer pool size   16382

Free buffers       16363

Database pages     19

Old database pages 0

Modified db pages  0

Pending reads      0

Pending writes: LRU 0, flush list 0, single page 0

Pages made young 0, not young 0

0.00 youngs/s, 0.00 non-youngs/s

Pages read 13, created 6, written 12

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: 19, unzip_LRU len: 0

I/O sum[0]:cur[0], unzip sum[0]:cur[0]

---BUFFER POOL 7

Buffer pool size   16382

Free buffers       16317

Database pages     65

Old database pages 0

Modified db pages  0

Pending reads      0

Pending writes: LRU 0, flush list 0, single page 0

Pages made young 0, not young 0

0.00 youngs/s, 0.00 non-youngs/s

Pages read 13, created 52, written 308

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: 65, 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=21556, Main thread ID=139720374998784, state: sleeping

Number of rows inserted 60824, updated 0, deleted 0, read 121836

0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s

----------------------------

END OF INNODB MONITOR OUTPUT

============================

 

1.4.   锁问题的监控

通过show full processlist和show engine Innodb status来判断事务中锁问题情况,另外还有三张表可查:

information_schema.INNODB_TRX

information_schema.INNODB_LOCKS

information_schema.INNODB_LOCK_WAITS

 

innodb_trx表部分字段

trx_id唯一的事务id号;

trx_state:事务状态;

trx_wait_started:事务开始等待时间。

trx_mysql_thread_id:线程ID,与show full processlist相互对应。

trx_query:事务运行的SQL;

trx_operation_state:事务运行的状态。

 

[(none)]>show full processlist;

+----+------+-----------+------+---------+------+----------+-----------------------+

| Id | User | Host      | db   | Command | Time | State    | Info                  |

+----+------+-----------+------+---------+------+----------+-----------------------+

| 57 | root | localhost | NULL | Query   |    0 | starting | show full processlist |

+----+------+-----------+------+---------+------+----------+-----------------------+


分享名称:mysql学习13:第八章:锁
网页URL:http://cdxtjz.cn/article/pshdid.html

其他资讯