通过案例学调优之--Oracle Latch基础知识

简介:

一、什么是 Latch
1.1、 Latch 是一种低级排队(串行)机制,用于保护 SGA 中共享内存结构。

1.2、 Latch 是一种快速的被获取和释放的内存锁,用于防止共享内存结构被多个用户同时 访问。

1.3、 Latch 请求的类型:  willing-to-wait 和 immediate

1.3.1 对于 willing-to-wait 类型的 latch:

如果一个进程在第一次尝试中没有获得 latch,那么它会等待并且再尝试一次,如果经过_spin_count 次争夺不能获得 latch, 然后该进程转入睡眠状态,睡眠醒来后,按顺序重复以 前的步骤。睡眠的时间会越来越长。

1.3.2 对于immediate类型的latch:

如果该闩不能立即得到的话,那么该进程就不会为获得该 latch 而等待。它将继续执行 另一个操作。

1.3.3 可以通过 v$latch 视图查看 latch 的具体信息,在 v$latch 中:
(以下字段反映了 
Willing-to-wait 类型请求)
GETS:成功地以 Willing-to-wait 请求类型请求一个 latch 的次数。
MISSES:初始以 Willing-to-wait 请求类型请求一个 latch 不成功的次数。

SLEEPS:初始以 Willing-to-wait 请求类型请求一个 latch 不成功后,进程等待获取 latch的次数

(以下字段反映了 Immediate 类型请求)
IMMEDIATE_GETS:以 Immediate 请求类型成功地获得一个 latch 的次数。

IMMEDIATE_MISSES:以 Immediate 请求类型请求一个 latch 不成功的次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
12 : 27 : 09  SYS@ prod >desc v$latch
  Name                                                              Null?    Type
  ----------------------------------------------------------------- -------- --------------------------------------------
  ADDR                                                                       RAW( 4 )
  LATCH#                                                                     NUMBER
  LEVEL#                                                                     NUMBER
  NAME                                                                       VARCHAR2( 64 )
  HASH                                                                       NUMBER
  GETS                                                                       NUMBER
  MISSES                                                                     NUMBER
  SLEEPS                                                                     NUMBER
  IMMEDIATE_GETS                                                             NUMBER
  IMMEDIATE_MISSES                                                           NUMBER
  WAITERS_WOKEN                                                              NUMBER
  WAITS_HOLDING_LATCH                                                        NUMBER
  SPIN_GETS                                                                  NUMBER
  SLEEP1                                                                     NUMBER
  SLEEP2                                                                     NUMBER
  SLEEP3                                                                     NUMBER
  SLEEP4                                                                     NUMBER
  SLEEP5                                                                     NUMBER
  SLEEP6                                                                     NUMBER
  SLEEP7                                                                     NUMBER
  SLEEP8                                                                     NUMBER
  SLEEP9                                                                     NUMBER
  SLEEP10                                                                    NUMBER
  SLEEP11                                                                    NUMBER
  WAIT_TIME                                                                  NUMBER

1.4、 与队列锁(enqueue)不同的是:

     对于 Latch 的请求,不用排队等待资源,由于 Latch的分配相当随机,所以当 Latch 释放后,不管哪个会话请求都可能得到它,当不能获得Latch的会话,就会在后续不断的重试(spin->sleep->spin...->get latch)。
1.4.1 为什么自旋(spin)?

在一台多 CPU的机器上,如果 Latch 不能立即获得,那么会话就会自旋,即在循环中反复地 尝试来或得 Latch。如果不这样的话,那么就会导致上下文切换(context switching),这样对 资源开销更大。

wKiom1Ri3XahcYUwAAGJBlVV5Do553.jpg

1.4.2 得到Latch的伪代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
If Latch gotten
Then
Return SUCCESS
Else
Misses  on  that Latch = Misses +  1
Loop
For I  in  1  ..  2000 (_spin_count)
Loop
Attempt to get Latch
If Latch gottenThen
Return SUCCESS
End  if
End loopGo to sleep  for  short period
Sleep  on  Latch = Sleeps +  1
End loop
End  if

图示如下:

wKioL1Ri3hfxZhNcAAEi1myeT7c029.jpg

所以,可以知道,当尝试获得 Latch 的时候,可能会消耗大量的 CPU 时间。系统看上去很忙, 但并没有做多少实际工作。

