在网络或者书籍中,我们可以非常容易的了解到ORACLE中游标的生命周期包括如下部分: 1,打开游标-- open cursor,此步骤在 UGA 里申请一块内存给游标使用,这个时候游标还没有与sql语句关联。 2,解析游标-- sql与游标关联起来,解析sql的内容(包括执行计
在网络或者书籍中,我们可以非常容易的了解到ORACLE中游标的生命周期包括如下部分:
1,打开游标-- open cursor,此步骤在 UGA 里申请一块内存给游标使用,这个时候游标还没有与sql语句关联。
2,解析游标-- sql与游标关联起来,解析sql的内容(包括执行计划),解析后的内容会被加载到共享池中(share pool-- library cache)。在UGA申请的内存用来保存指向这个共享游标(share cursor)在library cache中的位置。
3,定义输出变量-- 如果sql语句返回数据,必须先定义接收数据的变量。这一点不仅对查询语句很重要,对于使用returning 自居的delete、insert和update 语句也很重要。
4,绑定输入变量-- 如果sql语句使用了绑定变量,必须提供他们的值。绑定的过程是不做什么检查。如果指定了无效的数据,执行的过程中会爆出一个运行时错误。
5,执行游标-- 执行跟游标关联的sql。注意 数据库并非总是在这一步做重要的事情。事实上,对于很多类型的查询语句来说,真正的处理过程通常会被推迟到fetch数据阶段。
6,获取游标-- 如果sql语句返回数据,这一步会接受这些数据。特别是在查询语句中,大部分的处理工作都是在这一步进行的。在查询语句中,可能只会读取部分记录,换句话讲,游标有可能在取到所有记录前被关闭。
7,关闭游标-- 释放UGA中与这个游标有关的资源,从而这些资源可供其他的游标使用。在library cache中的share cursor不会被清除,它会继续保留在library cache 中,等待被重用(软解析重用)。
重复的内容,我们不做过多介绍,今天我们来看一下游标生命周期中各个部分所扮演的角色,以及如何利用它们来优化我们的程序。
借鉴《oracle性能诊断艺术》中的代码片段,我们来研究一下游标的生命周期;
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
create or replace procedure cursor_test as
l_ename emp.ename%TYPE := 'SCOTT';
l_empno dbms_sql.Number_Table;
l_cursor INTEGER;
l_retval INTEGER;
cnt integer := 1;
indx integer := 1;
res varchar2(4000);
BEGIN
for i in 1 .. 1000 loop
l_cursor := DBMS_SQL.open_cursor;
DBMS_SQL.parse(l_cursor,
'select empno from emp where ename :ename and 0 '||i , DBMS_SQL.native);
l_empno.delete();
DBMS_SQL.define_array(l_cursor, 1, l_empno,cnt,indx);
DBMS_SQL.bind_variable(l_cursor, ':ename', to_char(i));
l_retval := DBMS_SQL.execute(l_cursor);
while DBMS_SQL.fetch_rows(l_cursor) > 0 loop
dbms_sql.column_value(l_cursor, 1, l_empno);
end loop;
res :='';
for j in 1 .. l_empno.count() loop
res := res || L_EMPNO(j);
end loop;
DBMS_OUTPUT.PUT_LINE(res);
dbms_sql.close_cursor(l_cursor);
end loop;
end;
通过plsql profiler跟踪各行代码的执行效率如下:
可以看出,游标生命周期中的各个部分均会占有执行时间,因此,如果可以消除一些执行步骤显然会提高性能. 那些操作可以消除那?参加下图
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
-- -----------
-- | open_cursor |
-- -----------
-- |
-- |
-- v
-- -----
-- ------------>| parse |
-- | -----
-- | |
-- | |---------

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.