温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Oracle 学习之 性能优化(十四) 内存

发布时间:2020-07-18 20:56:10 来源:网络 阅读:1554 作者:lqding1980 栏目:关系型数据库

 Oracle数据库包含了如下基本内存组件

  • System global area (SGA)

    The SGA is a group of shared memory structures, known as SGA components, that contain data and control information for one Oracle Database instance. The SGA is shared by all server and background processes. Examples of data stored in the SGA include cached data blocks and shared SQL areas.

  • Program global area (PGA)

    A PGA is a nonshared memory region that contains data and control information exclusively for use by an Oracle process. The PGA is created by Oracle Database when an Oracle process is started.

    One PGA exists for each server process and background process. The collection of individual PGAs is the total instance PGA, or instance PGA. Database initialization parameters set the size of the instance PGA, not individual PGAs.

  • User Global Area (UGA)

    The UGA is memory associated with a user session.

  • Software code areas

    Software code areas are portions of memory used to store code that is being run or can be run. Oracle Database code is stored in a software area that is typically at a different location from user programs—a more exclusive or protected location.

Oracle 学习之 性能优化(十四) 内存


内存管理

Oracle依赖于内存相关的初始化参数来控制内存的管理。

内存管理有如下三个选项

  • Automatic memory management

    You specify the target size for instance memory. The database instance automatically tunes to the target memory size, redistributing memory as needed between the SGA and the instance PGA.

  • Automatic shared memory management

    This management mode is partially automated. You set a target size for the SGA and then have the option of setting an aggregate target size for the PGA or managing PGA work areas individually.

  • Manual memory management

    Instead of setting the total memory size, you set many initialization parameters to manage components of the SGA and instance PGA individually.

UGA概览

UGA是会话内存,用来保存会话变量例如登录信息,已经数据库会话需要的其他信息。

Oracle 学习之 性能优化(十四) 内存

当PL/SQL包加载进内存时,UGA中包含了package state,也就是调用PL/SQL时指定的变量值。

PGA概览

Oracle 学习之 性能优化(十四) 内存

PGA缓冲区,则主要是为了某个用户进程所服务的。这个内存区不是共享的,只有这个用户的服务进程本身才能够访问它自己的PGA区。做个形象的比喻,SGA就好像是操作系统上的一个共享文件夹,不同用户可以以此为平台进行数据方面的交流。而PGA就好像是操作系统上的一个私有文件夹,只有这个文件夹的所有者才能够进行访问,其他用户都不能够访问。虽然程序缓存区不向其他用户的进程开放,但是这个内存区仍然肩负着一些重要的使命,如数据排序、权限控制等等都离不开这个内存区。

PGA组件

Oracle 学习之 性能优化(十四) 内存


私有SQL区包含了绑定变量值和运行时期内存结构信息等数据。每一个运行SQL语句的会话都有一个块私有SQL区。一个游标的私有SQL区又分为两个生命周期不同的区:

永久区,包含绑定变量信息。当游标关闭时被释放。

 运行区,当执行结束时释放。

Cursor 

Oracle 学习之 性能优化(十四) 内存


cursor is a name or handle to a specific private SQL area


SGA包含如下组件

  • Database Buffer Cache

  • Redo Log Buffer

  • Shared Pool

  • Large Pool

  • Java Pool

  • Streams Pool

  • Fixed SGA


Buffer cache

Oracle 学习之 性能优化(十四) 内存

Buffer Cache是SGA区中专门用于存放从数据文件中读取的的数据块拷贝的区域。Oracle进程如果发现需要访问的数据块已经在buffer cache中,就直接读写内存中的相应区域,而无需读取数据文件,从而大大提高性能。Buffer cache对于所有oracle进程都是共享的,即能被所有oracle进程访问。

Buffer的大小和数据块一样。

Buffer cache按照类型分为3个池

  • Default pool

    This pool is the location where blocks are normally cached. Unless you manually configure separate pools, the default pool is the only buffer pool.

  • Keep pool

    This pool is intended for blocks that were accessed frequently, but which aged out of the default pool because of lack of space. The goal of the keep buffer pool is to retain objects in memory, thus avoiding I/O operations.

  • Recycle pool

    This pool is intended for blocks that are used infrequently. A recycle pool prevent objects from consuming unnecessary space in the cache.

Oracle还提供了非标准块大小的buffer cache。如果你建立的表空间指定的块大小为非数据库块大小,那么将使用这些buffer cache来缓存数据块。



Oracle 学习之 性能优化(十四) 内存

首先Oracle 以每个数据块的文件号、块号、类型做hash运算,得到hash值。

对于hash值相同的块,放在一个Hash Bucket中。

因为buffer的大小毕竟有限,buffer中的数据块需要根据一定的规则提出内存。

Oracle采用了LRU算法维护一个LRU链表,来决定哪些数据块被淘汰。

通用的淘汰算法如下

Oracle 学习之 性能优化(十四) 内存

Oracle改进了LRU算法,引入了Touch count概念、以及LRU链表分为热端头和冷端头。

