首页 > 数据库 > MySQL > 正文

mysql教学 锁

2022-08-01 18:56:44
字体:
来源:转载
供稿:网友
       mysql学习 锁:

1.   锁
       InnoDB支持行锁,有时升级为表锁。
 
     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   对容易产生死锁的业务,可以尝试使用升级锁粒度,通过表锁定减少锁产生的概率。
 
| InnoDB |      |
 
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)
  
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
  
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
  
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]
 
1.4.   锁问题的监控
通过show full processlist和show engine Innodb status来判断事务中锁问题情况,另外还有三张表可查:
 
information_schema.INNODB_TRX
 
information_schema.INNODB_LOCKS
 
information_schema.INNODB_LOCK_WAITS
 
[(none)]>show full processlist;
 
+----+------+-----------+------+---------+------+----------+-----------------------+
 
| Id | User | Host      | db   | Command | Time | State    | Info                  |
 
+----+------+-----------+------+---------+------+----------+-----------------------+
 
| 57 | root | localhost | NULL | Query   |    0 | starting | show full processlist |
 
+----+------+-----------+------+---------+------+----------+-----------------------+

(编辑:错新网)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表