Oracle游标共享,父游标和子游标的概念
Oracle设计shared pool内存的一个主要目的就是语句共享,通过语句共享,节省语句的解析时间,从而得到性能的提升。如果解释shared pool内存,工作量太大,这里只是做一个非常简单的演示。 查看语句共享可以借助两个数据字典:V$SQLAREA和V$SQL V$SQLAREA:保
Oracle设计shared pool内存的一个主要目的就是语句共享,通过语句共享,节省语句的解析时间,从而得到性能的提升。如果解释shared pool内存,工作量太大,这里只是做一个非常简单的演示。
查看语句共享可以借助两个数据字典:V$SQLAREA和V$SQL
V$SQLAREA:保留SQL语句的父游标信息,可以通过SQL_ID标识,其中的VERSION_COUNT列表示子游标的数量
V$SQL
V$SQL_SHARED_CURSOR:语句产生子游标的原因
首先确认参数cursor_sharing,默认值是EXACT,也就是说只有在不使用绑定变量的情况下,语句要完全一样才可以共享,包括大小写、空格回车等所有都要一样。
SQL> conn / as sysdba
Connected.
SQL> show parameter cursor_sharing
NAME
------------------------------------ ----------- ------------------------------
cursor_sharing
清空shared_pool内存,这个命令可以在实验的时候使用,在生产系统库要谨慎
SQL> alter system flush shared_pool;
System altered.
在SCOTT用户下和TJ用户下有一模一样的表叫做demo,这是我准备的实验场景,以下操作,浅色表示第一个窗口或者session,深色表示在另外一个窗口或session查询动态性能视图信息验证
在第一个窗口:
SQL> conn scott/tiger
Connected.
SQL> select * from demo;
---------- ---------- ----------
SQL> select empno,ename from demo where empno=7369;
---------- ----------
由于上边这条语句是清空share pool后第一次执行,所以Oracle要做硬解析,生成游标,确切来讲是一个父游标和一个子游标,分别可以通过V$SQLAREA和V$SQL查到相关信息
在第二个窗口:
[oracle@asm11g workshop]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on Sun Sep 16 21:20:24 2012
Copyright (c) 1982, 2011, Oracle.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
SQL> get qs.sql
SQL> @qs
Enter value for text: select empno
old
new
SQL_TEXT
-------------------------------------------------- -------------
select empno,ename from demo where empno=7369
SQL> select sql_id,child_number,executions,loads from v$sql where sql_id='dhdkpzyv9b1w7';
SQL_ID
------------- ------------ ---------- -----
dhdkpzyv9b1w7
通过查询我们可以看到,V$SQLAREA数据字典中的是父游标的信息,语句解析(LOADS)了一次,执行(EXEC)了一次,当然在V$SQL中也可以看到类似的信息。
到第一个窗口:
SQL> select empno,ename from demo where empno=7369;
---------- ----------
再一次执行上一条语句,由于语句在share pool内存中已经有游标存在,所以语句会共享。
到第二个窗口:
SQL> @qs
Enter value for text: select empno
old
new
SQL_TEXT
-------------------------------------------------- -------------
select empno,ename from demo where empno=7369
SQL> select sql_id,child_number,executions,loads from v$sql where sql_id='dhdkpzyv9b1w7';
SQL_ID
------------- ------------ ---------- -----
dhdkpzyv9b1w7
通过上面的查询我们可以看到,语句解析(LOADS)了一次,执行(EXEC)了两次,在子游标也可以看到相同的信息。
到第一个窗口:
SQL> select empno,ename from demo where empno=7499;
---------- ----------
这个语句和刚刚的语句的区别在于我把条件改成了7499,这样这就是一个全新的语句,Oracle要做硬解析,并在内存中申请新的父子游标。
第二个窗口:
SQL> @qs
Enter value for text: select empno
old
new
SQL_TEXT
-------------------------------------------------- -------------
select empno,ename from demo where empno=7499
select empno,ename from demo where empno=7369
到第一个窗口:
切换用户到tj,tj用户下也有相同的表demo,执行select empno,ename from demo where empno=7369;
SQL> conn tj/tj
Connected.
SQL> select empno,ename from demo where empno=7369;
---------- ----------
到第二个窗口:
SQL> @qs
Enter value for text: select empno
old
new
SQL_TEXT
-------------------------------------------------- -------------
select empno,ename from demo where empno=7499
select empno,ename from demo where empno=7369
SQL> select sql_id,child_number,executions,loads from v$sql where sql_id='dhdkpzyv9b1w7';
SQL_ID
------------- ------------ ---------- -----
dhdkpzyv9b1w7
dhdkpzyv9b1w7
虽然tj用户下的语句和scott用户下写的语句一模一样,但是语意显然不一样,两个demo表属于不同的用户,所以我们看到,Oracle的父游标解析(LOADS)加1,执行次数也加1,从父游标的角度来讲,语句是共享的,而在子游标中,有了区别,新生成了一个子游标CHILD_NUMBER 为1,解析(LOADS)了一次,执行了一次。
到第一个窗口:
再次执行语句
SQL> select empno,ename from demo where empno=7369;
---------- ----------
到第二个窗口:
SQL> @qs
Enter value for text: select empno
old
new
SQL_TEXT
-------------------------------------------------- -------------
select empno,ename from demo where empno=7499
select empno,ename from demo where empno=7369
SQL> select sql_id,child_number,executions,loads from v$sql where sql_id='dhdkpzyv9b1w7';
SQL_ID
------------- ------------ ---------- -----
dhdkpzyv9b1w7
dhdkpzyv9b1w7
我们看到语句的父游标解析次数没有增加,执行次数加1,而对于子游标来说,CHILD_NUMBER 为1的子游标执行次数加1。
产生子游标的原因很多,比如我上边的用户方案(SCHEMA)改变的例子,当然还有很多其他原因也可以导致子游标的产生,比如优化器模式的改变,或者绑定变量的窥视等,如果你想确定是由那种原因造成的,需要查看v$sql_shared_cursor
对于刚才的例子,属于验证/事物检查不匹配
SQL> select sql_id,CHILD_NUMBER,AUTH_CHECK_MISMATCH from v$sql_shared_cursor where sql_id='dhdkpzyv9b1w7';
SQL_ID
------------- ------------ -
dhdkpzyv9b1w7
dhdkpzyv9b1w7

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.