Touch count:

 用来记录数据块访问的频繁度,此数值在内存中不受保护,多个进程可以同时修改它。这个值并不是精准的表示块被访问的次数,只是一种趋势。3秒内无论多少用户,访问多少次块。此值加1.


Oracle 学习之 性能优化(十四) 内存

当数据块第一次被放到buffer中,Oracle将其放置在冷端的头部。

如果buffer已经没有空闲空间,那么如何淘汰数据块呢?Oracle从LRU的冷端尾部扫描数据块,当发现数据块的Touch count大于等于2时,将数据块移动到热端头部,并将Touch count置为0 。当Oracle发现Touch count小于2时,则淘汰该数据块。


当数据块被修改了,我们把这个块称之为脏块。脏块在写入磁盘前,是不会被踢出buffer的。

如果LRU中的脏块比较多,每次申请新的空间时,都要扫描很多脏块,但是又不能被淘汰。效率很低。

为此Oracle因为了脏LRU链表。专门用来记录脏数据块。

Oracle 学习之 性能优化(十四) 内存


当块被修改,并不会马上从LRU链表中移动到LRUW中。只有当Oracle需要淘汰数据块时,才会去扫描LRU链表,此时发现块为脏块,将数据块移动到LRUW链表中。


检查点链表

通过上面的描述,我们知道脏块在LRU和LRUW链表中都有。那么当dbwr写数据时,这两个链表都要扫描。首先效率比较低,并且无法保证先修改的数据块先被写入磁盘。

为此Oracle引入了检查点队列,该队列按照数据块第一次被修改顺序将脏块链接到一起。

dbwr写脏块时,只需读取检查点队列即可。

Oracle 学习之 性能优化(十四) 内存并且每个数据块与记录了日志条目的位置

Oracle 学习之 性能优化(十四) 内存


redo log buffer

Oracle 学习之 性能优化(十四) 内存

用户进程将redo entries 拷贝到redo log buffer中。LGWR负责将其写到磁盘中。

Redo entries contain the information necessary to reconstruct, or redo, changes made to the database by DML or DDL operations. Database recovery applies redo entries to data files to reconstruct lost changes.


共享池

Oracle 学习之 性能优化(十四) 内存


Library Cache:

主要存放shared curosr(SQL)和PLSQL对象(function,procedure,trigger)的信息,以及这些对象所依赖的table,index,view等对象的信息。


Private SQL Areas与Shared SQL Area的关系

Oracle 学习之 性能优化(十四) 内存


数据字典缓存

用来缓存系统数据字典表的内容,与普通表的缓存不同,普通表以块为单位缓存到buffer cache中。而数据字典缓存以行为单位,缓存到shared pool中的data dictionary cache中。


Server result cache

用来缓存sql或者plsql的执行结果。


大池:

 The large pool can provide large memory allocations for the following:

  • UGA for the shared server and the Oracle XA interface (used where transactions interact with multiple databases)

  • Message buffers used in the parallel execution of statements

  • Buffers for Recovery Manager (RMAN) I/O slaves


配置内存

Oracle提供了两个初始化参数用来配置内存自动管理

MEMORY_TARGET:sga+pga内存之和,Oracle自动分配SGA和PGA的大小。

MEMORY_MAX_TARGET:MEMORY_TARGET可以设置大小的上限。


SGA自动内存管理

设置初始化参数SGA_TARGET为非0值,并且将STATISTICS_LEVEL的值设置为TYPICAL或者ALL.


PGA自动内存管理

PGA_AGGREGATE_TARGET设置为非0值。

如果workarea_size_policy为auto则sort_area_size,hash_area_size等参数设置被忽略,如果workarea_size_policy为manual,则sort_area_size,hash_area_size等参数设置生效。


也可以手工配置其他各个内存池的大小。当配置了内存自动管理时,有配置了具体池的大小,那么该配置为自动内存分配时的最小大小。


查看内存情况

The following views provide information about dynamic resize operations:

  • V$MEMORY_CURRENT_RESIZE_OPS displays information about memory resize operations (both automatic and manual) which are currently in progress.

  • V$MEMORY_DYNAMIC_COMPONENTS displays information about the current sizes of all dynamically tuned memory components, including the total sizes of the SGA and instance PGA.

  • V$MEMORY_RESIZE_OPS displays information about the last 800 completed memory resize operations (both automatic and manual). This does not include in-progress operations.

  • V$MEMORY_TARGET_ADVICE displays tuning advice for the MEMORY_TARGET initialization parameter.

  • V$SGA_CURRENT_RESIZE_OPS displays information about SGA resize operations that are currently in progress. An operation can be a grow or a shrink of a dynamic SGA component.

  • V$SGA_RESIZE_OPS displays information about the last 800 completed SGA resize operations. This does not include any operations currently in progress.

  • V$SGA_DYNAMIC_COMPONENTS displays information about the dynamic components in SGA. This view summarizes information based on all completed SGA resize operations that occurred after startup.

  • V$SGA_DYNAMIC_FREE_MEMORY displays information about the amount of SGA memory available for future dynamic SGA resize operations.


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI