search
HomeDatabaseMysql TutorialOracle读一致性学习笔记

为避免ORA-1555快照太旧的错误,出现了undo_retention参数,表示当事务提交或回滚后,该事务所使用的undo块里的数据需要保留多长

1 回滚与撤销

       Refer:《深入解析Oracle》by eygle

(1)    为了多用户的读一致性和能回退事务,oracle提供了为修改的数据保存修改之前的旧值。

(2)    Redo:保证在故障时事务可以恢复

Undo:保证事务可以被回滚或撤销

(3)    9i之前,oracle提供回滚段(rollback)来撤销数据;之后,oracle使用undo表空间来管理。

(4)    下面这个例子是介绍9i前,是如何保证可以回滚的。

Update emp set sal=4000 where empno=7788;

简单看一下这个语句的执行过程:

A:检查empno=7788记录在database buffer cache中是否存在;否,则读取到database buffer cache中。

B:在回滚表空间的相应回滚段事务表上分配事物槽,这个操作需要记录redo信息。

C:在回滚段读入或者在database buffer cache中创建sal=3000的旧值,这需要产生redo信息并记入redo log buffer。

【B,C这两步保证了事务的可回滚性,此后事务的修改才能进行】

D:修改sal=4000,这是update的数据变更,需要记录redo log buffer。

F:当用户提交时,会在redo log buffer记录提交信息,并在回滚段标记该事务为非激活(inactive)。

(5)    如果用户回滚(rollback)事务,则oracle需要从回滚段中把旧值读取出来,修改database buffer cache,完成rollback,这个过程本身会产生redo,so回滚是expensive。

(6)    回滚段在undo表空间中分配,其作用:回退事务,事务恢复,提供读一致性。

对于DML:

Insert:回滚段只需记录插入的记录的rowid;

Update:回滚段只需记录被更新字段的旧值;

Delete:oracle必须记录整行的数据

所以,对产生undo的情况看,delete产生的undo最多,推荐对大规模数据删除操作时,分批删除,分次提交,以减少对回滚段的占用和冲击。

(7)    oracle区别于其他数据库的一个重要的特征:

通过多版本架构,oracle实现了读取和写入的分离,使得写入不阻塞读取,读取不阻塞写入。

多版本架构是通过一致性读来实现的。

      

  假定scott的薪水为3000:

A:t1时刻我们在session 1 查询可以得到3000;

B:t2时刻session 2进行update,但未提交(此时数据在database buffer cache中已经修改,该buffer为dirty)

C:t3时刻session 1再次查询,注意此时,oracle不会允许其他用户看到未提交的数据,oracle需要通过回滚段记录的旧值进行一致性读,将3000恢复出来给用户,这是一致性读的作用;

D:t4时刻,session 2提交该更改,此时数据修改已被永久化;

F:t5 时刻,其他用户再次查询;将会看到变化后的数据,也就是4000.

       Notice:

              A:每个数据块头部都会记录一个提交的SCN,当数据更改提交后,提交后,提交SCN同时被修改,这个SCN在查询时可以用来进行一致性读的判断。

              B:上图中,假定查询开始的时间为t1,则查询获取的数据块中,如果数据块的提交SCN小于t1,则oracle接收该数据【session 2便是这种情况】;如果提交SCN大于t1或者数据被锁定修改尚未记录提交SCN,则oracle需要通过回滚段构造前镜像来返回结果【session 1便是这种情景】,这就是一致性读的本质含义。

       (8)9i之后,oracle引入了undo表空间,若选择自动的undo表空间管理,,则oracle会动态创建和释放回滚段,自动为事务指定回滚段。

                命令:Show parameter undo

                里面有个参数undo_retention与ORA_01555错误有关。

                这个参数可调:alter system set undo_retention=**;

                Undo_retention:当事务提交后undo信息保留的时间(秒);

                10g对于undo增加了guarantee控制:

                     Alter tablespace undotbs1 retention guarantee | noguarantee

                     区别:

                            若把undo表空间自动扩展属性取消:

                     Alter database datafile ‘/u01/app/oracle/product/10.2.0/oradata/undotbs’

Autoextend off;

                     进行循环删除数据

                     在guarantee设置下,会出现ORA-30036错误;

                     在noguarantee设置下,则可以顺利完成,因为oracle启动自动调整以满足最长运行查询的需要。

       (9) ORA-01555成因与解决

              A:成因:

I)              由于回滚段是循环使用的,当事务提交后,该事务占用的回滚段事务表会被标识为inactive,回滚段表空间可以被覆盖重用。但是,当一个查询需要使用被覆盖的回滚段构造前镜像实现一致性读,那么就会出现著名的ORA-01555错误。

II)           未完待续。。。。。。。。。。

2     大师,你好!
   我在《深入浅出oracle》一书的167页的一句话不清楚,原话是:如果读取的block不满足读一致性需求,则server进程需要通过当前block版本和回滚段构造前镜像返回给用户。‘读一致性’是怎么判断出来的,被修改的数据要作排他锁,别人怎么读取那,盼回复,谢谢!

     根据SCN来判断阿。

3读一致性(Read Consistency),这是数据库的一个关键特性,可以确保用户在查询期间看到一致的数据。也就是说,当一个会话正在修改数据时,其他的会话将看不到该会话未提交的修改。

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.