Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand latch

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how to understand latch. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. Overview of serialization

The database system itself is a multi-user concurrent processing system. At the same point in time, multiple users may operate the database at the same time. When multiple users write data in the same physical location at the same time, they cannot cover each other. This is called serialization, which reduces the concurrency of the system, but it is necessary to protect the data structure from being destroyed. In the Oracle database, these two problems are solved through latch and lock.

Latches and locks have both similarities and differences. What they have in common is that they are all resources for serialization. The difference is that the Latch is a low-level, lightweight lock that can be acquired and released quickly, similar to a semaphore. Lock, on the other hand, can last for a long time, through the use of queues, on a first-in-first-out basis. It can also be simply understood that latches are micro-domain, while locking is macro-domain.

Note: latch is a serialized locking mechanism used to protect shared data structures in the SGA zone. It is not only used for buffer cache, but also for shared pool, log buffer and so on.

Overview of 2.Latch

Oracle databases use latches (latch) to manage the allocation and release of SGA memory. Latch is a serialized locking mechanism used to protect shared data structures in the SGA zone. The implementation of Latch is related to the operating system, especially whether a process needs to wait for a latch and how long it needs to wait.

Latch is a lock that can be acquired and released very quickly, and it is usually used to protect the data structure that describes the block in buffer cache.

3.SPIN and dormancy

(1) SPIN

A spin is a process that monopolizes the cpu time until the end of the run. Other processes cannot get the elapsed time of this cpu during this period. There is no spin concept for a single CPU.

For example, if a block in the data cache is to be read, we will get the latch of this block. This process is called spin. Another process happens to modify this block, and he also wants to spin this block. At this time, he must wait for the current process to release latch before it can spin live, and then modify it. If multiple processes request at the same time, there will be competition between them, without a queue mechanism, once the previous process releases the set. The rest of the process flocked, there was no first-come-first-come concept, and it all happened very quickly, because Latch is characterized by being fast and short.

(2)。 Dormancy

Hibernation means temporarily abandoning CPU and performing context switching (context switch), so that CPU saves some state information of the current process when it is running, such as stack, semaphore and other data structures, and then introduces the state information of subsequent processes, and then switches back to the original process state after processing. If this process occurs frequently in a processing system with high transactions and high concurrent processes, it will be a very expensive resource consumption. So Oracle chooses spin to let the process continue to occupy the CPU, run some empty instructions, then continue the request, continue the spin until the value of _ spin_ count is reached, and then abandon the CPU, sleep for a short time, and then continue the previous action. In the initial state, a process sleeps for 0.01 seconds. Then wake up and try to get the latch again. Once the process goes to sleep, it throws a corresponding wait event and records it in the view v$session_wait, indicating the type of latch that the process is currently waiting for.

4. Types of Latch

(1) Willing-To-Wait

At any time, only one process can access a block of data in memory. If the process cannot get the Latch because another process is occupying the block, it will spin the CPU for a very short time, and the spin will continue to be obtained, but will remain spin unsuccessfully until the number of spin reaches the threshold limit (this is specified by the implicit parameter _ spin_count). At this time, the process will stop spin and sleep for a short time. After hibernation, the previous action continues until the Latch on the block is obtained.

(the following fields reflect the Willing-to-wait type request)

GETS: the number of times a latch was successfully requested with the Willing-to-wait request type.

MISSES: the number of times an latch was not successfully requested initially with the Willing-to-wait request type.

SLEEPS: the number of times the process waits to get the latch after the initial request for a latch with the Willing-to-wait request type is unsuccessful.

(2) Immediate

There are relatively few latch of this type, and there are many latch available for this type of latch. When a process requests one of the latch, it starts the request in no-wait mode. If the requested latch is not available, the process does not wait, but immediately requests another latch. Wait only when all the latch is not available.

(the following fields reflect the Immediate type request)

IMMEDIATE_GETS: the number of times a latch was successfully obtained with the Immediate request type.

IMMEDIATE_MISSES: the number of times a latch was requested unsuccessfully with the Immediate request type.

5.Latch acquisition process

The difference between 6.Latch and Lock

-Latch access, including queries, is also mutually exclusive. At any time, only one process can pin a certain block of memory.

-latch only acts on memory and can only be accessed by the current instance, while Lock acts on database objects

-Latch is an instant occupation and release. The release of Lock needs to wait until the transaction ends correctly. The time it takes is determined by the size of the transaction.

-Latch is not on the team, while Lock is on the team.

-there is no deadlock in Latch, but it exists in Lock.

7. Cleanup of Latch

Session,pmon that cannot release latch for a long time due to an exception will be cleaned every 3 seconds.

8. Level of Latch

Divided into 0-14 level, when a process occupies a latch, it can only request a higher-level latch, and if you want to request a low-level latch, you need to release the current latch.

9. Resource contention of Latch

(1) .SQL statement

If you do not use binding variables, it is easy to read and write memory blocks in shared pool frequently. If a large number of SQL is repeatedly analyzed, it will cause a lot of Latch contention and a long wait, resulting in Latch contention in the shared pool associated with parsing SQL. The latch associated with shared pool shared pools are Library Cache Latch and Shared Pool Latch. If there is contention for the above latch in the database, it is necessary to check whether binding variables are used correctly.

(2) cache buffer chain

When a session needs to access a memory block, it first goes to a structure like a linked list to search whether the data block is in memory. When the session accesses the linked list, it needs to get a Latch. If the acquisition fails, Latch cache buffer chain wait will occur. The reason for this wait is that there are too many sessions accessing the same data block or the list is too long (if you read too much data in memory. The hash list that needs to manage the data block will be very long, so the session scanning list time will increase, the chache buffer chain latch holding time will be longer, the chances of other sessions getting this Latch will be reduced, and the wait will increase.

(3) Buffer busy waits

When a session needs to access a block of data that is being read from disk to memory by another user or is being modified by another session, the current session needs to wait, resulting in a buffer busy waits wait.

The direct cause of these Latch contention is the hot fast problem caused by too many sessions accessing the same data block, which may be caused by database settings or repeated SQL frequently accessing some of the same data blocks. The causes of hot blocks are different, according to the type of data blocks, can be divided into the following hot block types, different hot block types are handled differently: table data block, index data block, index root data block and file header data block.

The above is the editor for you to share how to understand latch, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report