1.5、相关视图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
1.5. 1  v$latch :
V$LATCH shows aggregate latch statistics  for  both parent  and  child latches, grouped by latch name.
13 : 20 : 55  SYS@ prod >desc v$latch
  Name                                                              Null?    Type
  ----------------------------------------------------------------- -------- --------------------------------------------
  ADDR                                                                       RAW( 4 )
  LATCH#                                                                     NUMBER
  LEVEL#                                                                     NUMBER
  NAME                                                                       VARCHAR2( 64 )
  HASH                                                                       NUMBER
  GETS                                                                       NUMBER
  MISSES                                                                     NUMBER
  SLEEPS                                                                     NUMBER
  IMMEDIATE_GETS                                                             NUMBER
  IMMEDIATE_MISSES                                                           NUMBER
  WAITERS_WOKEN                                                              NUMBER
  WAITS_HOLDING_LATCH                                                        NUMBER
  SPIN_GETS                                                                  NUMBER
  SLEEP1                                                                     NUMBER
  SLEEP2                                                                     NUMBER
  SLEEP3                                                                     NUMBER
  SLEEP4                                                                     NUMBER
  SLEEP5                                                                     NUMBER
  SLEEP6                                                                     NUMBER
  SLEEP7                                                                     NUMBER
  SLEEP8                                                                     NUMBER
  SLEEP9                                                                     NUMBER
  SLEEP10                                                                    NUMBER
  SLEEP11                                                                    NUMBER
  WAIT_TIME
 
1.5. 2  v$latch_children:V$LATCH_CHILDREN contains statistics about child latches.
13 : 20 : 15  SYS@ prod >desc v$latch_children
  Name                                                              Null?    Type
  ----------------------------------------------------------------- -------- --------------------------------------------
  ADDR                                                                       RAW( 4 )
  LATCH#                                                                     NUMBER
  CHILD#                                                                     NUMBER
  LEVEL#                                                                     NUMBER
  NAME                                                                       VARCHAR2( 64 )
  HASH                                                                       NUMBER
  GETS                                                                       NUMBER
  MISSES                                                                     NUMBER
  SLEEPS                                                                     NUMBER
  IMMEDIATE_GETS                                                             NUMBER
  IMMEDIATE_MISSES                                                           NUMBER
  WAITERS_WOKEN                                                              NUMBER
  WAITS_HOLDING_LATCH                                                        NUMBER
  SPIN_GETS                                                                  NUMBER
  SLEEP1                                                                     NUMBER
  SLEEP2                                                                     NUMBER
  SLEEP3                                                                     NUMBER
  SLEEP4                                                                     NUMBER
  SLEEP5                                                                     NUMBER
  SLEEP6                                                                     NUMBER
  SLEEP7                                                                     NUMBER
  SLEEP8                                                                     NUMBER
  SLEEP9                                                                     NUMBER
  SLEEP10                                                                    NUMBER
  SLEEP11                                                                    NUMBER
  WAIT_TIME                                                                  NUMBER
  
1.5. 3  v$latch_misses :This view contains statistics about missed attempts to acquire a latch.
13 : 20 : 10  SYS@ prod >desc v$latch_misses
  Name                                                              Null?    Type
  ----------------------------------------------------------------- -------- --------------------------------------------
  PARENT_NAME                                                                VARCHAR2( 64 )
  WHERE                                                                      VARCHAR2( 80 )
  NWFAIL_COUNT                                                               NUMBER
  SLEEP_COUNT                                                                NUMBER
  WTR_SLP_COUNT                                                              NUMBER
  LONGHOLD_COUNT                                                             NUMBER
  LOCATION                                                                   VARCHAR2( 80 )
  
1.5. 4  v$latchname :This view contains information about decoded latch names  for  the latches shown  in
13 : 19 : 11  SYS@ prod >desc v$latchname
  Name                                                              Null?    Type
  ----------------------------------------------------------------- -------- --------------------------------------------
  LATCH#                                                                     NUMBER
  NAME                                                                       VARCHAR2( 64 )
  HASH                                                                       NUMBER









本文转自 客居天涯 51CTO博客,原文链接:http://blog.51cto.com/tiany/1575651,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
JavaScript 前端开发 Java
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Jsp页面
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Jsp页面
15 0
|
1月前
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——OpSessionview实现
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——OpSessionview实现
17 0
|
1月前
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——EmpDao层代码
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——EmpDao层代码
22 0
|
1月前
|
Java
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Action的实现类
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Action的实现类
14 0
|
1月前
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——工具类
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——工具类
13 0
|
1月前
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——DeptDao层代码
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——DeptDao层代码
14 0
|
1月前
|
XML Java 数据库连接
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
16 0
|
1月前
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Dept实体类和对应的配置信息
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Dept实体类和对应的配置信息
16 1
|
1月前
|
Oracle 关系型数据库
oracle基本笔记整理及案例分析2
oracle基本笔记整理及案例分析2
20 0
|
1月前
|
Oracle 关系型数据库
oracle基本笔记整理及案例分析1
oracle基本笔记整理及案例分析1
27 0

推荐镜像

更多